Resolving design patterns with code and UML diagrams-decorative patterns

Source: Internet
Author: User

I have read this mode twice because I don't quite understand it, but I still don't understand it.

By writing code, self-understanding is to redecorate the object. By inheriting the same base class. Instead of adding additional classes ....

Right

Through the modifier class to achieve the desired effect. The modifier class usually initializes the base class.

[Cpp]
// Decorator. cpp: defines the entry point of the console application.
//************************************** **********************************/
/* @ Filename Decorator. cpp
@ Author wallwind
@ Createtime
@ Function command mode
@ Email */
/*************************************** *********************************/
 
# Include "stdafx. h"
# Include <iostream>
 
Using namespace std;
 
Class Widget
{
Public:
Widget (){}
Virtual ~ Widget (){}
 
Virtual void show () = 0;
};
 
Class TextField: public Widget
{
Public:
TextField (int ix, int iy)
: X (ix), y (iy)
{
 
}
~ TextField (){}
 
Void show ()
{
Cout <"x:" <x <endl;
Cout <"y:" <y <endl;
 
}
Private:
Int x;
Int y;
};
Class Decorator: public Widget
{
Public:
Decorator (Widget * widget)
: M_widget)
{}
Virtual ~ Decorator ()
{
Delete m_widget;
}
 
Void show ()
{
M_widget-> show ();
Cout <"Decorator: show ()" <endl;
}
Private:
Widget * m_widget;
};
 
Class BorderDecorator: public Decorator
{
Public:
BorderDecorator (Widget * widget)
: Decorator (widget)
{
 
}
Void show ()
{
Decorator: show ();
Cout <"BorderDecorator: show ()" <endl;
}
};
 
Class ScrollDecorator: public Decorator
{
Public:
ScrollDecorator (Widget * widget)
: Decorator (widget)
{
 
}
 
Void show ()
{
Decorator: show ();
Cout <"ScrollDecorator: show ()" <endl;
}
};
 
 
Int _ tmain (int argc, _ TCHAR * argv [])
{
 
Widget * twidget = new TextField (2, 3 );
Widget * sd = new ScrollDecorator (twidget );
Widget * br = new BorderDecorator (twidget );
 
Sd-> show ();
Br-> show ();
Return 0;
}

 

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.