Java设计模式学习09——装饰模式

转:http://blog.csdn.net/xu__cg/article/details/53024490


一、定义


装饰(Decorate)模式又称为包装(Wrapper)模式。装饰模式是以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。

二、职责


  • 动态的为一个对象增加新的功能。
  • 装饰模式是一种用于代替继承的技术,无须通过继承增加子类就能扩展对象的新功能。使用对象的关联关系代替继承关系,更加灵活,同时避免类型体系的快速膨胀。

三、装饰模式结构


装饰模式类图:

这里写图片描述

  • Component抽象构件角色:真实对象和装饰对象有相同的接口。这样,客户端对象就能够以与真实对象相同的方式同装饰对象交互。
  • ConcreteCompoent具体构建角色(真实对象):定义一个将要接收附加责任的类。
  • Decorator装饰角色:持有一个抽象构件的引用。装饰对象接受所有客户端的请求,并把这些请求转发给真实的对象。这样,就能在真实对象调用前后增加新的功能。
  • ConcreteDecorate具体装饰角色:负责给构件对象增加新的功能。

四、简单的案例


举一个简单的汽车例子,创造每一种功能的汽车都需要继承车的父类进行实现如下图,那么当我们需要既能路上行驶有能水上行驶的车又得继续继承父类拓展新的类。

这里写图片描述

所以每增加一种新功能的汽车都需要新增一个类,这样的话就会创建大量的类。这时候就能使用装饰模式了。

代码示例: 
抽象构件

public interface AbstractCar {
    void move();
}  

   
   
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

具体构建

public class Car implements AbstractCar{

    public void move() {
        System.out.println("路上行驶");
    }
}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

装饰角色

public class SuperCar implements AbstractCar{
    protected AbstractCar car;
    public SuperCar(AbstractCar car){
        this.car=car;
    }

    public void move() {
        car.move();
    }

}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

具体装饰 角色

/**
 * 飞行汽车  
*/   

ublic class FlyCar extends SuperCar{

    public FlyCar(AbstractCar car) {
        super(car);
    }

    public void fly() {
        System.out.println("空中行驶汽车");
    }


    @Override
    public void move() {
        super.move();
         fly();
    }

}



/**
 * 水上汽车
 */
public class WaterCar extends SuperCar{

    public WaterCar(AbstractCar car) {
        super(car);
    }

    public void swim() {
        System.out.println("水上行驶汽车");
    }
    @Override
    public void move() {
        super.move();
        swim();
    }

}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

客户端

public class Client {

    public static void main(String[] args) {
        Car car=new Car();
        car.move();

        System.out.println("------增加新功能,飞行------");
        FlyCar flyCar=new FlyCar(car);
        flyCar.move();

        System.out.println("------新增加功能,水上行驶-----");
        WaterCar waterCar=new WaterCar(car);
        waterCar.move();

        System.out.println("-----新增加两个功能,飞行与水上行驶-----");
        WaterCar waterCar2=new WaterCar(flyCar);
        waterCar2.move();

    }

}



//输出结果
路上行驶
------增加新功能,飞行------
路上行驶
空中行驶汽车
------新增加功能,水上行驶-----
路上行驶
水上行驶汽车
-----新增加两个功能,飞行与水上行驶-----
路上行驶
空中行驶汽车
水上行驶汽车
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

由此可知,使用装饰模式就不用创建大量新的类而可以拓展出具有更多功能的对象了。

四、装饰模式在Java I/O库中的应用


IO流实现细节:

  • Component抽象构件角色:io流中的InputStream,OutputStream,Reader,Writer
  • ConcreteComponent具体构件角色:io流中的FileInputStream,FileOutputStream
  • Decorate装饰角色:持有抽象构件的引用,FilterInputStream,FilterOutputStream
  • ConcreteDecorate具体装饰角色:负责给构件对象添加新的责任,BufferedInputStream,BufferedOutputStream等

五、总结


  • 装饰模式(Decorate)也叫包装模式(Wrapper)
  • 装饰模式降低系统的耦合度,可以动态的增加或删除对象的责任,并使得需要装饰的具体构建类和具体装饰类可以独立变化,以便增加新的具体构建类和具体装饰类。

六、优点


  • 扩展对象功能,比继承灵活,不会导致类个数急剧增加。
  • 可以对一个对象进行多次装饰,创造出不同行为的组合,得到功能更加强大的对象。
  • 具体构 件 类和具体装饰类可以独立变化,用户可以根据需要自己增加新的 具体构件子类和具体装饰子类。

七、缺点


  • 产生很多小对象。大量小的对象占据内存,一定程度上影响性能。
  • 装饰模式易出错,调试排查比较麻烦。

八、装饰模式与桥接模式区别


  两个模式都是为了解决过多子类对象的问题。但他们的诱因不同,桥接模式是对象自身现有机制沿着多个维度变化,是既有部分不稳定。装饰模式是为了增加新的功能。


转:http://blog.csdn.net/jason0539/article/details/22713711

另外一个好例子


