Add some additional responsibilities to the object dynamically. In terms of functionality, decorative mode is more flexible than derived subclasses.
You can use this design pattern when you need to improve a feature of a class rather than all of the objects that the class creates.
There are four kinds of characters in the decoration mode;
1, abstract components: we need to improve the class of the parent class, is an abstract class. Some virtual functions are defined.
2, specific components: that is, some of its objects may need to be improved. Also called the decorator.
3. Decoration: It is also a subclass of an abstract component, and it also contains a declaration of an abstract component that holds a reference to the adorner. It can be an abstract class or a non-abstract class.
4, specific decoration: it is an example of decoration.
This example implements a bird, which needs to be improved as an abstract component. There are three files, Bird.h and Bird.cpp, and decorator.cpp for testing
implemented as follows;
1, Bird.h
1 /*2 header file declarations for abstract components and specific components3 */4 5 #ifndef _bird_h_6 #define_bird_h_7 8 //Abstract Components9 classbird{Ten Public: One Virtual intFly () =0; A - }; - //specific components (decorator) the classSparrow: Publicbird{ - Public: - intFly ()Override;//override means that it is a virtual function of the overridden parent class - Public: + Const intDISTANCE = -; - }; + A //The decorator also inherits the abstract component (that is, the abstract component can point to the decorator or to the decorator) at classDecorator: Publicbird{ - Public: - Decorator () {} - -Decorator (bird*bird); - in protected: -Bird *mybird;//and relies on pumping components to }; + - //Concrete Decorator the classSparrowdecorator: Publicdecorator{ * Public: $Sparrowdecorator (Bird *bird);Panax Notoginseng intFly ()Override; - Private: the intelefly (); + Public: A Const intDISTANCE = -; the + }; - $ #endifView Code
2, Bird.cpp
1#include"Bird.h"2 3 intsparrow::fly ()4 {5 returnDISTANCE;6 }7 8 9Decorator::D ecorator (Bird *Bird)Ten { OneMybird =Bird; A } - -Sparrowdecorator::sparrowdecorator (Bird *bird):D ecorator (bird) the { - } - - intsparrowdecorator::fly () + { - intDistance =0; +Distance = Mybird->fly () +elefly (); A returndistance; at } - - intsparrowdecorator::elefly () - { - returnDISTANCE; -}View Code
3, Decorator.cpp
1#include <iostream>2#include"Bird.h"3 using namespacestd;4 5 voidNeedbird (Bird *Bird)6 {7 intFlydistance = bird->fly ();8cout <<"This bird can fly"<< flydistance <<"Mile"<<Endl;9 Ten } One A - intMain () - { theBird *sparrow =NewSparrow ();//ordinary birds. -Bird *sparrowdecorator1 =Newsparrowdecorator (sparrow); -Bird *sparrowdecorator2 =NewSparrowdecorator (sparrowDecorator1); - Needbird (sparrowDecorator1); + Needbird (SPARROWDECORATOR2); - return 0; +}View Code
The most important thing is that the "concrete components" and "decoration" are the subclasses of the abstract component, which means that the object declaration of the abstract component can hold the reference of "the decorator" or "The decorator's reference", so that a "decorator" is decorated as a "decorator" and can be decorated with multiple decorations.
(iv) Decoration mode-c++ Realization