设计模式总篇-装饰模式

/*
@作者:莫忘输赢
@时间:
2020/2/10 13:25
@版本:v1

@装饰模式
@作用
动态的给对象添加一些额外的职责。
@优点
装饰模式就是把附加的功能放到单独的类中,并让这个类包含它要装饰的对象,当需要执行时,客户端就可以
有选择的,按顺序的使用装饰功能
@缺点


@待完善:
之后利用引用计数进行删除对象
*/ 

#include <iostream>
#include <string>

//#include<vld.h>

class Person
{
private:
	std::string m_strName;
public:
	Person(std::string name)
	{
		m_strName = name;
	}
	Person(){
	}
	virtual void Show()
	{
		std::cout << "装扮的是" << m_strName<<"," ;
	}
};

//装饰类
class Finery : public Person
{
protected:
	Person* m_component;
public:

	void Decorate(Person* component)
	{
		m_component = component;
	}
	virtual void Show()
	{
		m_component->Show();
	}
};

class Colothes : public Finery
{
public:
	virtual void Show()
	{
		m_component->Show();
		std::cout << "穿上了美丽的衣服."<<std::endl;
	}
};

int main(int argc, char** argv)
{
	Person *p = new Person("小李");
	Colothes *co = new Colothes();

	co->Decorate(p);
	co->Show();
	if (co != nullptr)
	{
		delete co;
		co = nullptr;

	}
	if (p != nullptr)
	{
		delete p;
		p = nullptr;
	}

	return 0;
}
发布了141 篇原创文章 · 获赞 1 · 访问量 5326

猜你喜欢

转载自blog.csdn.net/wjl18270365476/article/details/104409778