[java]  view plain  copy
  1. //定义被装饰者    
  2. public interface Human {    
  3.     public void wearClothes();    
  4.     
  5.     public void walkToWhere();    
  6. }    
  7.     
  8. //定义装饰者    
  9. public abstract class Decorator implements Human {    
  10.     private Human human;    
  11.     
  12.     public Decorator(Human human) {    
  13.         this.human = human;    
  14.     }    
  15.     
  16.     public void wearClothes() {    
  17.         human.wearClothes();    
  18.     }    
  19.     
  20.     public void walkToWhere() {    
  21.         human.walkToWhere();    
  22.     }    
  23. }    
  24.     
  25. //下面定义三种装饰,这是第一个,第二个第三个功能依次细化,即装饰者的功能越来越多    
  26. public class Decorator_zero extends Decorator {    
  27.     
  28.     public Decorator_zero(Human human) {    
  29.         super(human);    
  30.     }    
  31.     
  32.     public void goHome() {    
  33.         System.out.println("进房子。。");    
  34.     }    
  35.     
  36.     public void findMap() {    
  37.         System.out.println("书房找找Map。。");    
  38.     }    
  39.     
  40.     @Override    
  41.     public void wearClothes() {    
  42.         // TODO Auto-generated method stub    
  43.         super.wearClothes();    
  44.         goHome();    
  45.     }    
  46.     
  47.     @Override    
  48.     public void walkToWhere() {    
  49.         // TODO Auto-generated method stub    
  50.         super.walkToWhere();    
  51.         findMap();    
  52.     }    
  53. }    
  54.     
  55. public class Decorator_first extends Decorator {    
  56.     
  57.     public Decorator_first(Human human) {    
  58.         super(human);    
  59.     }    
  60.     
  61.     public void goClothespress() {    
  62.         System.out.println("去衣柜找找看。。");    
  63.     }    
  64.     
  65.     public void findPlaceOnMap() {    
  66.         System.out.println("在Map上找找。。");    
  67.     }    
  68.     
  69.     @Override    
  70.     public void wearClothes() {    
  71.         // TODO Auto-generated method stub    
  72.         super.wearClothes();    
  73.         goClothespress();    
  74.     }    
  75.     
  76.     @Override    
  77.     public void walkToWhere() {    
  78.         // TODO Auto-generated method stub    
  79.         super.walkToWhere();    
  80.         findPlaceOnMap();    
  81.     }    
  82. }    
  83.     
  84. public class Decorator_two extends Decorator {    
  85.     
  86.     public Decorator_two(Human human) {    
  87.         super(human);    
  88.     }    
  89.     
  90.     public void findClothes() {    
  91.         System.out.println("找到一件D&G。。");    
  92.     }    
  93.     
  94.     public void findTheTarget() {    
  95.         System.out.println("在Map上找到神秘花园和城堡。。");    
  96.     }    
  97.     
  98.     @Override    
  99.     public void wearClothes() {    
  100.         // TODO Auto-generated method stub    
  101.         super.wearClothes();    
  102.         findClothes();    
  103.     }    
  104.     
  105.     @Override    
  106.     public void walkToWhere() {    
  107.         // TODO Auto-generated method stub    
  108.         super.walkToWhere();    
  109.         findTheTarget();    
  110.     }    
  111. }    
  112.     
  113. //定义被装饰者,被装饰者初始状态有些自己的装饰    
  114. public class Person implements Human {    
  115.     
  116.     @Override    
  117.     public void wearClothes() {    
  118.         // TODO Auto-generated method stub    
  119.         System.out.println("穿什么呢。。");    
  120.     }    
  121.     
  122.     @Override    
  123.     public void walkToWhere() {    
  124.         // TODO Auto-generated method stub    
  125.         System.out.println("去哪里呢。。");    
  126.     }    
  127. }    
  128. //测试类,看一下你就会发现,跟java的I/O操作有多么相似    
  129. public class Test {    
  130.     public static void main(String[] args) {    
  131.         Human person = new Person();    
  132.         Decorator decorator = new Decorator_two(new Decorator_first(    
  133.                 new Decorator_zero(person)));    
  134.         decorator.wearClothes();    
  135.         decorator.walkToWhere();    
  136.     }    
  137. }  //定义被装饰者    
  138. public interface Human {    
  139.     public void wearClothes();    
  140.     
  141.     public void walkToWhere();    
  142. }    
  143.     
  144. //定义装饰者    
  145. public abstract class Decorator implements Human {    
  146.     private Human human;    
  147.     
  148.     public Decorator(Human human) {    
  149.         this.human = human;    
  150.     }    
  151.     
  152.     public void wearClothes() {    
  153.         human.wearClothes();    
  154.     }    
  155.     
  156.     public void walkToWhere() {    
  157.         human.walkToWhere();    
  158.     }    
  159. }    
  160.     
  161. //下面定义三种装饰,这是第一个,第二个第三个功能依次细化,即装饰者的功能越来越多    
  162. public class Decorator_zero extends Decorator {    
  163.     
  164.     public Decorator_zero(Human human) {    
  165.         super(human);    
  166.     }    
  167.     
  168.     public void goHome() {    
  169.         System.out.println("进房子。。");    
  170.     }    
  171.     
  172.     public void findMap() {    
  173.         System.out.println("书房找找Map。。");    
  174.     }    
  175.     
  176.     @Override    
  177.     public void wearClothes() {    
  178.         // TODO Auto-generated method stub    
  179.         super.wearClothes();    
  180.         goHome();    
  181.     }    
  182.     
  183.     @Override    
  184.     public void walkToWhere() {    
  185.         // TODO Auto-generated method stub    
  186.         super.walkToWhere();    
  187.         findMap();    
  188.     }    
  189. }    
  190.     
  191. public class Decorator_first extends Decorator {    
  192.     
  193.     public Decorator_first(Human human) {    
  194.         super(human);    
  195.     }    
  196.     
  197.     public void goClothespress() {    
  198.         System.out.println("去衣柜找找看。。");    
  199.     }    
  200.     
  201.     public void findPlaceOnMap() {    
  202.         System.out.println("在Map上找找。。");    
  203.     }    
  204.     
  205.     @Override    
  206.     public void wearClothes() {    
  207.         // TODO Auto-generated method stub    
  208.         super.wearClothes();    
  209.         goClothespress();    
  210.     }    
  211.     
  212.     @Override    
  213.     public void walkToWhere() {    
  214.         // TODO Auto-generated method stub    
  215.         super.walkToWhere();    
  216.         findPlaceOnMap();    
  217.     }    
  218. }    
  219.     
  220. public class Decorator_two extends Decorator {    
  221.     
  222.     public Decorator_two(Human human) {    
  223.         super(human);    
  224.     }    
  225.     
  226.     public void findClothes() {    
  227.         System.out.println("找到一件D&G。。");    
  228.     }    
  229.     
  230.     public void findTheTarget() {    
  231.         System.out.println("在Map上找到神秘花园和城堡。。");    
  232.     }    
  233.     
  234.     @Override    
  235.     public void wearClothes() {    
  236.         // TODO Auto-generated method stub    
  237.         super.wearClothes();    
  238.         findClothes();    
  239.     }    
  240.     
  241.     @Override    
  242.     public void walkToWhere() {    
  243.         // TODO Auto-generated method stub    
  244.         super.walkToWhere();    
  245.         findTheTarget();    
  246.     }    
  247. }    
  248.     
  249. //定义被装饰者,被装饰者初始状态有些自己的装饰    
  250. public class Person implements Human {    
  251.     
  252.     @Override    
  253.     public void wearClothes() {    
  254.         // TODO Auto-generated method stub    
  255.         System.out.println("穿什么呢。。");    
  256.     }    
  257.     
  258.     @Override    
  259.     public void walkToWhere() {    
  260.         // TODO Auto-generated method stub    
  261.         System.out.println("去哪里呢。。");    
  262.     }    
  263. }    
  264. //测试类,看一下你就会发现,跟java的I/O操作有多么相似    
  265. public class Test {    
  266.     public static void main(String[] args) {    
  267.         Human person = new Person();    
  268.         Decorator decorator = new Decorator_two(new Decorator_first(    
  269.                 new Decorator_zero(person)));    
  270.         decorator.wearClothes();    
  271.         decorator.walkToWhere();    
  272.     }    
  273. }    

