Mode definition:
The modifier mode dynamically attaches the responsibility to the object. To expand the functionality, the decorator provides an alternative solution that is more flexible than inheritance.
The decorator and the decorator have the same ultra-tired type.
You can wrap an object with one or more decorators.
Since the decorator and the decorator object have the same super tired type, you can replace it with a decorated object in any case that requires the original object (wrapped.
The decorator can delegate the actions of the decorator before or after the decorator, and add their own actions to achieve a specific purpose.
Objects can be decorated at any time, so you can use your favorite decorator to decorate objects dynamically and unlimited during running.
Mode structure:
Example:
When purchasing coffee, you can add a variety of spices, such as Steamed Milk, Soy, Mocha or covered Milk. The cafe charges different fees based on the spices you add.
Solution: We use drinks as the main body, and then use spices to decorate drinks during operation. For example, if a customer wants Moka and milk foam for deep coffee, what they need to do is: Get a dark coffee (DarkRoast) object and decorate it with a Mocha object, describe it with a milk bubble object and call cZ labels? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vc3QoKbe9t6ijrLKi0sDAtc6vzdC9q7X3wc + partition + cjxwpjxpbwcgc3jjjpq =" http://www.2cto.com/uploadfile/Collfiles/20140607/20140607090958269.jpg "alt =" \ ">
Programming implementation and execution results:
# Include
# Include
Using namespace std; class Beverage {public: Beverage (string str = "Unknow Beverage"): description (str) {} virtual string getDescription () {return description ;} virtual double cost () {return 0;} private: string description;}; class CondimentDecorator: public Beverage {public: string getDescription () {return "" ;}}; class Espresso: public Beverage {public: Espresso (): Beverage ("Espresso") {}double cost () {return 1.99 ;}; class HouseBlend: public Beverage {public: HouseBlend (): beverage ("HouseBlend Coffee") {} double cost () {return 0.89 ;}}; class Mocha: public CondimentDecorator {public: Mocha (Beverage * beve) {beverage = beve ;} string getDescription () {return beverage-> getDescription () + ", Mocha";} double cost () {return 0.20 + beverage-> cost ();} private: beverage * beverage;}; class Whip: public CondimentDecorator {public: Whip (Beverage * beve) {beverage = beve;} string getDescription () {return beverage-> getDescription () + ", Whip";} double cost () {return 0.15 + beverage-> cost ();} private: Beverage * beverage;}; int main () {Beverage * pBeverage = new Espresso (); cout <pBeverage-> getDescription () <"$" <pBeverage-> cost () <
GetDescription () <"$" <pBeverage2-> cost () <
GetDescription () <"$" <pBeverage2-> cost () <
Execution result:
Espresso $1.99
Espresso, Mocha, Mocha $2.39
Espresso, Mocha, Mocha, Whip $2.54
Press any key to continue...
In this way, you can add different ingredients to different drinks without modifying the source code, and add new ingredients and beverage types.
Application of Design Principles:
Design Principle 5: The class should be extended and the modification should be disabled. For example, in the modifier mode, our goal is to allow classes to be easily extended, so that new behaviors can be combined without modifying the existing code.
Reference: Head First design mode