Design Mode C ++ implementation (11)-decoration Mode

Source: Internet
Author: User

 

The design patterns in the software field provide developers with an effective way to use expert design experience. The design patterns use the important features of object-oriented programming languages: encapsulation, inheritance, and polymorphism. It may be a long process to truly comprehend the essence of the design patterns, which requires a lot of practical experience. I recently read a book on design patterns. I wrote a small example in C ++ for each pattern to help me better understand it. Refer to "big talk Design Patterns" and "design patterns: Reusable basics of object-oriented software. This article describes how to implement the decoration mode.

Decoration mode: dynamically add some additional responsibilities to an object. As for the added function, the decoration mode is more flexible than the subclass generation. Sometimes we want to add some functions to an object instead of the entire class. For example, a mobile phone allows you to add features to your mobile phone, such as adding a pendant or screen film. A flexible design method is to embed a mobile phone into another object, and the feature is added by this object. We call this embedded object as decoration. This decoration is consistent with the component interface it decorated, so it is transparent to customers who use this component. The following is a UML diagram of the decoration mode.

In this design, the decoration function of the mobile phone is independent and can be developed independently, thus simplifying the design of specific mobile phone categories. The following describes the implementation of the phone class:

// Public abstract class phone {public: Phone () {} virtual ~ Phone () {} virtual void showdecorate (){}};

Specific definitions of mobile phone categories:

// Specific mobile phone class iPhone: public phone {PRIVATE: String m_name; // mobile phone name public: iPhone (string name): m_name (name ){}~ IPhone () {} void showdecorate () {cout <m_name <"decoration" <Endl ;};// specific mobile class nokiaphone: public phone {PRIVATE: string m_name; public: nokiaphone (string name): m_name (name ){}~ Nokiaphone () {}void showdecorate () {cout <m_name <"decoration" <Endl ;}};

Decoration class implementation:

// Decoration class decoratorphone: public phone {private: Phone * m_phone; // public: decoratorphone (phone * phone): m_phone (phone) {} virtual void showdecorate () {m_phone-> showdecorate () ;}}; // specific decoration class decoratorphonea: Public decoratorphone {public: decoratorphonea (phone * phone ): decoratorphone (phone) {}void showdecorate () {decoratorphone: showdecorate (); adddecorate () ;}private: void adddecorate () {cout <"add pendant" <Endl ;}// Add decoration}; // The specific decoration class decoratorphoneb: Public decoratorphone {public: decoratorphoneb (phone * phone ): decoratorphone (phone) {}void showdecorate () {decoratorphone: showdecorate (); adddecorate () ;}private: void adddecorate () {cout <"screen film" <Endl ;}// added decoration };

Customer usage:

Int main () {Phone * iPhone = new nokiaphone ("6300"); phone * DPA = new decoratorphonea (iPhone); // decoration, add the phone * DPB = new decoratorphoneb (DPA); // decoration, screen film DPB-> showdecorate (); Delete DPA; Delete DPB; Delete iPhone; return 0 ;}

The decoration mode provides a more flexible way to add roles to objects. You can use the add and remove methods to add and delete responsibilities at runtime. The decoration mode provides an "pay-as-you-go" method.
To add responsibilities. It does not try to support all foreseeable features in a complicated And customizable class. Instead, you can define a simple class and gradually add features to it using the decoration class. Complex functions can be combined from simple components. [DP]

In the example in this article, we define two specific mobile phone classes, iPhone and nokiaphone, and add features for them through separate decoration classes to combine complex functions.
I enjoy the copyright of blog articles, reprint please indicate the source http://blog.csdn.net/wuzhekai1985

 

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.