Big talk Design Mode Study Notes-decoration mode (c ++ description)

Source: Internet
Author: User

First, what is "rule mode": dynamically add some additional responsibilities to an object. In addition, the decoration mode is more flexible than generation. Big talk design model P48)


Decorative mode UML class diagram:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1931033931-0.jpg "title =" decorative .jpg "alt =" 163703357.jpg"/>

1. The Component class defines an object interface. You can dynamically add responsibilities to these objects.

2. The ConcreteComponent class defines specific objects, which are added as duties.

3. the Decorator class defines the interface of the decoration class, which inherits from the Component class and is used to add responsibilities to the object.

3. The ConcreteDecorator class is a specific object of the decoration class. These Implementation classes can add their own features or behaviors to add responsibilities to the object, which is more flexible than inheritance.


Code implementation:

// Componentclass Component {public: virtual void Operation () = 0 ;}; // ConcreteComponentclass ConcreteComponent: public Component {public: void Operation () {// actions on specific objects }}; // Decoratorclass Decorator: public Component {public: void SetComponent (Component * pc) {this-> component = pc;} void Operation () {if (component) {component-> Operation () ;}} protected: Component * component ;}; // ConcretrDecoratorAclass ConcretrDecoratorA: public Decorator {public: void Operation () {Decorator:: Operation (); addedState = ""; cout <addedState;} private: string addedState; // Add feature}; // ConcretrDecoratorBclass ConcretrDecoratorB: public Decorator {public: void Operation () {Decorator: Operation (); AddedBehavior (); // do A} private: void AddedBehavior () {// do B} // Add behavior };


Client:

int main() {    ConcreteComponent *pc = new ConcreteComponent();    ConcretrDecoratorA *pda = new ConcretrDecoratorA();    ConcretrDecoratorB * pdb = new ConcretrDecoratorB();    pda->SetComponent(pc);    pdb->SetComponent(pda);    pdb->Operation();    return 0;}


The following describes how to add auxiliary materials to a beverage:

A cup of ordinary coffee only 5 yuan, add milk bubble 1 yuan), add chocolate 2 yuan), add sugar 1.5 yuan ).....

So take the beverage class as the object interface, there are various drinks specific objects such as coffee, milk tea, milk, etc.), and various accessories such as decoration class), there are milk bubbles, chocolate and so on ....


Beverages:

// Beverage class Beverage {public: Beverage () {} Beverage (string n, double c): name (n), cost (c) {} virtual void show () {cout <name;} virtual double GetCost () {return cost;} protected: string name; double cost ;}; // Coffee beverage; class Coffee: public Beverage {public: Coffee (): Beverage ("Coffee", 5) {}}; // milk tea Beverage, implementation class MilkTea: public Beverage {public: MilkTea (): Beverage ("Milk Tea", 6 ){}};


Accessories:

// Decoration class, accessories class Decorator: public Beverage {public: void SetComponent (shared_ptr <Beverage> B) {beverage = B;} void show () {if (beverage) beverage-> show ();} double GetCost () {if (beverage) return beverage-> GetCost ();} protected: shared_ptr <Beverage> beverage ;}; // auxiliary material, milk bubble class MilkFoam: public Decorator {public: void show () {Decorator: show (); cout <"+" <"milk bubble ";} double GetCost () {return Decorator: GetCost () + 1 ;}}; // auxiliary material class, Chocolate class Chocolate: public Decorator {public: void show () {Decorator :: show (); cout <"+" <"Chocolate";} double GetCost () {return Decorator: GetCost () + 2 ;}}; // accessory class, sugar Cube class Suger: public Decorator {public: void show () {Decorator: show (); cout <"+" <"Sugar Cube";} double GetCost () {return Decorator: GetCost () + 1.5 ;}};


Client code:

Int main () {cout <"I want coffee sugar" <std: endl; shared_ptr <Beverage> Coffee (new coffee ()); shared_ptr <Decorator> suger (new Suger (); suger-> SetComponent (coffee); suger-> show (); cout <"\ n total price: "<suger-> GetCost () <std: endl; cout <" I want milk tea, milk foam, and chocolate "<std: endl; shared_ptr <Beverage> milktea (new MilkTea (); shared_ptr <Decorator> milkfoam (new MilkFoam (); shared_ptr <Decorator> chocolate (new Chocolate ()); milkfoam-> SetComponent (milktea); chocolate-> SetComponent (milkfoam); chocolate-> show (); cout <"\ n total price:" <chocolate-> GetCost () <std: endl; return 0 ;}


Result:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/193103D08-1.jpg "title =" fruit .jpg "alt =" 181215713.jpg"/>


The decoration mode can flexibly add responsibilities to objects, which is a good embodiment of the open-closed principle!

This article is from the "World of Canada" blog, please be sure to keep this source http://xuyjun.blog.51cto.com/7470650/1304165

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.