Designpatterns Study Notes: C + + language implementation---2.3 Decorator

Source: Internet
Author: User

Designpatterns Study Notes: C + + language implementation---2.3 Decorator

2016-07-22

(WWW.CNBLOGS.COM/ICMZN)

Pattern understanding

1. Decorator Mode definition
Generally can be divided into 4 characters
(1) Component abstract class: Defines the abstract behavior or characteristics of the person being adorned.
(2) The specific component class: The specific object to be decorated.
(3) Abstract Decorator class:
Abstract decorators need to inherit abstract component class to possess component same behavior and characteristics, facilitate concrete decorator inheritance, and conduct decoration in specific decorator inheritance methods.
Members in abstract decorator need to be defined as protected so that they can be accessed in subclasses.
(4) Specific decorator class: Different scenes use different specific decorator classes.
Characteristics: Absdecorator inherits Abscomponent, and the specific cdecorator inherits from Absdecorator, so these three roles have a common method of operation, but
Specific decorator in the component method, can be specific decorator decoration modification.

2. Decorator Advantages
(1) Extension is quite easy, not only abscomponent can derive a variety of "being decorated", and absdecorator can also derive specific decorator,
In general: Different concrete decorator can decorate the "concrete component" standard operation method by any method.

3. Decorator Application Scenario

4. Decorator Discussion
(1) Decorator mode is also in some cases better than using inherited polymorphism, because decorator is more flexible and inheritance is prone to problems.
For example, when inheriting polymorphic use, the problem is that the personalization interface of the subclass is called through the pointer polymorphism of the parent class, so the different personalization interfaces in the different subclasses must be added to the parent class
In this way, there is an interface that is not relevant to you in the parent class for many derived subclasses, and this situation is unavoidable. So decorator is very flexible to deal with the "component" personalization problem.
Just add the appropriate personalization interface to the specific decorator, and then complete the polymorphic invocation of the corresponding "decorator" regular interface, using the abscomponent pointer.

Program Implementation (c + +)

Component.h

1 #pragmaOnce2#include <iostream>3 using namespacestd;4 5 classcabscomponent6 {7  Public:8     Virtual voidComponentoperation () =0;9 };Ten  One //Concrete object of the decorator's class A classCcompontenta: Publiccabscomponent - { -  Public: the     voidComponentoperation ()Override -     { -cout <<"******* The concrete operation of the decorator *********"<<Endl; -     } + }; -  + //you can also extend other component classes ...

Decorator.h

1 #pragmaOnce2#include <iostream>3#include"Compontent.h"4 classCabsdecorator: Publiccabscomponent5 {6  Public:7 Cabsdecorator ()8     {9M_pcomponent =nullptr;Ten     } OneCabsdecorator (cabscomponent*pcomponent) A     { -M_pcomponent =pcomponent; -     } the~Cabsdecorator () -     { - Delete m_pcomponent; -     } +  Public: -     // +     Virtual voidComponentoperation ()Override A     { at _assert (m_pcomponent); -M_pcomponent->componentoperation (); -     } - protected: -     //This can be referenced in the subclass of abstract decorator, and the disease is decorated with specific inherited methods . -cabscomponent*m_pcomponent; in }; -  to //specific decorators, which can be decorated by different behaviors, are decorated by the behavior of the person + classCdecoratoradef: PublicCabsdecorator - { the  Public: *Cdecoratoradef (cabscomponent*pcomponent): Cabsdecorator (pcomponent) {}; $  Public:Panax Notoginseng     //Special decorative behavior, can be arbitrarily modified -     voidDospecialpre () the     { +cout <<"AAAAA Special Special behavior, can be arbitrarily modified. "<<Endl; A     } the     // +     voidComponentoperation ()Override -     { $cout <<"you can manipulate the same properties of the combined object (Special operations before) in a way that inherits"<<Endl; $ Dospecialpre (); -M_pcomponent->componentoperation (); -     } the }; - Wuyi  the //specific decorators, which can be decorated by different behaviors, are decorated by the behavior of the person - classCdecoratorbdef: PublicCabsdecorator Wu { -  Public: AboutCdecoratorbdef (cabscomponent*pcomponent): Cabsdecorator (pcomponent) {}; $  Public: -     voiddospecialend () -     { -cout <<"BBBBB Special Special behavior, can be arbitrarily modified. "<<Endl; A     } +     //Special decorative behavior, can be arbitrarily modified the     voidcomponentoperation () -     { $cout <<"you can manipulate the same properties of the combined object (Special operations after that) in a way that inherits"<<Endl; theM_pcomponent->componentoperation ();//The behavior of the adorned person of the composition, the dospecialend (); the     } the};

(1) Template application

Main.cpp

1 //Decorator.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include"DecoratorDef.h"6 7#include <iostream>8 using namespacestd;9 Ten  One int_tmain (intARGC, _tchar*argv[]) A { -cout <<"\ n Decorate componenta with a decorator ..."<<Endl; -cabsdecorator* Pdecorator =NewCdecoratoradef (NewCcompontenta ()); thePdecorator->componentoperation (); - Delete pdecorator; -  -      +cout <<"\ n Decorate the Componenta with a B decorator ..."<<Endl; -Pdecorator =NewCdecoratorbdef (NewCcompontenta ()); +Pdecorator->componentoperation (); A Delete pdecorator; at  -System"Pause"); -     return 0; -}

(2) Output display

Designpatterns Study Notes: C + + language implementation---2.3 Decorator

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.