Big talk design pattern C + + Implementation-6th chapter-Decorative Mode

Source: Internet
Author: User

First, UML diagram


Second, the concept

Decorating mode: Dynamically adds some additional responsibilities to an object, and the adornment mode is more flexible than generating subclasses for added functionality.

Third, the description

Role:

(1)Component is defined as an object that can dynamically add responsibilities to these objects.

(2)concretecomponent is defined as a specific object, it can also add some responsibilities to the object.

(3)Decorator, Decorative abstract class, inherited the component, from the outer class to extend the function of component class, but for component, there is no need to know the existence of Decorator.

(4) As for the Concretedecorator is the specific decorative objects, play a role to add responsibility to component.

When to use:

(1) need to be in the internal assembly to complete the display of the situation.

(2) similar to the builder model, but the requirements of the builder pattern must be stable, while the construction of the decoration pattern is unstable.

(3) We need to put the required functions in the correct sequence in series to control.

Advantages:

(1) The decoration function of the class is removed from the class, so that the original class can be simplified.

(2) effectively distinguish the core functions of the class from the decoration function, and can remove the repetitive decoration logic in the related class.

Iv. implementation of C + +

(1) Conponent is the Concretecomponent class: Here is the person class

#ifndef person_h#define person_h#include <string> #include <iostream>//concretecomponent is Componentclass Person{private:std::string Name;public:person () {}; Person (std::string name) {this->name=name;} virtual void Show () {std::cout<< "decorated" <<name<<std::endl;}}; #endif


(2) Decorator and Concretedecorator: Here is finery and its subclasses

#ifndef finery_h#define finery_h#include <iostream> #include "Person.h"//decorator class Finery:public person{ protected:person* component;public:void Decorator (person* component) {this->component=component;} void Show () {if (component!=null) component->show ();}};/ /Below is a series of Concretedecorator class Tshirts:public finery{public:void Show () {std::cout<< "Big T-shirts"; Finery::show ();}};/ /concretedecorator class Bigtrouser:public finery{public:void Show () {std::cout<< "Down pants"; Finery::show ();}};/ /concretedecorator class Sneakers:public finery{public:void Show () {std::cout<< "Broken Sneakers"; Finery::show ();}};/ /concretedecorator class Suit:public finery{public:void Show () {std::cout<< "Suit"; Finery::show ();}};/ /concretedecorator class Tie:public finery{public:void Show () {std::cout<< "Tie"; Finery::show ();}};/ /concretedecorator class Leathershoes:public finery{public:void Show () {std::cout<< "Leather shoes"; Finery::show ();} }; #endif


(3) Client:main

#include "Finery.h" #include <string> #include <iostream>//client void Main () {person* xc=new person ("side dish"); STD: :cout<< "First dress up:" <<std::endl; sneakers* pqx=new sneakers (); bigtrouser* kk=new Bigtrouser (); tshirts* dtx=new tshirts ();p qx->decorator (XC), Kk->decorator (PQX);d tx->decorator (KK);d tx->show (); STD: :cout<< "Second Dress up:" <<std::endl; leathershoes* px=new leathershoes (); tie* ld=new Tie (); suit* xz=new Suit ();p x->decorator (XC); Ld->decorator (px); xz->decorator (LD); Xz->show ();std::cout< < "" <<std::endl;system ("pause");


Five, the output



Big talk design pattern C + + Implementation-6th chapter-Decorative Mode

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.