一般有两种方式可以实现给一个类或对象增加行为:
装饰模式以对客户透明的方式动态地给一个对象附加上更多的责任,换言之,客户端并不会觉得对象在装饰前和装饰后有什么不同。装饰模式可以在不需要创造更多子类的情况下,将对象的功能加以扩展。这就是装饰模式的模式动机。
装饰模式(Decorator Pattern) :动态地给一个对象增加一些额外的职责(Responsibility),就增加对象功能来说,装饰模式比生成子类实现更为灵活。其别名也可以称为包装器(Wrapper),与适配器模式的别名相同,但它们适用于不同的场合。根据翻译的不同,装饰模式也有人称之为“油漆工模式”,它是一种对象结构型模式。
装饰模式包含如下角色:
定义一个对象接口,可以给这些对象动态地添加职责。
定义一个对象,可以给这个对象添加一些职责。
维持一个指向Component对象的指针,并定义一个与Component接口一致的接口
向组件添加职责。
Component.h
#pragma once
#include
using namespace std;
//饮料类(抽象类)接口
class IBeverages
{
public:
IBeverages();
~IBeverages();
virtual string Name() = 0;
virtual double Price() = 0;
};
Component.cpp
#include "Component.h"
IBeverages::IBeverages()
{
}
IBeverages::~IBeverages()
{
}
ConcreteComponent.h
#pragma once
#include "Component.h"
/********** 具体的饮料(咖啡)**********/
// 黑咖啡,属于混合咖啡
class BlackCoffee : public IBeverages
{
public:
BlackCoffee();
~BlackCoffee();
string Name();
double Price();
};
// 深度烘培咖啡豆
class DarkRoast : public IBeverages
{
public:
DarkRoast();
~DarkRoast();
string Name();
double Price();
};
ConcreteComponent.cpp
#include "ConcreteComponent.h"
BlackCoffee::BlackCoffee()
{
}
BlackCoffee::~BlackCoffee()
{
}
string BlackCoffee::Name()
{
return "BlackCoffee";
}
double BlackCoffee::Price()
{
return 30.50;
}
DarkRoast::DarkRoast()
{
}
DarkRoast::~DarkRoast()
{
}
string DarkRoast::Name()
{
return "DarkRoast";
}
double DarkRoast::Price()
{
return 28.50;
}
Decorator.h
#pragma once
#include "Component.h"
// 调味品
class CondimentDecorator : public IBeverages
{
public:
CondimentDecorator(IBeverages *_IBeverages);
virtual ~CondimentDecorator();
string Name();
double Price();
protected :
IBeverages *m_pIBeverages;
};
Decorator.cpp
#include "Decorator.h"
CondimentDecorator::CondimentDecorator(IBeverages *_IBeverages):m_pIBeverages(_IBeverages)
{
}
CondimentDecorator::~CondimentDecorator()
{
}
string CondimentDecorator::Name()
{
return m_pIBeverages->Name();
}
double CondimentDecorator::Price()
{
return m_pIBeverages->Price();
}
ConcreteDecoratorA.h
#pragma once
#include "Decorator.h"
/********** 具体的饮料(调味品)**********/
// 奶油
class Cream :public CondimentDecorator
{
public:
Cream(IBeverages *pIBeverages);
~Cream();
string Name();
double Price();
};
ConcreteDecoratorA.cpp
#include "ConcreteDecoratorA.h"
Cream::Cream(IBeverages *pIBeverages):CondimentDecorator(pIBeverages)
{
}
Cream::~Cream()
{
}
string Cream::Name()
{
return m_pIBeverages->Name() + "Cream";
}
double Cream::Price()
{
return m_pIBeverages->Price() + 5.0;
}
ConcreteDecoratorB.h
#pragma once
#include "Decorator.h"
/********** 具体的饮料(调味品)**********/
// 摩卡
class MoCha :public CondimentDecorator
{
public:
MoCha(IBeverages *pIBeverages);
~MoCha();
string Name();
double Price();
};
ConcreteDecoratorB.cpp
#include "ConcreteDecoratorB.h"
MoCha::MoCha(IBeverages *pIBeverages) : CondimentDecorator(pIBeverages)
{
}
MoCha::~MoCha()
{
}
string MoCha::Name()
{
return m_pIBeverages->Name() + "MoCha";
}
double MoCha::Price()
{
return m_pIBeverages->Price() + 4.0;
}
ConcreteDecoratorC.h
#pragma once
#include "Decorator.h"
/********** 具体的饮料(调味品)**********/
// 糖浆
class Syrup :public CondimentDecorator
{
public:
Syrup(IBeverages *pIBeverages);
~Syrup();
string Name();
double Price();
};
ConcreteDecoratorC.cpp
#include "ConcreteDecoratorC.h"
Syrup::Syrup(IBeverages *pIBeverages):CondimentDecorator(pIBeverages)
{
}
Syrup::~Syrup()
{
}
string Syrup::Name()
{
return m_pIBeverages->Name() + "Syrup";
}
double Syrup::Price()
{
return m_pIBeverages->Price() + 2.0;
}
main.cpp
#include "Component.h"
#include
#include "Decorator.h"
#include "ConcreteComponent.h"
#include "ConcreteDecoratorA.h"
#include "ConcreteDecoratorC.h"
#include "ConcreteDecoratorB.h"
using namespace std;
int main(int argc, char* argv[])
{
/********** 黑咖啡 **********/
IBeverages *pBlackCoffee = new BlackCoffee;
cout << pBlackCoffee->Name() <<"【黑咖啡】"<< " : " << pBlackCoffee->Price() << endl;
// 黑咖啡 + 奶油
CondimentDecorator *pCream = new Cream(pBlackCoffee);
cout << pCream->Name() << "【黑咖啡 + 奶油】"<<" : " << pCream->Price() << endl;
// 黑咖啡 + 摩卡
CondimentDecorator *pMocha = new MoCha(pBlackCoffee);
cout << pMocha->Name() << "【黑咖啡 + 摩卡】"<<" : " << pMocha->Price() << endl;
// 黑咖啡 + 糖浆
CondimentDecorator *pSyrup = new Syrup(pBlackCoffee);
cout << pSyrup->Name() << "【黑咖啡 + 糖浆】"<<" : " << pSyrup->Price() << endl;
/********** 深度烘培咖啡豆 **********/
IBeverages *pDarkRoast = new DarkRoast();
cout << pDarkRoast->Name() <<"【深度烘培咖啡豆】" <<" : " << pDarkRoast->Price() << endl;
// 深度烘培咖啡豆 + 奶油
CondimentDecorator *pCreamDR = new Cream(pDarkRoast);
cout << pCreamDR->Name() <<" 【深度烘培咖啡豆 + 奶油】" <<" : " << pCreamDR->Price() << endl;
// 深度烘培咖啡豆 + 奶油 + 摩卡
CondimentDecorator *pCreamMocha = new MoCha(pCreamDR);
cout << pCreamMocha->Name() << "【深度烘培咖啡豆 + 奶油 + 摩卡】"<<" : " << pCreamMocha->Price() << endl;
// 深度烘培咖啡豆 + 奶油 + 摩卡 + 糖浆
CondimentDecorator *pCreamMochaSyrup = new Syrup(pCreamMocha);
cout << pCreamMochaSyrup->Name() <<"【深度烘培咖啡豆 + 奶油 + 摩卡 + 糖浆】"<< " : " << pCreamMochaSyrup->Price() << endl;
delete pBlackCoffee;
delete pCream;
delete pMocha;
delete pSyrup;
delete pDarkRoast;
delete pCreamDR;
delete pCreamMocha;
delete pCreamMochaSyrup;
getchar();
}
运行结果:
BlackCoffee【黑咖啡】 : 30.5
BlackCoffeeCream【黑咖啡 + 奶油】 : 35.5
BlackCoffeeMoCha【黑咖啡 + 摩卡】 : 34.5
BlackCoffeeSyrup【黑咖啡 + 糖浆】 : 32.5
DarkRoast【深度烘培咖啡豆】 : 28.5
DarkRoastCream 【深度烘培咖啡豆 + 奶油】 : 33.5
DarkRoastCreamMoCha【深度烘培咖啡豆 + 奶油 + 摩卡】 : 37.5
DarkRoastCreamMoChaSyrup【深度烘培咖啡豆 + 奶油 + 摩卡 + 糖浆】 : 39.5
装饰模式的优点:
装饰模式的缺点:
在以下情况下可以使用装饰模式:
装饰模式的简化-需要注意的问题:
对其进行扩展。 - 如果只有一个具体构件类而没有抽象构件类,那么抽象装饰类可以作为具体构件类的直接子类。
[email protected]:Varygod/CPP_Study.git
参考链接