【C++设计模式】装饰者模式

#ifndef __DECORATOR_H__
#define __DECORATOR_H__

#include <string>

//【说明】
// 动态地将责任附加到对象上。想要扩展功能, 装饰者提供有别于继承的另一种选择。

//【定义】
// 装饰者模式(Decorator):动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式比生成子类更为灵活。

//【角色】
// Component(抽象构件)角色:给出一个抽象接口,以规范要接收附加责任的对象。 
// Concrete Component(具体构件)角色:定义一个将要接收附加责任的类。 
// Decorator(装饰者)角色:持有一个Component对象的实例,并定义一个与抽象构件接口一致的接口。 
// Concrete Decorator(具体装饰)角色:负责给构件对象“贴上”附加的责任。

//【意义】
// 可以通过一种动态的方式来扩展一个对象的功能,在运行时选择不同的装饰器,从而实现不同的行为。
// 通过使用不同的具体装饰类以及这些装饰类的排列组合,可以创造出很多不同行为的组合。可以使用多个具体装饰类来装饰同一对象,得到功能更为强大的对象。
// 具体构件类与具体装饰类可以独立变化,用户可以根据需要增加新的具体构件类和具体装饰类,在使用时再对其进行组合,原有代码无须改变,符合“开闭原则”。
// 这种比继承更加灵活机动的特性,也同时意味着装饰模式比继承更加易于出错,排错也很困难,对于多次装饰的对象,调试时寻找错误可能需要逐级排查,较为烦琐。

//【示例】

class Display
{
public:
	Display(){ }

	virtual ~Display(){ }      //虚析构函数保证通过基类指针释放派生类对象

public:
	virtual int GetColumns() = 0;

	virtual int GetRows() = 0;

	virtual std::string GetRowText(int row) = 0;

	void Show();
};

//具体的被装饰物,当增加新功能时,我们不对它做任何修改
class StringDisplay : public Display
{
public:
	StringDisplay(const std::string &str) : m_str(str){ }

	~StringDisplay(){ }

public:

	virtual int GetColumns();

	virtual int GetRows();

	virtual std::string GetRowText(int row);

private:
	std::string m_str;
};

//装饰者:通过继承和委托,装饰者与被装饰物具有相同的接口
class Border : public Display
{
public:
	Border(){ }

	Border(Display * display) : m_display(display){ }

	virtual ~Border()
	{
		if (m_display)
		{
			delete m_display;
			m_display = NULL;
		}
	}

protected:
	Display * m_display;
};

//具体装饰者A
class SideBorder : public Border
{
public:
	SideBorder(){ }

	SideBorder(Display * display, const std::string & ch) : Border(display), m_ch(ch){ }

	virtual ~SideBorder(){ }

public:
	virtual int GetColumns();

	virtual int GetRows();

	virtual std::string GetRowText(int row);

private:
	std::string m_ch;
};

//具体装饰者B
class FullBorder : public Border
{
public:
	FullBorder(){ }

	FullBorder(Display * display) : Border(display){ }

	virtual ~FullBorder(){ }

public:
	virtual int GetColumns();

	virtual int GetRows();

	virtual std::string GetRowText(int row);
};

void TestDecorator();



#endif

#include "Decorator.h"

void Display::Show()
{
	for (int i=0; i<GetRows(); i++)
	{
		printf("%s \n", GetRowText(i).c_str());
	}
}

int StringDisplay::GetColumns()
{
	return m_str.size();
}

int StringDisplay::GetRows()
{
	return 1;
}

std::string StringDisplay::GetRowText(int row)
{
	return m_str.c_str();
}

int SideBorder::GetColumns()
{
	return 1 + m_display->GetColumns() + 1;
}

int SideBorder::GetRows()
{
	return m_display->GetRows();
}

std::string SideBorder::GetRowText(int row)
{
	return m_ch + m_display->GetRowText(row) + m_ch;
}

int FullBorder::GetColumns()
{
	return 1 + m_display->GetColumns() + 1;
}

int FullBorder::GetRows()
{
	return 1 + m_display->GetRows() + 1;
}

std::string FullBorder::GetRowText(int row)
{
	if (row == 0)
	{
		return "+-------------------------------+";
	}
	else if (row == m_display->GetRows() + 1)
	{
		return "+-------------------------------+";
	}
	else
	{
		return "|" + m_display->GetRowText(row) + "|";
	}
}

void TestDecorator()
{
	//被装饰物
	Display * b1 = new StringDisplay("Hello world");

	b1->Show();

	//装饰模式支持递归
	Display * b2 = new SideBorder(new FullBorder(new SideBorder(b1, "*")),"#");

	b2->Show();

	delete b2;	
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值