zoukankan      html  css  js  c++  java
  • 使用装饰模式来看接口

    装饰模式(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)产生很多其它的对象,给查错带来困难


  • 相关阅读:
    mongodb的热备工具pbm-agent
    注释
    mongodb的启动配置查询serverCmdLineOpts
    副本集状态
    分片集群的serverStatus监控
    副本集的replSetGetStatus监控
    京东
    副本集的replSetGetConfig监控
    mysql的replace函数
    副本集的serverStatus监控
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6984183.html
Copyright © 2011-2022 走看看