• Decorator Pattern (装饰者模式)


    装饰者模式( Decorator Pattern )

    意图 : 动态的给一个对象添加一些额外的功能,IO这块内容体现出了装饰模式,Decorator模式相比生成子类更为灵活。

    角色 :

    1)抽象构件角色(Component)--- 定义成一个接口类型

    2)具体构件角色 (ConcreteComponent) --- 该类(被装饰者)实现了 Component 接口,

    3)装饰角色 (Decorator) --- 该类实现了 Component 接口,并持有 Component接口的引用

    4)具体装饰角色 (ConcreteDecorator) --- 该类继承了装饰类

    UML实现:

    代码实现:

    Component.java

    [java] view plain copy
    1. package com.decorator ;  
    2.   
    3. //抽象构件角色  
    4. public interface Component  
    5. {  
    6.     public void operation() ;  
    7. }  

    ConcreteComponent.java

    [java] view plain copy
    1. package com.decorator ;  
    2.   
    3. //具体构件角色  
    4. public class ConcreteComponent implements Component  
    5. {  
    6.     public void operation()  
    7.     {  
    8.         System.out.println("实现功能A") ;  
    9.     }  
    10.   
    11. }  

    Decorator.java

    [java] view plain copy
    1. package com.decorator ;  
    2.   
    3. //装饰角色,持有一个构件角色的引用  
    4. public class Decorator implements Component  
    5. {  
    6.     Component component = null ;  
    7.   
    8.     public Decorator(Component component)  
    9.     {  
    10.         this.component = component ;  
    11.     }  
    12.   
    13.     public void operation()  
    14.     {  
    15.         this.component.operation() ;  
    16.     }  
    17. }  

    ConcreteDecoratorA.java

    [java] view plain copy
    1. package com.decorator ;  
    2.   
    3. //具体装饰角色A  
    4. public class ConcreteDecoratorA extends Decorator  
    5. {  
    6.     public ConcreteDecoratorA(Component component)  
    7.     {  
    8.         super(component) ;  
    9.     }  
    10.   
    11.     public void operation()  
    12.     {  
    13.         super.operation() ;  
    14.         System.out.println("实现功能B") ;  
    15.     }  
    16.   
    17.   
    18. }  

    ConcreteDecoratorB.java

    [java] view plain copy
    1. package com.decorator ;  
    2.   
    3. //具体装饰角色B  
    4. public class ConcreteDecoratorB extends Decorator  
    5. {  
    6.     public ConcreteDecoratorB(Component component)  
    7.     {  
    8.         super(component) ;  
    9.     }  
    10.   
    11.     public void operation()  
    12.     {  
    13.         super.operation() ;  
    14.         System.out.println("实现功能C") ;  
    15.     }  
    16.   
    17.   
    18. }  

    Client.java

    [java] view plain copy
    1. package com.decorator ;  
    2.   
    3. public class Client   
    4. {  
    5.     public static void main(String[] args)   
    6.     {  
    7.         //装饰者一般不用出现在客户端 , 因它内部自己会处理  
    8.         //ConcreteComponent cc = new ConcreteComponent() ;  
    9.         //ConcreteDecoratorA cd = new ConcreteDecoratorA(cc) ;  
    10.         //ConcreteDecoratorB cd2 = new ConcreteDecoratorB(cd) ;  
    11.         //cd2.operation() ;  
    12.           
    13.         //上面的代码等价于下面的代码  
    14.         ConcreteDecoratorB cd = new ConcreteDecoratorB(new ConcreteDecoratorA(new ConcreteComponent())) ;  
    15.         cd.operation() ;  
    16.           
    17.     }  
    18. }  

    小结:

    装饰者和被装饰者拥有共同的接口;

    装饰者一般不用客户端去调用 , 因它内部自己会处理;

    可以用一个或多个装饰者去包装一个对象,具体装饰类和装饰类可以组合成多种行为;

  • 相关阅读:
    转贴"三个月内通过Adsense赚一百万美金"
    今天申请了Google Adsense
    Asp.Net Core 多样性的配置来源
    Identity第二章
    Identity第一章
    Identity第三章 Authorize原理解析
    async和await
    ASP.Net Core简介
    【学习笔记】后缀数组 SA
    题解 [NOI2009] 植物大战僵尸
  • 原文地址:https://www.cnblogs.com/hoobey/p/5294387.html
Copyright © 2020-2023  润新知