23种设计模式之五(代理模式)

Posted 淼淼之森

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了23种设计模式之五(代理模式)相关的知识,希望对你有一定的参考价值。

代理模式:(为一个对象提供一个替身,以控制对这个对象的访问)

 被代理的对象可以是远程对象、创建开销大的对象或需要安全控制的对象;代理模式有很多变体,都是为了控制与管理对象访问。

代理模式包含如下角色:

ISubject:抽象主题角色,是一个接口。该接口是对象和它的代理共用的接口。

RealSubject:真实主题角色,是实现抽象主题接口的类。

Proxy:代理角色,内部含有对真实对象RealSubject的引用,从而可以操作真实对象。代理对象提供与真实对象相同的接口,以便在任何时刻都能代替真实对象。同时,代理对象可以在执行真实对象操作时,附加其他的操作,相当于对真实对象进行封装。

实现动态代理的关键技术是反射。

 

例子:(通过这个例子,从不使用代理模式,到使用代理模式)

监控糖果机:地点、糖果库存和当前状态

ISubject

 1 package com.java.mmzs.agentmode.candymachine;
 2 
 3 import java.io.Serializable;
 4 
 5 public interface State extends Serializable{
 6     public void insertCoin();
 7     public void returnCoin();
 8     public void turnCrank();
 9     public void dispense();
10     public void printstate();
11     public String getstatename();
12 }
State
 1 package com.java.mmzs.agentmode.candymachinermi;
 2 
 3 import java.rmi.Remote;
 4 import java.rmi.RemoteException;
 5 
 6 import com.java.mmzs.agentmode.candymachine.State;
 7 
 8 public interface CandyMachineRemote extends Remote{
 9     public String  getLocation() throws RemoteException;
10     public int getCount() throws RemoteException;
11     public State getstate() throws RemoteException;
12 }
CandyMachineRemote

