代理模式详解

内容:

1、什么是代理模式

2、静态代理模式

3、动态代理模式

 

 

 

1、什么是代理模式

代理模式的定义:代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用。通俗的来讲代理模式就像生活中常见的中介。

举个例子来说明:比如通过二手车商买二手车,如下图所示:

为什么要使用代理模式:

(1)中介隔离作用:在某些情况下,一个客户类不想或者不能直接引用一个委托对象,而代理类对象可以在

客户类和委托对象之间起到中介的作用,其特征是代理类和委托类实现相同的接口

(2)开闭原则,增加功能:代理类除了是客户类和委托类的中介之外,我们还可以通过给代理类增加额外的功能来

扩展委托类的功能,这样做我们只需要修改代理类而不需要再修改委托类,符合代码设计的开闭原则。

代理类主要负责为委托类预处理消息、过滤消息、把消息转发给委托类,以及事后对返回结果的处理等。

代理类本身并不真正实现服务,而是同过调用委托类的相关方法,来提供特定的服务。真正的业务功能

还是由委托类来实现,但是可以在业务功能执行的前后加入一些公共的服务。例如我们想给项目加入

缓存、日志这些功能,我们就可以使用代理类来完成,而没必要打开已经封装好的委托类。

 

代理模式简单实例:

 1 /*
 2  *     接口
 3  */
 4 public interface NvYouIntetface {
 5     // 做饭
 6     public abstract void cook();
 7 
 8     // 倒水
 9     public abstract void daoShui();
10 
11     // 按摩
12     public abstract void moMo();
13 }
 1 // 男孩的方法中通过NvYouIntetface对象对应的实现类对象来调用实现一个简单的代理
 2 // 男孩
 3 class Boy {
 4     String name;
 5     NvYouIntetface girlFirend;    // 女友
 6     
 7     public void hungry(){
 8         System.out.println(this.name + " hungry...");
 9         girlFirend.cook();
10     }
11     
12     public void thirsty(){
13         System.out.println(this.name + " thirsty...");
14         girlFirend.daoShui();
15     }
16     
17     public void tired(){
18         System.out.println(this.name + " tired...");
19         girlFirend.moMo();
20     }
21 }
22 
23 // 女孩
24 class Girl implements NvYouIntetface{
25     String name;
26     
27     public void cook(){
28         System.out.println(this.name + " in cooking...");
29     }
30     
31     public void daoShui(){
32         System.out.println(this.name + " in daoShui...");
33     }
34     
35     public void moMo(){
36         System.out.println(this.name + " in anmo...");
37     }
38 }
39 
40 // 普通人
41 class Man implements NvYouIntetface{
42     String name;
43     
44     public void cook(){
45         System.out.println("自己做饭...");
46     }
47     
48     public void daoShui(){
49         System.out.println("自己倒水...");
50     }
51     
52     public void moMo(){
53         System.out.println("按个屁a....");
54     }
55 }
56 
57 public class easyDemo {
58     public static void main(String[] args) {
59         Boy xp = new Boy();
60         xp.name = "saf";
61         
62         Girl xx = new Girl();
63         xx.name = "xia";
64         
65         Man mm = new Man();
66         mm.name = "xiaoMin";
67         
68         // 他们之间的关系:
69         xp.girlFirend = xx;
70         xp.hungry();
71         xp.thirsty();
72         xp.tired();
73         
74         System.out.println();
75         xp.girlFirend = mm;
76         xp.hungry();
77         xp.thirsty();
78         xp.tired();
79         
80     }
81 }

 

 

2、静态代理模式

 1 /*
 2  * 代理商和电脑公司的接口
 3  * 
 4  * */
 5 public interface ComputerInterface {
 6     
 7     // 卖电脑
 8     public abstract String sellComputer();
 9     
10     // 修电脑
11     public abstract void repairComputer();
12     
13 }
14 
15 
16 // 被代理类 =》 电脑公司 =》 实现类(被代理类)
17 class ComputerCompany implements ComputerInterface{
18 
19     public String sellComputer() {
20         return "M7-6888-1T电脑, 4888¥";
21     }
22 
23     public void repairComputer() {
24         // TODO Auto-generated method stub
25         System.out.println("三年内保修");
26     }
27     
28 }
29 
30 // 代理类 =》 经销商  =》 代理类
31 class ProxyPerson implements ComputerInterface{
32 
33     private ComputerInterface myCompany;    // 被代理对象
34     
35     public ProxyPerson(ComputerInterface company) {
36         this.myCompany = company;
37     }
38 
39     public String sellComputer() {
40         // TODO Auto-generated method stub
41         return "鼠标, 键盘, 电脑包, U盘, 操作系统 1000¥ + " + myCompany.sellComputer();
42     }
43 
44     public void repairComputer() {
45         // TODO Auto-generated method stub
46         System.out.println("运费80块");
47         myCompany.repairComputer();
48     }
49     
50 }
51 
52 public class ProxyDemo {
53     public static void main(String[] args) {
54         // 被代理公司
55         ComputerCompany company = new ComputerCompany();
56         // 代理对象
57         ProxyPerson pp = new ProxyPerson(company);
58         // 下面的操作通过访问代理对象的方法实现
59         // 卖电脑
60         String s = pp.sellComputer();
61         System.out.println(s);
62         // 修电脑
63         pp.repairComputer();
64     }
65 }

静态代理总结:

优点:可以做到在符合开闭原则的情况下对目标对象进行功能扩展

缺点:我们得为每一个服务都得创建代理类,工作量太大,不易管理。同时接口一旦发生改变,代理类也得相应修改

 

 

3、动态代理模式

 

posted @ 2019-01-26 18:20  woz333333  阅读(273)  评论(0编辑  收藏  举报