装饰者模式(Decorator Pattern):动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更有弹性的替代方案
注:装饰者模式的,常常造成设计中有大量的小类,在实例化组件时增加代码的复杂度
实例:连锁咖啡店订单系统,如果使用继承的方法,每种饮料,以加入的调料不同而声明不同的类,那么”类爆炸”就不幸地发生了
代码:
JAVA
- abstract class Beverage
- {
- String description = "";
- public String getDescription()
- {
- return description;
- }
- public abstract double cost();
- }
- abstract class CondimentDecorator extends Beverage
- {
- public abstract String getDescription();
- }
- class Espresso extends Beverage
- {
- public Espresso()
- {
- description = "Espresso";
- }
- public double cost()
- {
- return 1.99;
- }
- }
- class HouseBlend extends Beverage
- {
- public HouseBlend()
- {
- description = "House Blend Coffee";
- }
- public double cost()
- {
- return 0.89;
- }
- }
- class Mocha extends CondimentDecorator
- {
- Beverage beverage;
- public Mocha(Beverage beverage)
- {
- this.beverage = beverage;
- }
- public String getDescription()
- {
- return beverage.getDescription() + ", Mocha";
- }
- public double cost()
- {
- return 0.20 + beverage.cost();
- }
- }
- class Soy extends CondimentDecorator
- {
- Beverage beverage;
- public Soy(Beverage beverage)
- {
- this.beverage = beverage;
- }
- public String getDescription()
- {
- return beverage.getDescription()+", Soy";
- }
- public double cost()
- {
- return 0.15 + beverage.cost();
- }
- }
- class Whip extends CondimentDecorator
- {
- Beverage beverage;
- public Whip(Beverage beverage)
- {
- this.beverage = beverage;
- }
- public String getDescription()
- {
- return beverage.getDescription() + ", Whip";
- }
- public double cost()
- {
- return 0.10 + beverage.cost();
- }
- }
- public class StarbuzzCoffee
- {
- public static void main(String args[])
- {
- Beverage beverage = new Espresso();
- System.out.println(beverage.getDescription() + " $ " + beverage.cost());
- Beverage beverage1 = new Espresso();
- beverage1 = new Mocha(beverage1);
- beverage1 = new Mocha(beverage1);
- beverage1 = new Whip(beverage1);
- System.out.println(beverage1.getDescription()+" $ "+beverage1.cost());
- Beverage beverage2 = new HouseBlend();
- beverage2 = new Soy(beverage2);
- beverage2 = new Mocha(beverage2);
- beverage2 = new Whip(beverage2);
- System.out.println(beverage2.getDescription() + " $ " + beverage2.cost());
- }
- }
C++:
- // StarbuzzCoffee.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <string>
- #include <iostream>
- using namespace std;
- class Beverage
- {
- protected:
- string description;
- public:
- virtual string getDescription()
- {
- return description;
- }
- virtual double cost()=0;
- };
- class CondimentDecorator : public Beverage
- {
- public:
- virtual string getDescription()=0;
- };
- class Espresso : public Beverage
- {
- public:
- Espresso()
- {
- description = "Espresso";
- }
- double cost()
- {
- return 1.99;
- }
- };
- class HouseBlend : public Beverage
- {
- public:
- HouseBlend()
- {
- description = "House Blend Coffee";
- }
- double cost()
- {
- return 0.89;
- }
- };
- class Mocha :public CondimentDecorator
- {
- private:
- Beverage* beverage;
- public:
- Mocha(Beverage* beverage)
- {
- this->beverage = beverage;
- }
- string getDescription()
- {
- return beverage->getDescription() + ", Mocha";
- }
- double cost()
- {
- return 0.20 + beverage->cost();
- }
- };
- class Soy :public CondimentDecorator
- {
- private:
- Beverage* beverage;
- public:
- Soy(Beverage* beverage)
- {
- this->beverage = beverage;
- }
- string getDescription()
- {
- return beverage->getDescription()+", Soy";
- }
- double cost()
- {
- return 0.15 + beverage->cost();
- }
- };
- class Whip:public CondimentDecorator
- {
- private:
- Beverage* beverage;
- public:
- Whip(Beverage* beverage)
- {
- this->beverage = beverage;
- }
- string getDescription()
- {
- return beverage->getDescription() + ", Whip";
- }
- double cost()
- {
- return 0.10 + beverage->cost();
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- Beverage *beverage=new Espresso();
- cout<<beverage->getDescription() <<" $ " << beverage->cost()<<endl;
- Beverage *beverage1=new Espresso();
- beverage1=new Mocha(beverage1);
- beverage1=new Mocha(beverage1);
- beverage1 = new Whip(beverage1);
- cout<<beverage1->getDescription() <<" $ " << beverage1->cost()<<endl;
- Beverage* beverage2 = new HouseBlend();
- beverage2 = new Soy(beverage2);
- beverage2 = new Mocha(beverage2);
- beverage2 = new Whip(beverage2);
- cout<<beverage2->getDescription() <<" $ " << beverage2->cost()<<endl;
- return 0;
- }
结果: