设计模式学习笔记之装饰者模式
在装饰模式中的角色有:
抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
具体构件(ConcreteComponent)角色:定义一个将要接收附加责任的类。
装饰(Decorator)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。
具体装饰(ConcreteDecorator)角色:负责给构件对象“贴上”附加的责任。

| /**  * 孙悟空的本尊,并且定义了七十二变的方法。  * 抽象构件(Component)角色  */ public abstract class Monkey {     protected String type = "本尊";     protected abstract void change();     public String getType() {         return type;     } } | 
| /**  * 齐天大圣  * 具体构件(ConcreteComponent)角色  * */ public class MonkeyKing extends Monkey{     public MonkeyKing() {         type = "齐天大圣 -> ";     }     @Override     protected void change() {         System.out.println("七十二变......");     } } | 
| /**  * 变化的孙悟空  * 装饰(Decorator)角色  * */ public abstract class ChangeMonkey extends Monkey{     @Override     protected abstract void change(); } | 
| /**  * 变化后为鱼  * 具体装饰(ConcreteDecorator)角色  * */ public class Fish extends ChangeMonkey{     private Monkey monkey;     public Fish(Monkey monkey) {         this.monkey = monkey;     }     @Override     protected void change() {         System.out.println("变化为 Fish ......");     }     public String getType() {         return monkey.getType() + "fish -> ";     } } | 
| /**  * 变化后为鸟  * 具体装饰(ConcreteDecorator)角色  * */ public class Bird extends ChangeMonkey{     private Monkey monkey;     public Bird(Monkey monkey) {         this.monkey = monkey;     }     @Override     protected void change() {         System.out.println("Bird......");     }     public String getType() {         return monkey.getType() + "bird -> ";     } } | 
| public class Client {     public static void main(String[] args) {         Monkey monkey = new MonkeyKing();         Monkey fish = new Fish(monkey);         Monkey fish2 = new Fish(fish);         Monkey bird = new Bird(fish2);         System.out.println(bird.getType());         bird.change();     } } | 
根据上图可以看出:
● 抽象构件(Component)角色:由InputStream扮演。这是一个抽象类,为各种子类型提供统一的接口。
● 具体构件(ConcreteComponent)角色:由ByteArrayInputStream、FileInputStream、PipedInputStream、StringBufferInputStream等类扮演。它们实现了抽象构件角色所规定的接口。
● 抽象装饰(Decorator)角色:由FilterInputStream扮演。它实现了InputStream所规定的接口。
● 具体装饰(ConcreteDecorator)角色:由几个类扮演,分别是BufferedInputStream、DataInputStream以及两个不常用到的类LineNumberInputStream、PushbackInputStream。
(1)装饰模式与继承关系的目的都是要扩展对象的功能,但是装饰模式可以提供比继承更多的灵活性。装饰模式允许系统动态决定“贴上”一个需要的“装饰”,或者除掉一个不需要的“装饰”。继承关系则不同,继承关系是静态的,它在系统运行前就决定了。
(2)通过使用不同的具体装饰类以及这些装饰类的排列组合,设计师可以创造出很多不同行为的组合。
由于使用装饰模式,可以比使用继承关系需要较少数目的类。使用较少的类,当然使设计比较易于进行。但是,在另一方面,使用装饰模式会产生比使用继承关系更多的对象。更多的对象会使得查错变得困难,特别是这些对象看上去都很相像。
 
                    
                
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号