装饰者模式 java代码小示例

1、定义及作用

该模式以对客户端透明的方式扩展对象的功能。

2、涉及角色

抽象构件角色:定义一个抽象接口,来规范准备附加功能的类。

具体构件角色:将要被附加功能的类,实现抽象构件角色接口。

抽象装饰者角色:持有对具体构件角色的引用并定义与抽象构件角色一致的接口。

具体装饰角色:实现抽象装饰者角色,负责为具体构件添加额外功能。

3、简单实现

抽象构件角色java 代码
  1. package decorator;   
  2. /**  
  3.  * 装饰者和原组建的共同方法接口(抽象构件角色)  
  4.  * @author mouca.he  
  5.  *  
  6.  */  
  7. public interface InterfaceComponent {   
  8.   
  9.     /**  
  10.      * 组件方法 say()  
  11.      *  
  12.      */  
  13.     public void say();   
  14. }   

具体构件角色java 代码

  1. package decorator;   
  2. /**  
  3.  * 原组件(具体构件角色)  
  4.  * @author mouca.he  
  5.  *  
  6.  */  
  7. public class Component implements InterfaceComponent{   
  8.   
  9.     public void say() {   
  10.         // TODO 自动生成方法存根   
  11.         System.out.println("Component.say():原组件的方法!");   
  12.     }   
  13.   
  14. }   

抽象装饰者角色java 代码

  1. package decorator;   
  2. /**  
  3.  * 抽象装饰者  
  4.  * @author mouca.he  
  5.  *  
  6.  */  
  7. public abstract class AbstractDecorator implements InterfaceComponent{   
  8.   
  9.     private InterfaceComponent component;   
  10.        
  11.     public AbstractDecorator(InterfaceComponent component){   
  12.         this.component = component;   
  13.     }   
  14.     /**  
  15.      * 组件方法执行前预处理方法  
  16.      *  
  17.      */  
  18.     protected void preSay(){};   
  19.        
  20.     /**  
  21.      * 组件方法执行后处理方法  
  22.      *  
  23.      */  
  24.     protected void afterSay(){};   
  25.        
  26.     public void say(){   
  27.            
  28.         preSay();   
  29.         component.say();   
  30.         afterSay();   
  31.            
  32.     };   
  33. }   

具体装饰者二java 代码

  1. package decorator;   
  2. /**  
  3.  * 装饰者二  
  4.  * @author mouca.he  
  5.  *  
  6.  */  
  7. public class DecoratorTwo extends AbstractDecorator{   
  8.   
  9.     public DecoratorTwo(InterfaceComponent component) {   
  10.         super(component);   
  11.         // TODO 自动生成构造函数存根   
  12.     }   
  13.   
  14.     /**  
  15.      * 根据需要重载模板类preSay()方法  
  16.      */  
  17.     protected void preSay(){   
  18.         System.out.println("DecoratorTwo.preSay():装饰者二的preSay()方法!");   
  19.     }   
  20.        
  21.     /**  
  22.      * 根据需要重载模板类afterSay()方法  
  23.      */  
  24.     protected void afterSay(){   
  25.         System.out.println("DecoratorTwo.afterSay():装饰者二的afterSay()方法!");   
  26.     }   
  27.   
  28. }   

装饰者一java 代码

  1. package decorator;   
  2. /**  
  3.  * 装饰者一  
  4.  * @author mouca.he  
  5.  *  
  6.  */  
  7. public class DecoratorOne extends AbstractDecorator{   
  8.   
  9.     public DecoratorOne(InterfaceComponent component) {   
  10.         super(component);   
  11.         // TODO 自动生成构造函数存根   
  12.     }   
  13.     /**  
  14.      * 根据需要重载模板类preSay()方法  
  15.      */  
  16.     protected void preSay(){   
  17.         System.out.println("DecoratorOne.preSay():装饰者一的preSay()方法!");   
  18.     }   
  19.        
  20.     /**  
  21.      * 根据需要重载模板类afterSay()方法  
  22.      */  
  23.     protected void afterSay(){   
  24.         System.out.println("DecoratorOne.afterSay():装饰者一的afterSay()方法!");   
  25.     }   
  26.     /**  
  27.      * 测试方法  
  28.      * @param args  
  29.      */  
  30.     public static void main(String[] args) {   
  31.         // TODO 自动生成方法存根   
  32.         InterfaceComponent interfaceComponent = new DecoratorTwo(new DecoratorOne(new Component()));   
  33.         interfaceComponent.say();   
  34.         /*  
  35.          * 控制台输出:  
  36.          * DecoratorTwo.preSay():装饰者二的preSay()方法!  
  37.          * DecoratorOne.preSay():装饰者一的preSay()方法!  
  38.          * Component.say():原组件的方法!  
  39.          * DecoratorOne.afterSay():装饰者一的afterSay()方法!  
  40.          * DecoratorTwo.afterSay():装饰者二的afterSay()方法!  
  41.          */  
  42.     }   
  43. }   

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值