Home  >  Article  >  Backend Development  >  A brief introduction to decoration mode in C++ design patterns

A brief introduction to decoration mode in C++ design patterns

黄舟
黄舟Original
2017-01-18 14:48:421103browse

Decoration mode: Dynamically add some additional responsibilities to an object. In terms of adding functionality, the decoration mode is more flexible than generating subclasses.

Four roles of decoration mode:

Component class: Component

Specific component class: ConereteComponent

Decoration class: Decorator (extended from external class The function of Component class, but for Component, there is no need to know the existence of Decorator)

Specific decorative class: ConcreteDecorator (function to add responsibilities to Component)

Here, people Take dressing modification as an example to implement decoration mode.

Test case:

[code]int main(){
    //初始化person(人)组件类Component,如果只有一个ConereteComponent类而不需要抽象的Component类,那么可以直接让Decorator继承具体的Component类。
    concretePerson *cp = new concretePerson("Jarrett");

    //初始化具体服饰类(装饰类Decorate)
    concreteTshirts *Tshirts = new concreteTshirts;
    concreteTrouser *Trouser = new concreteTrouser;
    concreteShoes *Shoe = new concreteShoes;
    //concreteSuit *Suit = new concreteSuit;
    concreteHat *Hat = new concreteHat;

    //依次进行装饰
    Tshirts->decorate(cp);
    Trouser->decorate(Tshirts);
    Shoe->decorate(Trouser);
    //Suit->decorate(Shoe);
    //显示结果
    Shoe->show();
    std::cout << std::endl;
    //再装饰一次查看效果
    Hat->decorate(Shoe);
    Hat->show();

    return 0;
}

Decoration mode implementation:

[code]//主类
class concretePerson{
private:
    std::string name;//人名
public:
    concretePerson(){}
    concretePerson(std::string n):name(n){} //构造方式
    virtual void show()const{
        std::cout << name << "&#39;s dress: ";
    }
};

//服饰类(装饰类主类)
class Finery: public concretePerson{
protected:
    concretePerson *cPerson;//重点是维护一个主体类对象
public:
    void decorate(concretePerson *cp){
        cPerson = cp;
    }
    void show()const{
        if(cPerson != NULL)
            cPerson->show();
    }
};

//具体服饰类Tshirts
class concreteTshirts: public Finery{
public:
  void show()const{
      Finery::show(); //调用基类方法
      std::cout << "Tshirts ";  //用此来修饰
  }
};

//具体服饰类Tshirts
class concreteTrouser: public Finery{
public:
  void show()const{
      Finery::show(); //调用基类方法
      std::cout << "Trouser ";  //用此来修饰
  }
};

//具体服饰类Tshirts
class concreteShoes: public Finery{
public:
  void show()const{
      Finery::show(); //调用基类方法
      std::cout << "Shoe ";  //用此来修饰
  }
};

//具体服饰类Tshirts
class concreteSuit: public Finery{
public:
  void show()const{
      Finery::show(); //调用基类方法
      std::cout << "Suit ";  //用此来修饰
  }
};

//具体服饰类Tshirts
class concreteHat: public Finery{
public:
  void show()const{
      Finery::show(); //调用基类方法
      std::cout << "Hat ";  //用此来修饰
  }
};


Summary:

Decoration mode is for existing Functions are a way to dynamically add more functionality.

When should it be used?

If the functions added to an existing program are only to meet the needs of special behaviors that will only be executed under certain circumstances, adding code to the main class will increase the complexity of the main class. At this time, the decoration mode is needed to put each function to be decorated in a separate class, and let this class wrap the object it wants to decorate.

Because, when special behavior needs to be performed, the client code can selectively use decoration functions to wrap objects in order at runtime as needed.

The decoration mode means that you can write your own main class and specific class however you want. When I want to add additional special functions, I use additional classes to add functions. This is better than generating subclasses. More flexible, no need to modify the code of the original main class and concrete class.

In this way, the original class can be simplified by moving and removing the decorative functions in the class from the main class. It effectively separates the core responsibilities and decoration functions of the class, and can remove repeated decoration logic in related classes.


The above is the content of a brief introduction to the decoration mode of C++ design patterns, and more For related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn