Big talk Design Mode C ++-decoration mode and big talk Design Mode
A woman often says that a man is fond of a new person, and only a new person smiles. The old man cries, but the Decorator is a design pattern that makes new friends and never forgets old friends, it is very suitable to become a husband in ancient times (Modern Times cannot, because only one wife can be married ). The essence of the decoration mode is that each decoration object is retained with an object decorated by it. When displaying new functions, the decoration object also calls the same function of the decorated object, using such a layer-by-layer call (decoration) to form a structure similar to a linked list, we will see more design patterns similar to the linked list structure in the next section, for example, the responsibility chain mode and the status mode.
Taking the example of dressing dishes in the decorative mode in the book "big talk design mode" as an example, let's take a look at how the decorative mode achieves a red flag at home, and the color flag is floating outside. Before leaving, to make a good impression on the girl, pick a pair of jeans, and then take a simple T-shirt, dress up and go out, in order to make the dishes match any clothes, describe this process using the decoration mode.
1. Dressing base class. There is only one function Show () to Show the clothes to wear.
class CDress{public:virtual~CDress(){}virtualvoidShow(){printf("dressed boy.\n");}};
2. Base decoration 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. Specific decorations for T-shirts and jeans
class CTShirt : publicCFinery{public:virtual~CTShirt(){}voidShow(){printf("Tshirt ");CFinery::Show();}};class CJeans : publicCFinery{public:virtual~CJeans(){}voidShow(){printf("Jeans ");CFinery::Show();}};
Note that the Show () function of the decoration class object must call the Show () function of the decoration base class while displaying the dress of this object, to display the old dress of the decorated object, you cannot forget 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;}
The decoration process shows that the oCJeans object is decorated with the shirshirt object. Therefore, when oCJeans calls the Show () function, the Show () function of the shirshirt is called, and the oCTShirt is decorated with the oCDress object, at this time, the shirshirt will call the Show () function of oCDress, which is similar to recursion and chain table. Of course, we know that recursion requires a Terminator, otherwise it will never end, therefore, the last object to be decorated, oCDress, does not have any decorative objects.
At the same time, if the girl of the dish does not like the dress style, she says she wants to change the T-shirt to a shirt. Then we only need to add a shirt decoration class CShirt and change the oCJeans decoration object to a CShirt, it can be seen that after the decoration mode is used, it is much more convenient and flexible to change clothes.
5. Application of decoration Mode
The decoration mode is suitable for adding new functions to the original functions, but the original functions need to be called before/after the new functions are called. It is especially suitable for the expansion of the functions layer by layer, at the same time, it ensures that old functions can be normally called. For example, we designed a control class for the editing box. At first, only the text editing function was available. After we added a decoration class, we could edit the color text, in this case, you only need to set the background color of the control and then call the edit text function of the old object. After a while, the boss raised new requirements and asked the edit box control to build a harmonious society and shield sensitive words. Then we added a new decoration class and added the function of building a harmonious society, call the decoration class to edit the color text. Of course, we can also make flexible changes. In order not to be fancy, we can also directly decorate the original edit box controls and discard the process of engaging in colored text in the middle.
Copyright: This article is the original author Article, if you need to reprint please go to http://blog.csdn.net/gufeng99