装饰(Decorator)模式

装饰(Decorator)模式:也叫包装模式,Adaptor模式也可以叫包装模式,不过有本质的区别,Adaptor模式没有使原有功能增强 
Decorator模式使原有功能增强,而且不是通过子类来实现功能增强的。 

装饰模式主要特色: 

1.用来扩展特定 对象 的功能, 不是扩充某个类的功能 。 
2.不需要子类,防止由于子类而导致的复杂和混乱 
3.对于一个给定的对象,同事可能有不同的装饰对象,客户端可以通过它的需要选择合适的装饰对象 

java.io包下类大量使用到装饰模式。 

装饰模式主要角色 
1.抽象构件角色:给出一个抽象接口,以规范准备接受附加责任的对象 
2.具体构件角色:定义一个将要接收附加责任的类 
3.装饰角色: 持有一个构件对象的实例 ,并定义一个与抽象构件接口一致的接口 
4.具体装饰角色:负责给构件对象"贴上"附加的责任 


Java代码   收藏代码
  1. package com.pattern.decorator;  
  2. /** 
  3.  * 抽象构件角色 
  4.  */  
  5. public interface Component {  
  6.   
  7.     public void doSomething();  
  8. }  

Java代码   收藏代码
  1. package com.pattern.decorator;  
  2. /** 
  3.  * 具体构件角色 
  4.  */  
  5. public class ConcreteComponent implements Component {  
  6.   
  7.     public void doSomething() {  
  8.         System.out.println("功能A");  
  9.     }  
  10.   
  11. }  

Java代码   收藏代码
  1. package com.pattern.decorator;  
  2. /** 
  3.  * 装饰角色 
  4.  */  
  5. public class Decorator implements Component {  
  6.     //拥有一个抽象构件对象  
  7.     private Component component;  
  8.     public Decorator(Component component){  
  9.         this.component = component;  
  10.     }  
  11.     public void doSomething() {  
  12.         component.doSomething();  
  13.     }  
  14.   
  15. }  

Java代码   收藏代码
  1. package com.pattern.decorator;  
  2. /** 
  3.  * 具体装饰角色一 
  4.  */  
  5. public class ConcreteDecorator1 extends Decorator {  
  6.   
  7.     public ConcreteDecorator1(Component component){  
  8.         super(component);  
  9.     }  
  10.       
  11.     public void doSomething(){  
  12.         super.doSomething();  
  13.         this.doAnotherthing();  
  14.           
  15.     }  
  16.     private void doAnotherthing(){  
  17.         System.out.println("功能B");  
  18.     }  
  19. }  

Java代码   收藏代码
  1. package com.pattern.decorator;  
  2. /** 
  3.  * 具体装饰角色二 
  4.  */  
  5. public class ConcreteDecorator2 extends Decorator {  
  6.   
  7.     public ConcreteDecorator2(Component component){  
  8.         super(component);  
  9.     }  
  10.       
  11.     public void doSomething(){  
  12.         super.doSomething();  
  13.         this.doAnotherthing();  
  14.           
  15.     }  
  16.     private void doAnotherthing(){  
  17.         System.out.println("功能C");  
  18.     }  
  19. }  

Java代码   收藏代码
  1. package com.pattern.decorator;  
  2. /** 
  3.  * 客户端 
  4.  */  
  5. public class Client {  
  6.       
  7.     public static void main(String[] args){  
  8.    //扩充了component 对象的功能。  
  9.         Component component = new ConcreteDecorator2(new ConcreteDecorator1(new ConcreteComponent()));  
  10.         component.doSomething();  
  11.     }  
  12. }  


运行结果: 
功能A 
功能B 
功能C 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值