RealSubject

 1 package com.java.mmzs.agentmode.candymachine;
 2 
 3 public class OnReadyState implements State {
 4     private transient CandyMachine mCandyMachine;
 5     public OnReadyState(CandyMachine mCandyMachine)
 6     {
 7         this.mCandyMachine=mCandyMachine;
 8     }
 9 
10     @Override
11     public void insertCoin() {
12         // TODO Auto-generated method stub
13         System.out
14         .println("you have inserted a coin,next,please turn crank!");
15         mCandyMachine.setState(mCandyMachine.mHasCoin);
16     }
17 
18     @Override
19     public void returnCoin() {
20         // TODO Auto-generated method stub
21         System.out.println("you haven\'t inserted a coin yet!");
22         
23     }
24 
25     @Override
26     public void turnCrank() {
27         // TODO Auto-generated method stub
28         System.out.println("you turned,but you haven\'t inserted a coin!");
29         
30     }
31 
32     @Override
33     public void dispense() {
34         // TODO Auto-generated method stub
35 
36     }
37 
38     @Override
39     public void printstate() {
40         // TODO Auto-generated method stub
41         System.out.println("***OnReadyState***");
42         
43     }
44     @Override
45     public String getstatename() {
46         // TODO Auto-generated method stub
47         return "OnReadyState";
48     }
49 }
OnReadyState
 1 package com.java.mmzs.agentmode.candymachine;
 2 
 3 import java.util.Random;
 4 
 5 public class HasCoin implements State {
 6     private transient CandyMachine mCandyMachine;
 7 
 8     public HasCoin(CandyMachine mCandyMachine) {
 9         this.mCandyMachine = mCandyMachine;
10     }
11 
12     @Override
13     public void insertCoin() {
14         // TODO Auto-generated method stub
15         System.out.println("you can\'t insert another coin!");
16 
17     }
18 
19     @Override
20     public void returnCoin() {
21         // TODO Auto-generated method stub
22         System.out.println("coin return!");
23         mCandyMachine.setState(mCandyMachine.mOnReadyState);
24     }
25 
26     @Override
27     public void turnCrank() {
28         // TODO Auto-generated method stub
29         System.out.println("crank turn...!");
30         Random ranwinner=new Random();
31         int winner=ranwinner.nextInt(10);
32         if(winner==0)
33         {
34             mCandyMachine.setState(mCandyMachine.mWinnerState);
35 
36         }else
37         {
38             mCandyMachine.setState(mCandyMachine.mSoldState);
39 
40         }
41         
42     }
43 
44     @Override
45     public void dispense() {
46     }
47 
48     @Override
49     public void printstate() {
50         // TODO Auto-generated method stub
51         System.out.println("***HasCoin***");
52 
53     }
54 
55     @Override
56     public String getstatename() {
57         // TODO Auto-generated method stub
58         return "HasCoin State";
59     }
60 
61 }
HasCoin
 1 package com.java.mmzs.agentmode.candymachine;
 2 
 3 public class SoldState implements State {
 4     private transient CandyMachine mCandyMachine;
 5     public SoldState(CandyMachine mCandyMachine)
 6     {
 7         this.mCandyMachine=mCandyMachine;
 8     }
 9 
10     @Override
11     public void insertCoin() {
12         // TODO Auto-generated method stub
13         System.out.println("please wait!we are giving you a candy!");
14 
15     }
16 
17     @Override
18     public void returnCoin() {
19         // TODO Auto-generated method stub
20         System.out.println("you haven\'t inserted a coin yet!");
21         
22     }
23 
24     @Override
25     public void turnCrank() {
26         // TODO Auto-generated method stub
27         System.out
28         .println("we are giving you a candy,turning another get nothing,!");
29 
30     }
31 
32     @Override
33     public void dispense() {
34         // TODO Auto-generated method stub
35         
36         mCandyMachine.releaseCandy();
37         if (mCandyMachine.getCount() > 0) {
38             mCandyMachine.setState(mCandyMachine.mOnReadyState);
39         } else {
40             System.out.println("Oo,out of candies");
41             mCandyMachine.setState(mCandyMachine.mSoldOutState);
42         }
43 
44     
45     
46     }
47 
48     @Override
49     public void printstate() {
50         // TODO Auto-generated method stub
51         System.out.println("***SoldState***");
52         
53     }
54     @Override
55     public String getstatename() {
56         // TODO Auto-generated method stub
57         return "SoldState";
58     }
59 }
SoldState
 1 package com.java.mmzs.agentmode.candymachine;
 2 
 3 public class SoldOutState implements State {
 4 
 5     private  transient CandyMachine mCandyMachine;
 6     public SoldOutState(CandyMachine mCandyMachine)
 7     {
 8         this.mCandyMachine=mCandyMachine;
 9     }
10 
11     @Override
12     public void insertCoin() {
13         // TODO Auto-generated method stub
14         System.out.println("you can\'t insert coin,the machine sold out!");
15         
16     }
17 
18     @Override
19     public void returnCoin() {
20         // TODO Auto-generated method stub
21         System.out
22         .println("you can\'t return,you haven\'t inserted a coin yet!");
23 
24     }
25 
26     @Override
27     public void turnCrank() {
28         // TODO Auto-generated method stub
29         System.out.println("you turned,but there are no candies!");
30         
31     }
32 
33     @Override
34     public void dispense() {
35         // TODO Auto-generated method stub
36 
37     }
38 
39     @Override
40     public void printstate() {
41         // TODO Auto-generated method stub
42         System.out.println("***SoldOutState***");
43     
44     }
45     @Override
46     public String getstatename() {
47         // TODO Auto-generated method stub
48         return "SoldOutState";
49     }
50 }
SoldOutState
 1 package com.java.mmzs.agentmode.candymachine;
 2 
 3 public class WinnerState implements State {
 4 
 5     private transient CandyMachine mCandyMachine;
 6 
 7     public WinnerState(CandyMachine mCandyMachine) {
 8         this.mCandyMachine = mCandyMachine;
 9     }
10 
11     @Override
12     public void insertCoin() {
13         // TODO Auto-generated method stub
14         System.out.println("please wait!we are giving you a candy!");
15 
16     }
17 
18     @Override
19     public void returnCoin() {
20         // TODO Auto-generated method stub
21         System.out.println("you haven\'t inserted a coin yet!");
22 
23     }
24 
25     @Override
26     public void turnCrank() {
27         // TODO Auto-generated method stub
28         System.out
29                 .println("we are giving you a candy,turning another get nothing,!");
30 
31     }
32 
33     @Override
34     public void dispense() {
35         // TODO Auto-generated method stub
36 
37         
38         mCandyMachine.releaseCandy();
39         if (mCandyMachine.getCount() == 0) {
40             mCandyMachine.setState(mCandyMachine.mSoldOutState);
41         } else {
42             System.out.println("you are a winner!you get another candy!");
43             mCandyMachine.releaseCandy();
44             if (mCandyMachine.getCount() > 0) {
45                 mCandyMachine.setState(mCandyMachine.mOnReadyState);
46             } else {
47                 System.out.println("Oo,out of candies");
48                 mCandyMachine.setState(mCandyMachine.mSoldOutState);
49             }
50         }
51 
52     }
53 
54     @Override
55     public void printstate() {
56         // TODO Auto-generated method stub
57         System.out.println("***WinnerState***");
58 
59     }
60     @Override
61     public String getstatename() {
62         // TODO Auto-generated method stub
63         return "WinnerState";
64     }
65 }
WinnerState
 1 package com.java.mmzs.agentmode.candymachine;
 2 
 3 import java.rmi.RemoteException;
 4 import java.rmi.server.UnicastRemoteObject;
 5 
 6 import com.java.mmzs.agentmode.candymachinermi.CandyMachineRemote;
 7 
 8 public class CandyMachine extends UnicastRemoteObject implements CandyMachineRemote{
 9 
10     State mSoldOutState;
11     State mOnReadyState;
12     State mHasCoin;
13     State mSoldState;
14     State mWinnerState;
15     private String location="";
16     private State state;
17     private int count = 0;
18 
19     public CandyMachine(String location,int count) throws RemoteException{
20         this.location=location;
21         this.count = count;
22         mSoldOutState = new SoldOutState(this);
23         mOnReadyState = new OnReadyState(this);
24         mHasCoin = new HasCoin(this);
25         mSoldState = new SoldState(this);
26         mWinnerState = new WinnerState(this);
27         if (count > 0) {
28             state = mOnReadyState;
29         } else {
30             state = mSoldOutState;
31         }
32     }
33     public String getLocation()
34     {
35         return location;
36     }
37     public void setState(State state) {
38         this.state = state;
39     }
40 
41     public void insertCoin() {
42         state.insertCoin();
43     }
44 
45     public void returnCoin() {
46         state.returnCoin();
47     }
48 
49     public void turnCrank() {
50         state.turnCrank();
51         state.dispense();
52     }
53 
54     void releaseCandy() {
55 
56         // TODO Auto-generated method stub
57         if (count > 0) {
58             count = count - 1;
59             System.out.println("a candy rolling out!");
60         }
61 
62     }
63 
64     public int getCount() {
65         return count;
66     }
67 
68     public void printstate() {
69         state.printstate();
70     }
71     public State getstate() {
72         return state;
73     }
74 }
CandyMachine

