Chinaunix首页 | 论坛 | 博客
  • 博客访问: 169425
  • 博文数量: 60
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 638
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-26 10:59
个人简介

喜欢coding,因为那是一件伟大的事情,是将无生命的IC赋予灵魂的过程,让我拥有了和上帝一样的成就感。(w1c2g3@163.com)

文章分类

全部博文(60)

文章存档

2017年(7)

2016年(41)

2015年(1)

2014年(4)

2013年(7)

我的朋友

分类: C/C++

2016-10-30 22:41:36

装饰者模式 动态地将责任附加到对象上。若要扩展功能,装饰着提供了比继承更有弹性的替代方案。




  1. #include <iostream>
  2. #include <string>

  3. using namespace std;


  4. struct Beverage {
  5. public:
  6.     virtual string getDescription() {
  7.         return description;
  8.     }
  9.     
  10.     virtual double cost() = 0;
  11.     
  12. protected:
  13.     string description;
  14. };


  15. struct HouseBlend : public Beverage {
  16. public:
  17.     HouseBlend() {
  18.         description = "HouseBlend";
  19.     }
  20.     
  21.     virtual double cost() {
  22.         return 2.0;
  23.     }
  24. };

  25. struct DarkRoast : public Beverage {
  26. public:
  27.     DarkRoast() {
  28.         description = "DarkRoast";
  29.     }
  30.     
  31.     virtual double cost() {
  32.         return 2.1;
  33.     }
  34. };

  35. struct Espresso : public Beverage {
  36. public:
  37.     Espresso() {
  38.         description = "Espresso";
  39.     }
  40.     
  41.     virtual double cost() {
  42.         return 2.2;
  43.     }
  44. };

  45. struct Decaf : public Beverage {
  46. public:
  47.     Decaf() {
  48.         description = "Decaf";
  49.     }
  50.     
  51.     virtual double cost() {
  52.         return 2.3;
  53.     }
  54. };


  55. struct CondimentDecorator : public Beverage {
  56. public:
  57.     virtual string getDescription() = 0;
  58. };

  59. struct Milk : public CondimentDecorator {
  60. public:
  61.     Milk(Beverage *mbeverage) {
  62.         beverage = mbeverage;
  63.     }
  64.     
  65.     virtual double cost() {
  66.         return 0.1 + beverage->cost();
  67.     }
  68.     
  69.     virtual string getDescription() {
  70.         return beverage->getDescription() + ", Milk";
  71.     }
  72.     
  73. private:
  74.     Beverage *beverage;
  75. };

  76. struct Mocha : public CondimentDecorator {
  77. public:
  78.     Mocha(Beverage *mbeverage) {
  79.         beverage = mbeverage;
  80.     }
  81.     
  82.     virtual double cost() {
  83.         return 0.2 + beverage->cost();
  84.     }
  85.     
  86.     virtual string getDescription() {
  87.         return beverage->getDescription() + ", Mocha";
  88.     }
  89. private:
  90.     Beverage *beverage;
  91. };

  92. struct Soy : public CondimentDecorator {
  93. public:
  94.     Soy(Beverage *mbeverage) {
  95.         beverage = mbeverage;
  96.     }
  97.     
  98.     virtual double cost() {
  99.         return 0.3 + beverage->cost();
  100.     }
  101.     
  102.     virtual string getDescription() {
  103.         return beverage->getDescription() + ", Soy";
  104.     }
  105. private:
  106.     Beverage *beverage;
  107. };

  108. struct Whip : public CondimentDecorator {
  109. public:
  110.     Whip(Beverage *mbeverage) {
  111.         beverage = mbeverage;
  112.     }
  113.     
  114.     virtual double cost() {
  115.         return 0.4 + beverage->cost();
  116.     }
  117.     
  118.     virtual string getDescription() {
  119.         return beverage->getDescription() + ", Whip";
  120.     }
  121. private:
  122.     Beverage *beverage;
  123. };


  124. main()
  125. {
  126.     Beverage *beverage = new HouseBlend();
  127.     
  128.     beverage = new Milk(beverage);
  129.     beverage = new Whip(beverage);
  130.     beverage = new Soy(beverage);
  131.     
  132.     cout << beverage->getDescription() << endl;
  133.     cout << beverage->cost() << endl;
  134.     
  135. }

阅读(640) | 评论(0) | 转发(0) |
0

上一篇:观察者模式

下一篇:工厂方法模式

给主人留下些什么吧!~~