码迷,mamicode.com
首页 > 其他好文 > 详细

装饰和代理的区别

时间:2016-05-05 22:38:29      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

装饰是在原有类上增加功能用的
代理是对原有类作限制用的
机制差不多,但语义上方向相反

 

————————————————————————

追美女的一种方式是从她的朋友下手,让她的死党帮忙,那就离成功没有多远了。好,我们把她的死党作为这个美女的代理,开始的时候我们当然要和代理打交道了,我们用代理模式实现以下。定义一个接口,这个借口有一个behavior()方法。

  1. 1.public interface Girl {     
  2. 2.         
  3. 3.    public void behavior();     
  4. 4.    
  5. 5.}    
1.public interface Girl {   
2.       
3.    public void behavior();   
4.  
5.}  


然后让美女类实现这个接口

  1. 1.public class NiceGirl implements Girl {     
  2. 2.    
  3. 3.    private String name;     
  4. 4.    public NiceGirl(String name){     
  5. 5.        this.name = name;     
  6. 6.    }     
  7. 7.    @Override    
  8. 8.    public void behavior() {     
  9. 9.        System.out.println(this.name+"长的非常nice");     
  10. 10.        System.out.println(this.name+"说话也非常nice");     
  11. 11.    
  12. 12.    }     
  13. 13.    
  14. 14.}    
1.public class NiceGirl implements Girl {   
2.  
3.    private String name;   
4.    public NiceGirl(String name){   
5.        this.name = name;   
6.    }   
7.    @Override  
8.    public void behavior() {   
9.        System.out.println(this.name+"长的非常nice");   
10.        System.out.println(this.name+"说话也非常nice");   
11.  
12.    }   
13.  
14.}  


 

接下来定义代理类,代理类也实现了Girl接口,不但如此,代理类还要关联它要代理的对象,所以要定义Girl类型的一个成员变量,

  1. 1.import java.util.Random;     
  2. 2.    
  3. 3.public class GirlAgent implements Girl {     
  4. 4.         
  5. 5.    private Girl girl;     
  6. 6.    
  7. 7.    public GirlAgent(Girl girl) {     
  8. 8.        super();     
  9. 9.        this.girl = girl;     
  10. 10.    }     
  11. 11.    
  12. 12.    @Override    
  13. 13.    public void behavior() {     
  14. 14.        Random rand = new Random();     
  15. 15.        if(rand.nextBoolean())     
  16. 16.        {     
  17. 17.            System.out.println("我安排你们上自习");     
  18. 18.            girl.behavior();     
  19. 19.        }     
  20. 20.        else{     
  21. 21.            System.out.println("先看你的表现,上自习以后再说");     
  22. 22.        }     
  23. 23.    }     
  24. 24.}    
1.import java.util.Random;   
2.  
3.public class GirlAgent implements Girl {   
4.       
5.    private Girl girl;   
6.  
7.    public GirlAgent(Girl girl) {   
8.        super();   
9.        this.girl = girl;   
10.    }   
11.  
12.    @Override  
13.    public void behavior() {   
14.        Random rand = new Random();   
15.        if(rand.nextBoolean())   
16.        {   
17.            System.out.println("我安排你们上自习");   
18.            girl.behavior();   
19.        }   
20.        else{   
21.            System.out.println("先看你的表现,上自习以后再说");   
22.        }   
23.    }   
24.}  


OK,代理类图中的所有的类都实现了,下面编写一个测试类

  1. 1.public class Client {     
  2. 2.    
  3. 3.    public static void main(String[] args) {     
  4. 4.             
  5. 5.      Girl niceGirl = new NiceGirl("小美");     
  6. 6.    
  7. 7.      Girl friend = new GirlAgent(niceGirl);     
  8. 8.           
  9. 9.      friend.behavior();     
  10. 10.    }     
  11. 11.    
  12. 12.}    
1.public class Client {   
2.  
3.    public static void main(String[] args) {   
4.           
5.      Girl niceGirl = new NiceGirl("小美");   
6.  
7.      Girl friend = new GirlAgent(niceGirl);   
8.         
9.      friend.behavior();   
10.    }   
11.  
12.}  


哈哈,代理模式就学会了吧。那装饰模式是怎么回事呢?装饰模式只要改动一处代码就可以了,对代理类的behavior()方法改动如下,其他的类不用动。

    1.      @Override    
    2. 2.       public void behavior() {     
    3. 3.    
    4. 4.System.out.println("我家MM不但知书达礼,而且还会做饭");     
    5. 5.girl.behavior();     
    6. 6.    
    7. 7.        }    

 

装饰和代理的区别

标签:

原文地址:http://www.cnblogs.com/skyball/p/5463464.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!