The modifier mode is a design pattern that dynamically adds new behaviors to an object. Inheritance expands existing classes to add the basic class function. The extension is completed during the compilation period, and the modification mode expands an object to achieve the purpose of modification, this modifier is completed at runtime. The following is an example program that describes a person written in C ++ and uses the decoration mode.
# Include <iostream> # include <string> using namespace STD; // componentclass person {public: Virtual void describe () = 0; virtual ~ Person () // There is an inheritance relationship, you need to use the virtual destructor {}}; // concretecomponentclass Student: public person {public: Student (const string & N ): name (n) {}// rewrite void describe () {cout <"name:" <name <Endl;} PRIVATE: string name ;}; // decoratorclass decorator: public person {public: decorator (person * P) {person = P;} void describe () {person-> describe (); // call the method of the object to be modified} PRIVATE: person * person; // Save the object to be modified}; // concretedecoratorclass decoratorage: Public decorator {public: decoratorage (person * P, int A): decorator (P), age (a) {} void describe () {decorator: Describe (); cout <"age: "<age <Endl; // modifier} PRIVATE: int age ;}; // concretedecoratorclass decoratorsex: Public decorator {public: decoratorsex (person * P, const string & S): decorator (P), sex (s) {} void describe () {decorator: Describe (); cout <"Sex:" <sex <Endl; // modifier} PRIVATE: String sex;}; int main () {student s ("Nestle"); cout <"no modifier:" <Endl; S. describe (); cout <Endl; cout <"modifier age:" <Endl; decoratorage (& S, 24); // modifier decoratorage. describe (); cout <Endl; cout <"modifier Gender:" <Endl; decoratorsex (& S, "man"); // modifier decoratorsex. describe (); cout <Endl; cout <"modify both age and gender:" <Endl; decoratorsex decoratorall (& decoratorage, "man "); // modifier decoratorall. describe (); cout <Endl; System ("pause"); Return 0 ;}
Running result:
in this example, I use people as modifier objects and derive a student non-abstract class (called concretecomponent in decoration mode) from the person abstract class (called component in decoration mode ). This class has a member function describe describing itself, but the description content is very simple, so you need to use the decoration mode to modify and expand the description content. Next, a decorator class is derived from the person class, which is the base class of other modifier classes. That is to say, the class that actually modifies the student object must inherit from the decorator class. In the decorator class, the pointer of the modified object is saved. We need to use this pointer to complete the operation of the modified object itself. In this example, I constructed two specific modifier classes (called concretedecorator in the decoration mode), one is the decoratorage class that modifies the student age, and the other is the decoratorsex class that modifies the Student gender. After performing the original operations on the object to be modified, they will perform their own modification behaviors and output the age and gender respectively to achieve the modification purpose. In User code, a student object and three modifiers are instantiated. The modifier can modify student objects at will.
The advantage of the decoration mode is that you do not need to add code to the class of the object to expand the function of an object. You only need to create one or several classes separately and use these classes to modify the object. This effectively separates the core responsibilities of the class from the decorative functions. In addition, the modification method is very flexible. In the above example, we can only modify the age or object or simultaneously to form a modifier chain.
Refer:
Chapter 2 of "big talk Design Model"
Wikipedia