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;
}