装饰模式实现服装搭配

#include 
using namespace std;

class CPerson
{
private:
	string m_sname;
public:
	void SetName(string name)
	{
		m_sname = name;
	}
	~CPerson()
	{

	}
	virtual void ShowAll()
	{
		cout << "Person ShowAll" << endl;
	}
};
class CDress:public CPerson
{
protected:
	CPerson *t_component;
public:
	void Decorate(CPerson *component)
	{

		t_component = component;
		cout << "t_component:" << t_component << endl;
	}
	void ShowAll()
	{
		cout << "t_component:"<ShowAll();
	}
};
class CShirts :public CDress
{
public:
	void ShowAll()
	{
		cout << "CShirts this:" << this << endl;
		cout << "Shirts!" << endl;
		CDress::ShowAll();
	}
};
class CPants :public CDress
{
public:
	void ShowAll()
	{
		cout << "this:"<SetName("Han Meimei");
	CPants *ppant = new CPants;
	CShoes *pshoes = new CShoes;
	CShirts *pshirts = new CShirts;
	ppant->Decorate(pMM);
	pshoes->Decorate(ppant);
	pshirts->Decorate(pshoes);
	pshirts->ShowAll();
	return 0;
}

执行结果:

t_component:011A5598
t_component:011AEF90
t_component:011AF738
CShirts this:011AF698
Shirts!
t_component:011AF738
Dress ShowAll
CShoes this:011AF738
Shoes!
t_component:011AEF90
Dress ShowAll
this:011AEF90
Pants!
t_component:011A5598
Dress ShowAll
Person ShowAll
请按任意键继续. . .


注意Decorate(CPerson *componet)。


你可能感兴趣的:(装饰模式实现服装搭配)