The design pattern of C + + in light of decoration mode

Source: Internet
Author: User
Decorative mode: Dynamically add some additional responsibilities to an object, and the adornment mode is more flexible than the subclass to increase functionality.

Four characters in decorating mode:

Component class: Component

Specific Component class: Coneretecomponent

Adornment class: Decorator (from the outer class to extend the functionality of the component class, but for component there is no need to know Decorator exists.) )

Concrete Decoration class: Concretedecorator (function to add duties to component)

In this, the adornment pattern is realized by the people's dressing modification as an example.

Test Case:

[Code]int Main () {    //Initialize the person (human) component class component, if there is only one coneretecomponent class and no abstract component class is required, Then you can directly let decorator inherit the specific component class.    Concreteperson *CP = new Concreteperson ("Jarrett");    Initialize the specific costume class (decoration class decorate)    concretetshirts *tshirts = new Concretetshirts;    Concretetrouser *trouser = new Concretetrouser;    Concreteshoes *shoe = new Concreteshoes;    Concretesuit *suit = new Concretesuit;    Concretehat *hat = new Concretehat;    Decorate    tshirts->decorate (CP) in turn;    Trouser->decorate (tshirts);    Shoe->decorate (trouser);    Suit->decorate (Shoe);    Show Results    shoe->show ();    Std::cout << Std::endl;    Re-decorate the viewing effect    hat->decorate (Shoe);    Hat->show ();    return 0;}

Decorative Mode implementation:

[code]//Main class class concreteperson{private:std::string name;//name Public:concreteperson () {} Concreteperson (Std::strin    G N): Name (n) {}//constructed by virtual void Show () const{std::cout << name << "' s dress:"; }};//Costume Class (decoration class main Class) class Finery:public Concreteperson{protected:concreteperson *cperson;//focus is to maintain a principal class object public:void Dec    Orate (Concreteperson *cp) {CPerson = CP;    } void Show () const{if (CPerson! = NULL) cperson->show (); }};//specific Costume class Tshirtsclass Concretetshirts:public finery{public:void Show () const{finery::show ();//Call base class method Std::c  Out << "Tshirts";      Use this to decorate}};//specific costumes class Tshirtsclass Concretetrouser:public finery{public:void Show () const{finery::show ();//Call base class method  Std::cout << "Trouser";      Use this to decorate}};//specific costumes class Tshirtsclass Concreteshoes:public finery{public:void Show () const{finery::show ();//Call base class method  Std::cout << "Shoe"; Use this to decorate}};//specific costume class Tshirtsclass Concretesuit:public FinEry{public:void Show () const{finery::show ();//Call base class method Std::cout << "Suit"; Use this to decorate}};//specific costumes class Tshirtsclass Concretehat:public finery{public:void Show () const{finery::show ();//Call base class method S  Td::cout << "Hat"; Use this to decorate}};


Summary:

Decorating mode is a way to dynamically add more functionality to an existing feature.

When should you use it?

Adding code to the main class increases the complexity of the main class if the functionality added in an existing program is simply to satisfy the need for special behavior that will only be performed in certain circumstances. In this case, the decorating mode is required to place each function to be decorated in a separate class and let the class wrap the object it wants to decorate.

Because, when special behavior needs to be performed, the customer code can wrap the object in sequential order using the adornment function, as needed, at run time.

Decorating mode means that your own subject class and specific class should write how to write, I want to add special features, the use of additional classes to add functionality, this is more flexible than the generation of subclasses, do not need to modify the original principal class and the specific class of code.

This removes the adornment function from the main class and simplifies the original class. Effectively separates the core functions of the class from the decorative functions, and removes the repetitive decorative logic in the related classes.


The above is the C + + design mode of shallow knowledge of the content of decorative mode, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.