不使用代理:

 1 package com.java.mmzs.agentmode;
 2 
 3 import java.util.ArrayList;
 4 
 5 import com.java.mmzs.agentmode.candymachine.CandyMachine;
 6 
 7 public class Monitor {
 8     private ArrayList<CandyMachine> candyMachinelst;
 9 
10     public Monitor() {
11         candyMachinelst = new ArrayList<CandyMachine>();
12     }
13 
14     public void addMachine(CandyMachine mCandyMachine) {
15         candyMachinelst.add(mCandyMachine);
16     }
17 
18     public void report() {
19         CandyMachine mCandyMachine;
20         for (int i = 0, len = candyMachinelst.size(); i < len; i++) {
21             mCandyMachine = candyMachinelst.get(i);
22             System.out.println("Machine Loc:" + mCandyMachine.getLocation());
23             System.out.println("Machine Candy count:"
24                     + mCandyMachine.getCount());
25             System.out.println("Machine State:"
26                     + mCandyMachine.getstate().getstatename());
27 
28         }
29 
30     }
31 
32 }
Monitor
 1 package com.java.mmzs.agentmode;
 2 
 3 import java.rmi.RemoteException;
 4 
 5 import com.java.mmzs.agentmode.candymachine.CandyMachine;
 6 
 7 public class MainTest {
 8     public static void main(String[] args) throws RemoteException {
 9         Monitor mMonitor=new Monitor();
10         CandyMachine mCandyMachine = new CandyMachine("NY",6);
11         mMonitor.addMachine(mCandyMachine);
23种设计模式(23):代理模式

23种设计模式之代理模式

23种设计模式-静态代理模式

23种设计模式-代理模式

23种设计模式-代理模式

23种设计模式之代理模式(Proxy)