Big talk design mode C + + version--decoration mode

Source: Internet
Author: User

women often say that men hate, saw the new laugh, that the old people cry, but the decorative mode (Decorator) is a new friend do not forget the design of old friends, is very suitable for the ancient when the husband (modern is not, because can only marry a wife). The essence of decorative mode is that each decorative object is retained by its decorative objects, decorative objects in the display of new features will be at the same time to invoke the object is decorated with the same function function, through such a layer contains calls (that is, decoration), forming a similar list structure, the following introduction, We will also see more design patterns like linked list structures, such as responsibility chain mode and state mode.
Still take "big talk Design pattern" a book adornment pattern of side dish dress example, to see how the adornment pattern is to do the home red flag does not fall, outside flags fluttering. To go to the meeting sister, before leaving to give a good impression on her sister, a selection of jeans, and then catch a simple wind T-shirt, dress up a new out, in order to be able to match any dress, use decorative mode to describe the process.
1, clothing base class, only a function show (), show wear clothes
Class Cdress{public:virtual~cdress () {}virtualvoidshow () {printf ("dressed boy.\n");}};
2. Decorative class base class
Class Cfinery:public Cdress{public:cfinery (): M_podress (NULL) {}virtual~cfinery () {}virtualvoidshow () {if (m_poDress) {m_podress->show ();}} Voiddecorate (cdress* pocdress) {m_podress = pocdress;} Private:cdress*m_podress;};
3, T-shirts and jeans specific decorative categories
Class Ctshirt:publiccfinery{public:virtual~ctshirt () {}voidshow () {printf ("tshirt"); Cfinery::show ();}}; Class Cjeans:publiccfinery{public:virtual~cjeans () {}voidshow () {printf ("Jeans"); Cfinery::show ();}};

It is important to note that the show () function of the adornment class object, while showing the dress of this object, needs to call the show () function of the decorative base class to show the old dress of the decorated object, that is, the old friend.

4. Decoration Process
int main (int argc, char* argv[]) {cdressocdress; Ctshirtoctshirt; Cjeansocjeans;octshirt.decorate (&ocdress); ocjeans.decorate (&octshirt); Ocjeans.show (); return 0;}

As you can see from the decorating process, the Ocjeans object is adorned with the Octshirt object, so Ocjeans calls the show () function, Octshirt's show () function is called, Octshirt is decorated with ocdress objects, and Octshirt will also call Ocdress's Show () function, a bit like recursion, but also a bit like the chain list of taste, of course, we know that recursion needs to have a terminator, or endless, so the last object decorated ocdress is not any decorative objects.

At the same time, if the girl does not like the dress style, said to the side of the shirt to replace the shirt, then we just add a shirt decoration class cshirt, and then the Ocjeans decoration objects to cshirt can be, here, the use of decorative mode, change clothes are more convenient and flexible.

5. Application of decoration mode
The decoration mode is suitable for adding new functions to the original function, but the new function is called before/after the original function is called, especially for the layer-by-layer extension of the function, while keeping the old functional and normal call scenes. For example, we design an edit box of the control class, initially only edit the text function, we add a decoration class, we can edit the color of the text, then we only need to set the background color of the control, and then to call the old object editing text function. After a while, the boss asked for a new request, the edit box control to engage in a harmonious society, to block sensitive words, then we add a decoration class, to increase the construction of a harmonious social function, then to call the editing of color text decoration class. Of course, we can also flexibly change, engage in harmonious society decoration class for not fancy, can also directly decorate the original edit box control class, discard the middle of the process of color text.

Copyright NOTICE: This article for Bo Master original article, if you need to reprint please explain go to http://blog.csdn.net/gufeng99

Big talk design mode C + + version--decoration mode

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.