运行结果:

其实就是进房子找衣服,然后找地图这样一个过程,通过装饰者的三层装饰,把细节变得丰富。

关键点:
1、Decorator抽象类中,持有Human接口,方法全部委托给该接口调用,目的是交给该接口的实现类即子类进行调用。
2、Decorator抽象类的子类(具体装饰者),里面都有一个构造方法调用super(human),这一句就体现了抽象类依赖于子类实现即抽象依赖于实现的原则。因为构造里面参数都是Human接口,只要是该Human的实现类都可以传递进去,即表现出Decorator dt = new Decorator_second(new Decorator_first(new Decorator_zero(human)));这种结构的样子。所以当调用dt.wearClothes();dt.walkToWhere()的时候,又因为每个具体装饰者类中,都先调用super.wearClothes和super.walkToWhere()方法,而该super已经由构造传递并指向了具体的某一个装饰者类(这个可以根据需要调换顺序),那么调用的即为装饰类的方法,然后才调用自身的装饰方法,即表现出一种装饰、链式的类似于过滤的行为。
3、具体被装饰者类,可以定义初始的状态或者初始的自己的装饰,后面的装饰行为都在此基础上一步一步进行点缀、装饰。
4、装饰者模式的设计原则为:对扩展开放、对修改关闭,这句话体现在我如果想扩展被装饰者类的行为,无须修改装饰者抽象类,只需继承装饰者抽象类,实现额外的一些装饰或者叫行为即可对被装饰者进行包装。所以:扩展体现在继承、修改体现在子类中,而不是具体的抽象类,这充分体现了依赖倒置原则,这是自己理解的装饰者模式。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值