开闭原则: 对扩展开放,对修改关闭。
使得设计具有d性可以应对改变,可以接受新的功能来应对改变需求。
不需要每个地方都遵循,而是在设计最有可能改变的地方应用开闭原则。
eg: 观察者模式,通过加入新的观察者,可以扩展主题对象,而不需向主题中添加代码。
动态地将责任附加到对象上,若要扩展功能,装饰者提供了比继承更有d性的替代方案。
装饰类继承自超类,继承是为了有正确的类型,而非继承超类的行为。
#include
#include
#include
#include
#include
//饮料抽象类
class Beverage {
public:
virtual ~Beverage() {};
virtual std::string getDescription() = 0;
virtual double cost() = 0;
protected:
std::string description;
};
//调料装饰者类
class Condimentecorator :public Beverage {
public:
virtual ~Condimentecorator() {};
virtual std::string getDescription() = 0;
};
//espresso 饮料类
class Espresso :public Beverage {
public:
Espresso() :Beverage()
{
description = "Espresso";
}
std::string getDescription() { return description; }
double cost()
{
return 1.99;
}
};
//houseblend 饮料类
class HouseBlend :public Beverage {
public:
HouseBlend() :Beverage()
{
description = "HouseBlend";
}
std::string getDescription() { return description; }
double cost()
{
return 0.89;
}
};
//DarkRoast 饮料类
class DarkRoast :public Beverage {
public:
DarkRoast() :Beverage()
{
description = "DarkRoast";
}
std::string getDescription() { return description; }
double cost()
{
return 0.99;
}
};
//Decat 饮料类
class Decat :public Beverage {
public:
Decat() :Beverage()
{
description = "Decat";
}
std::string getDescription() { return description; }
double cost()
{
return 1.05;
}
};
//Mocha调料装饰者
class Mocha :public Condimentecorator {
public:
Mocha(std::shared_ptr be) :Condimentecorator(), beverage(be) {}
std::string getDescription()
{
return (beverage->getDescription() + " Mocha");
}
double cost()
{
return 0.2 + beverage->cost();
}
public:
std::shared_ptr beverage;
};
//Soy调料装饰者
class Soy :public Condimentecorator {
public:
Soy(std::shared_ptr be):Condimentecorator(),beverage(be){}
std::string getDescription()
{
return (beverage->getDescription() + " Soy");
}
double cost()
{
return 0.15 + beverage->cost();
}
public:
std::shared_ptr beverage;
};
//Whip调料装饰者
class Whip :public Condimentecorator {
public:
Whip(std::shared_ptr be) :Condimentecorator(), beverage(be) {}
std::string getDescription()
{
return (beverage->getDescription() + " Whip");
}
double cost()
{
return 0.10 + beverage->cost();
}
public:
std::shared_ptr beverage;
};
int main()
{
std::shared_ptr beverage (new Espresso());
std::cout << beverage->getDescription()<<" costs: "<cost() << std::endl;
std::shared_ptr beverage2(new DarkRoast());
std::cout << beverage2->getDescription() << " costs: " << beverage2->cost() << std::endl;
beverage2 = std::shared_ptr(new Mocha(beverage2));
std::cout << beverage2->getDescription() << " costs: " << beverage2->cost() << std::endl;
beverage2 = std::shared_ptr(new Mocha(beverage2));
std::cout << beverage2->getDescription() << " costs: " << beverage2->cost() << std::endl;
beverage2 = std::shared_ptr(new Whip(beverage2));
std::cout << beverage2->getDescription() << " costs: " << beverage2->cost() << std::endl;
auto it = new Whip(beverage2);
std::cout << it->cost() << " " << it->getDescription() << std::endl;
std::cout << it->beverage->cost() << " " << it->beverage->getDescription() << std::endl;
/*
Beverage *beverage3 = new HouseBlend();
beverage3 = new Soy(beverage3);
beverage3 = new Mocha(beverage3);
beverage3 = new Whip(beverage3);
cout << beverage3->getDescription() << " $" << beverage3->cost() << endl;
*/
system("pause");
}
代码实现参考自:HeadFirst设计模式之装饰者模式(C++实现)_Xavier丶Zeng的博客-CSDN博客
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)