Thanks to the authors of the following two articles. Let's look at the second one from the first example, which is very clear! The code is slightly changed, just for your convenience in the future, please forgive me!
1: http://blog.csdn.net/dylgsy/article/details/876323
2: http://www.cnblogs.com/bastard/archive/2012/02/02/2336150.html
Example 1: // define the basic interface class, a general method of design pattern
Class cchildcomponent
{
Public:
// Children eat
Virtual void eat () = 0;
};
Class cChild: Public cchildcomponent
{
Public:
Virtual void eat ()
{
Cout <"I have eaten. "<Endl;
}
};
// If a child wants to eat an apple before eating, add a modifier class. This class and the Child class are derived from the same parent class.
Class cdecorator: Public cchildcomponent
{
Public:
Cdecorator (cchildcomponent * childcom)
{
_ Child = childcom;
}
Virtual void eat ()
{
_ Child-> eat ();
}
Protected:
Cchildcomponent * _ child;
};
// Concrete modifier class
Class cchilddecorator: Public cdecorator
{
Public:
Cchilddecorator (cchildcomponent * childcom): cdecorator (childcom)
{
}
Virtual void eat ()
{
Cout <"I want to eat an apple first." <Endl;
Cdecorator: Eat ();
}
};
Void main ()
{
CChild child;
Cchilddecorator childdec (& Child );
Childdec. Eat ();
}
Result: I have an apple first.
I have eaten
Bytes -----------------------------------------------------------------------------------------------------------
Example 2:
# Include <iostream. h>
# Include <memory. h>
/*----------------------------------------------------------------*/
/* Class visualcomponent */
/*----------------------------------------------------------------*/
Class visualcomponent
{
Public:
Visualcomponent (){}
Virtual void draw () = 0;
};
/*----------------------------------------------------------------*/
/* Class textview */
/*----------------------------------------------------------------*/
Class textviewcontrol: Public visualcomponent
{
# Define content_len 100
Public:
Textviewcontrol (char * STR, int Len)
{
Memset (m_content, 0, content_len );
Memcpy (m_content, STR, Len );
}
Virtual void draw ()
{
Cout <"textviewcontrol draw con" <Endl;
}
PRIVATE:
Char m_content [content_len];
};
/*----------------------------------------------------------------*/
/* Class decorator */
/*----------------------------------------------------------------*/
Class decorator: Public visualcomponent
{
Public:
Decorator (visualcomponent * component)
{
M_componnet = component;
}
Virtual void draw ()
{
M_componnet-> draw ();
Cout <"I am decorator" <Endl;
}
PRIVATE:
Visualcomponent * m_componnet;
};
/*----------------------------------------------------------------*/
/* Class boarderdecorator */
/*----------------------------------------------------------------*/
Class boarderdecorator: Public decorator
{
Public:
Boarderdecorator (visualcomponent * component, int boarderwidth): decorator (Component)
{
M_boarderwidth = boarderwidth;
}
Virtual void draw ()
{
Decorator: Draw ();
Cout <"I am boarderdecorator" <Endl;
This-> drawborder ();
}
Protected:
Void drawborder ()
{
Cout <"drawborder m_boarderwidth =" <m_boarderwidth <Endl;
}
PRIVATE:
Int m_boarderwidth;
};
/*----------------------------------------------------------------*/
/* Class scrolldecorator */
/*----------------------------------------------------------------*/
Class scrolldecorator: Public decorator
{
Public:
Scrolldecorator (visualcomponent * component, int scrollheight): decorator (Component)
{
M_scrollheight = scrollheight;
}
Virtual void draw ()
{
Decorator: Draw ();
Cout <"I am scrolldecorator" <Endl;
This-> drawscroll ();
}
Protected:
Void drawscroll ()
{
Cout <"drawscroll m_scrollheight =" <m_scrollheight <Endl;
}
PRIVATE:
Int m_scrollheight;
};
--------- Main '-----------------------
Int main ()
{
Char STR [] = "textviewcontrol ";
Textviewcontrol * textview = new textviewcontrol (STR, strlen (STR ));
Scrolldecorator * scroll = new scrolldecorator (textview, 100 );
Boarderdecorator * board = new boarderdecorator (scroll, 200 );
Board-> draw ();
Return 0;
}
Result:
Textviewcontrol draw con
I am decorator
I am scrolldecorator
M_scrollheight = 100
I am decorator
I am boarderdecorator
M_boarderwidth = 200