Decoration Mode:
Decorate, dynamically adds some additional responsibilities to an object. For added functionality, the adornment mode is more flexible than generating subclasses. (DP)
Adornment mode provides a more flexible way to add responsibilities to objects. It is possible to add and remove duties by adding and separating methods, with decorations at run time. Decoration mode provides a "pay-as-you-go" way to add responsibilities. Instead of trying to support all of the predictable features in a complex, customizable class, you can define a simple class and gradually add functionality to it with a decorative class. Complex functions can be combined from simple components.
Instance:
For example, there is a mobile phone that allows you to add features to your phone, such as adding pendants, screen stickers, and so on. A flexible way of designing is to embed the phone in another object, which is done by the object, and we call this embedded object decorative. This decoration is consistent with the components it decorates, so it is transparent to customers using the component. The following is a uml diagram of the decorating mode.
In this design, the function of the mobile phone decoration is independent, can be developed independently, thus simplifying the design of specific mobile phone class. The following is an implementation of the phone class:
1 //public abstract class2 classPhone3 {4 Public:5 Phone () {}6 Virtual~Phone () {}7 Virtual voidshowdecorate () {}8 };9 Ten //specific mobile phone class One classIphone: PublicPhone A { - Private: - Char* M_NAME;//Phone name the Public: -IPhone (Char*name): M_name (name) {} -~IPhone () {} - voidshowdecorate () + { -cout<<m_name<<"the decoration"<<Endl; + } A }; at - //specific mobile phone class - classNokiaphone: PublicPhone - { - Private: - Char*M_name; in Public: -Nokiaphone (Char*name): M_name (name) {} to~Nokiaphone () {} + voidshowdecorate () - { thecout<<m_name<<"the decoration"<<Endl; * } $};
1 //Decoration class2 classDecoratorphone: PublicPhone3 {4 Private:5Phone *m_phone;//to decorate the phone6 Public:7Decoratorphone (Phone *phone): M_phone (phone) {}8 Virtual voidshowdecorate ()9 { TenM_phone->showdecorate (); One } A }; - - //the specific decoration class the classDecoratorphonea: PublicDecoratorphone - { - Public: -Decoratorphonea (Phone *phone): Decoratorphone (phone) {} + //This shows the order, which can be changed in order here, adddecorate (); Decoratorphone::showdecorate (); - voidshowdecorate () + { A decoratorphone::showdecorate (); at adddecorate (); - } - - Private: - voidAdddecorate () {cout<<"Add pendant"<<endl; }//Added Decoration - }; in - //the specific decoration class to classDecoratorphoneb: PublicDecoratorphone + { - Public: theDecoratorphoneb (Phone *phone): Decoratorphone (phone) {} * //This shows the order, which can be changed in order here, adddecorate (); Decoratorphone $ voidshowdecorate ()Panax Notoginseng { - decoratorphone::showdecorate (); the adddecorate (); + } A the Private: + voidAdddecorate () {cout<<"Screen Foil"<<endl; }//Added Decoration -};
1 //decorative mode. Cpp:defines the entry point for the console application.2 //3 4#include"stdafx.h"5#include <stdio. H>6#include <string. H>7#include <IOSTREAM>8 using namespacestd;9 intMainintargcChar*argv[])Ten { OnePhone *phone =NewIPhone ("IPhone");//using pointer types, parameters do not grow into new instance objects ADecoratorphone *DPA =NewDecoratorphonea (phone);//Choose the decorations that are needed to reflect selectivity -Decoratorphone *DPB =NewDecoratorphoneb (DPA);//Choose the decorations you need -Phone->showdecorate (); the //the iphone's décor -cout<<Endl; -Dpa->showdecorate (); - //the iphone's décor + //Add pendant -cout<<Endl; +Dpb->showdecorate (); A //the iphone's décor at //Add pendant - //Screen Foil - return 0; -}
Usage scenarios:
When the system needs new functionality, it is to add new code to the old class, which usually decorates the core duties or main behavior of the original class, and the problem is that new fields, new methods, and new logic are added to the main class, which increases the complexity of the main class. And these new things are just to meet the needs of some particular behavior that will only be performed in certain circumstances. The decorating mode offers a very good solution, placing each function to be decorated in a separate class and letting the class wrap the object it is decorating, so that when special behavior is required, the client code can selectively , sequentially, and optionally wrap an object with decorative features.
Advantages:
Removing the decoration function from the class, effectively separating the core functions of the class from the decoration function, and removing the repetitive decoration logic in the related class, can also simplify the original class.
Design mode-1.11 decoration mode