使用装饰模式来看接口

来源:互联网 发布:appstore美区软件 编辑:程序博客网 时间:2024/04/30 03:20

装饰模式(Decorator)

意图
装饰模式

装饰模式

动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。
适用性
  • 在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。
  • 处理那些可以撤消的职责。
  • 当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。
  • 使用装饰模式我们就可以看到调用接口的地方实现了接口,
  • 实现接口的地方实现了接口的方法,然后在调用接口的地方接收到实现接口的对象然后在调用接口的类中可以直接实现接口中的方法(接口中的放法)。
  • 抽象构件角色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. }   

    4、优缺点

    优点:1)提供比继承更多的灵活性 2)使用不同的装饰组合可以创造出不同行为的组合 3)需要的类的数目减少

    缺点:1)灵活性带来比较大的出错性 2)产生更多的对象,给查错带来困难


0 0
原创粉丝点击