zoukankan      html  css  js  c++  java
  • 常见设计模式的解析和实现(C++)之九—Decorator模式

    作用:动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。

     

    UML结构图:



    抽象基类:

    1)  Component :定义一个对象接口,可以为这个接口动态地添加职责。

    2)  Decorator:维持一个指向Component的指针,并且有一个和Component一致的接口函数。

     

    接口函数:

    1)  Component::Operation:这个接口函数由Component声明,因此Component的派生类都需要实现,可以在这个接口函数的基础上给它动态添加职责。

     

    解析:

    Decorator的派生类可以为ConcreateComponent类的对象动态地添加职责,或者可以这么说:Decorator的派生类装饰ConcreateComponent类的对象。具体是这么实现的:

    首先初始化一个ConcreateComponent类的对象(被装饰者),采用这个对象去生成一个Decorator对象(装饰者),之后对Operator函数的调用则是对这个Decorator对象成员函数的多态调用。这里的实现要点是Decorator类和ConcreateComponent类都继承自Component,从而两者的接口函数是一致的;其次,Decorator维护了一个指向Component的指针,从而可以实现对Component::Operation函数的动态调用

     

    实现:

    1)  Decorator.h

    1. #ifndef DECORATOR_H  
    2. #define DECORATOR_H  
    3.   
    4. // 抽象基类,定义一个对象接口,可以为这个接口动态地添加职责  
    5. class Component  
    6. {  
    7. public:  
    8.     Component(){}  
    9.     virtual ~Component(){}  
    10.   
    11.     // 纯虚函数,由派生类实现  
    12.     virtual void Operation() = 0;  
    13. };  
    14.   
    15. // 抽象基类,维护一个指向Component对象的指针  
    16. class Decorator : public Component  
    17. {  
    18. public:  
    19.     Decorator(Component* pComponent) : m_pComponent(pComponent){}  
    20.     virtual ~Decorator();  
    21. protected:  
    22.     Component* m_pComponent;  
    23. };  
    24.   
    25.   
    26. // 派生自Component,在这里表示需要给它动态添加职责的类  
    27. class ConcreateComponent : public Component  
    28. {  
    29. public:  
    30.     ConcreateComponent(){}  
    31.     virtual ~ConcreateComponent();  
    32.       
    33.     virtual void Operation();  
    34. };  
    35.   
    36. // 派生自Decorator,这时代表为ConcreateComponent动态添加职责的类  
    37. class ConcreateDecorator : public Decorator  
    38. {  
    39. public:  
    40.     ConcreateDecorator(Component* pComponent) : Decorator(pComponent){}  
    41.     virtual ~ConcreateDecorator() {}  
    42.   
    43. private:  
    44.   
    45.     AddedBehavior();  
    46. };  
    47. #endif  

    Decorator.cpp

    1. #include "Decorator.h"  
    2. #include <iostream>  
    3.   
    4.   
    5. Decorator::~Decorator()  
    6. {  
    7.     delete m_pComponent;  
    8.     m_pComponent = NULL;  
    9. }  
    10.    
    11.   
    12.  void ConcreateComponent::Operation()  
    13.  {  
    14.      std::cout << "Operation of ConcreateComponent ";  
    15.  }  
    16.   
    17.   
    18.  void ConcreateDecorator::Operation()  
    19.  {  
    20.      m_pComponent->Operation();  
    21.      AddedBehavior();  
    22.  }  
    23. void ConcreateDecorator::AddedBehavior()  
    24. {  
    25.      std::cout << "AddedBehavior of ConcreateDecorator ";  
    26. }  
  • 相关阅读:
    Java基础——字符编码
    三:简单工厂模式
    二:代理模式(静态代理,动态代理)
    一:设计者模式六大原则和单例模式
    java源码
    JDK8新特性 -- Function接口: apply,andThen,compose
    JDK8新特性:Lambda表达式
    Java同步的三种实现方式
    spring boot注解和启动
    springboot:基础学习一 linux下后台启动springboot项目
  • 原文地址:https://www.cnblogs.com/redrainblog/p/4209731.html
Copyright © 2011-2022 走看看