| 装饰者模式目标 把许多要实现的功能,加载在子类上,类的继承,显得很臃肿,装饰着模式是在不改变原有类文件和使用继承的情况下,通过创建一个包装对象动态地扩展一个对象的功能,相比生成子类更为灵活装饰模式的结构   
 在装饰模式中的角色有:   ●  抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。   ●  具体构件(ConcreteComponent)角色:定义一个将要接收附加责任的类。   ●  装饰(Decorator)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。   ●  具体装饰(ConcreteDecorator)角色:负责给构件对象“贴上”附加的责任源代码 抽象构件角色
 
 
		[Java] 纯文本查看 复制代码 
		
		
		
		
			
			| 1 2 3 4 5 | 
			publicinterfaceComponent {
     
     publicvoidsampleOperation();
     
 }
 |  具体构件角色
 
 
		[Java] 纯文本查看 复制代码 
		
		
		
		
			
			| 1 2 3 4 5 6 7 8 | 
			publicclassConcreteComponent implementsComponent {
     @Override
     publicvoidsampleOperation() {
         
     }
 }
 |  装饰角色
 
 
		[Java] 纯文本查看 复制代码 
		
		
		
		
			
			| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 | 
			publicclassDecorator implementsComponent{
     privateComponent component;
     
     publicDecorator(Component component){
         this.component = component;
     }
     @Override
     publicvoidsampleOperation() {
         
         component.sampleOperation();
     }
     
 }
 |  具体装饰角色
 
		[Java] 纯文本查看 复制代码 
		
		
		
		
			
			| 01 02 03 04 05 06 07 08 09 10 11 12 | 
			publicclassConcreteDecoratorA extendsDecorator {
     publicConcreteDecoratorA(Component component) {
         super(component);
     }
     
     @Override
     publicvoidsampleOperation() {
      super.sampleOperation();
         
     }
 }
 |  
 
		[Java] 纯文本查看 复制代码 
		
		
		
		
			
			| 01 02 03 04 05 06 07 08 09 10 11 12 | 
			publicclassConcreteDecoratorB extendsDecorator {
     publicConcreteDecoratorB(Component component) {
         super(component);
     }
     
     @Override
     publicvoidsampleOperation() {
       super.sampleOperation();
         
     }
 }
 |  齐天大圣的例子
 
 孙悟空有七十二般变化,他的每一种变化都给他带来一种附加的本领。他变成鱼儿时,就可以到水里游泳;他变成鸟儿时,就可以在天上飞行。   本例中,Component的角色便由鼎鼎大名的齐天大圣扮演;ConcreteComponent的角色属于大圣的本尊,就是猢狲本人;Decorator的角色由大圣的七十二变扮演。而ConcreteDecorator的角色便是鱼儿、鸟儿等七十二般变化源代码 抽象构件角色“齐天大圣”接口定义了一个move()方法,这是所有的具体构件类和装饰类必须实现的。
 
 
		[Java] 纯文本查看 复制代码 
		
		
		
		
			
			| 1 2 3 4 5 | 
			
			publicinterfaceTheGreatestSage {
     
     publicvoidmove();
 }
 |  具体构件角色“大圣本尊”猢狲类
 
 
		[Java] 纯文本查看 复制代码 
		
		
		
		
			
			| 1 2 3 4 5 6 7 8 9 | 
			publicclassMonkey implementsTheGreatestSage {
     @Override
     publicvoidmove() {
         
         System.out.println("Monkey Move");
     }
 }
 |  抽象装饰角色“七十二变”
 
 
		[AppleScript] 纯文本查看 复制代码 
		
		
		
		
			
			| 01 02 03 04 05 06 07 08 09 10 11 12 13 | 
			public classChange implements TheGreatestSage {
     private TheGreatestSage sage;
     
     public Change(TheGreatestSage sage){
         this.sage =sage;
     }
     @Override
     public void move(){
         //代码
         sage.move();
     }
 }
 |  具体装饰角色“鱼儿”
 
 
		[Java] 纯文本查看 复制代码 
		
		
		
		
			
			| 01 02 03 04 05 06 07 08 09 10 11 12 | 
			publicclassFish extendsChange {
     
     publicFish(TheGreatestSage sage) {
         super(sage);
     }
     @Override
     publicvoidmove() {
         
         System.out.println("Fish Move");
     }
 }
 |  具体装饰角色“鸟儿”
 
 
		[Java] 纯文本查看 复制代码 
		
		
		
		
			
			| 01 02 03 04 05 06 07 08 09 10 11 12 | 
			publicclassBird extendsChange {
     
     publicBird(TheGreatestSage sage) {
         super(sage);
     }
     @Override
     publicvoidmove() {
         
         System.out.println("Bird Move");
     }
 }
 |  客户端类
 
 
		[Java] 纯文本查看 复制代码 
		
		
		
		
			
			| 01 02 03 04 05 06 07 08 09 10 11 12 13 | 
			publicclassClient {
     publicstaticvoidmain(String[] args) {
         TheGreatestSage sage = newMonkey();
         
         TheGreatestSage bird = newBird(sage);
         TheGreatestSage fish = newFish(bird);
         
         
         fish.move(); 
     }
 }
 |  
 “大圣本尊”是ConcreteComponent类,而“鸟儿”、“鱼儿”是装饰类。要装饰的是“大圣本尊”,也即“猢狲”实例。   上面的例子中,系统把大圣从一只猢狲装饰成了一只鸟儿(把鸟儿的功能加到了猢狲身上),然后又把鸟儿装饰成了一条鱼儿(把鱼儿的功能加到了猢狲+鸟儿身上,得到了猢狲+鸟儿+鱼儿)。 如上图所示,大圣的变化首先将鸟儿的功能附加到了猢狲身上,然后又将鱼儿的功能附加到猢狲+鸟儿身上。设计模式在JAVA I/O库中的应用 
 装饰模式在Java语言中的最著名的应用莫过于Java I/O标准库的设计了。   由于Java I/O库需要很多性能的各种组合,如果这些性能都是用继承的方法实现的,那么每一种组合都需要一个类,这样就会造成大量性能重复的类出现。而如果采用装饰模式,那么类的数目就会大大减少,性能的重复也可以减至最少。因此装饰模式是Java I/O库的基本模式。   Java I/O库的对象结构图如下,由于Java I/O的对象众多,因此只画出InputStream的部分。 根据上图可以看出:   ●  抽象构件(Component)角色:由InputStream扮演。这是一个抽象类,为各种子类型提供统一的接口。   ●  具体构件(ConcreteComponent)角色:由ByteArrayInputStream、FileInputStream、PipedInputStream、StringBufferInputStream等类扮演。它们实现了抽象构件角色所规定的接口。   ●  抽象装饰(Decorator)角色:由FilterInputStream扮演。它实现了InputStream所规定的接口。 |