装饰者模式:动态地将责任附加到对象上,若要扩展功能,装饰者提供了比继承更有弹性的替代方案。
装饰者和被装饰者必须是一样的类型,也就是有共同的超类(超类我是设计成接口或者定义虚函数),利用继承达到类型匹配。而不是利用继承获得行为。
将装饰者与组件组合时,就是加入新的行为。所得的行为,不是继承自超类,而是由组合对象得来的。
利用继承设计子类的行为,是在编译时静态决定的,而且所有的子类都会继承到相同的行为。然而,如果能够利用组合的做法扩展对象的行为,就可以运行时动态的扩展。
通过动态的组合对象,可以写新的代码添加新功能,而无须修改现有代码。也就是开放-关闭原则。对扩展开放,对修改关闭。
-----------------------------C++星巴滋咖啡实现——————————————————————————————————————
#include<iostream>
#include<string>
using namespace std;
class Beverage
{
public:
string describtion;
virtual string getDescribition()//抽象主键,如果不设计成virtural,并不能动态的改变
{
return describtion;
}
virtual double cost()=0;
virtual ~Beverage(){}
};
class CondimentDecorator:public Beverage //装饰组件抽象类
{
public:
virtual string getDescribition()=0;//装饰者与被装饰者必须有一样的类型,利用继承达到类型匹配
};
class Expresso :public Beverage//具体组件
{
public:
Expresso()
{
describtion="Expresso";
}
double cost()
{
return 1.99;
}
};
class DarkRost :public Beverage
{
public:
DarkRost()
{
describtion="DarkRost";
}
double cost()
{
return 0.99;
}
};
class HouseBlend:public Beverage
{
public:
HouseBlend()
{
describtion="HouseBlend";
}
double cost()
{
return 0.89;
}
};
class Mocha:public CondimentDecorator//具体装饰者组件
{
private:
Beverage *beverage;
public:
Mocha(Beverage *beverage)
{
this->beverage=beverage;
}
string getDescribition()
{
return beverage->getDescribition() + " ,Mocha";
}
double cost()
{
return 0.20+beverage->cost();
}
};
class Whip:public CondimentDecorator
{
private:
Beverage *beverage;
string addstr;
public:
Whip(Beverage *beverage)
{
this->beverage=beverage;
}
string getDescribition()
{
return beverage->getDescribition()+" ,Whip";
}
double cost()
{
return 0.1+beverage->cost();
}
};
class Soy:public CondimentDecorator
{
private:
Beverage *beverage;
public:
Soy(Beverage *beverage)
{
this->beverage=beverage;
}
string getDescribition()
{
return beverage->getDescribition()+" ,Soy";
}
double cost()
{
return 0.15+beverage->cost();
}
};
int main()
{
Beverage *beverage=new Expresso();
cout<<beverage->getDescribition()<<" $"<<beverage->cost()<<endl;
Beverage *beverage1=new DarkRost();
beverage1=new Mocha(beverage1);
beverage1=new Mocha(beverage1);
beverage1=new Whip(beverage1);
cout<<beverage1->getDescribition()<<" $"<<beverage1->cost()<<endl;
delete beverage;
delete beverage1;
return 0;
}