Catégories

Design Patterns - Decorator - Code source simple en C++

Temps de lecture : 2 minutes

Je lis toujours Head First Design Patterns. J'en suis au chapitre 3, page 79. Je m'attaque donc au Decorator en C++.

Ce que j'ai retenu

Plus tard, je vais mettre ici mes notes à propos du Design Patterns en question. Là il n'y a rien car j'ai collé le code et zou, je suis passé à autre chose.

Le code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifdef _MSC_VER
  #define _CRTDBG_MAP_ALLOC
  #include <crtdbg.h>
#endif
 
#include <iostream>
#include <string>
#include <memory>
 
using namespace std;
 
// ----------------------------------------------------------------------------
class Beverage {
protected:
  string mDescription;
 
public:
  Beverage() : mDescription("Unknown Beverage") {
  }
 
  // Don't forget the "virtual" here
  virtual string getDescription() const {
    return mDescription;
  }
 
  virtual double cost() const = 0;
 
  // See Strategy source code for comments
  virtual ~Beverage() = 0 {}
};
 
// ----------------------------------------------------------------------------
class CondimentDecorator : public Beverage {
public:
  virtual string getDescription() const = 0;
  virtual ~CondimentDecorator() = 0 {}
};
 
// ----------------------------------------------------------------------------
class Espresso : public Beverage {
public :
  Espresso() {
    // TODO : Cannot be done in initialization list since mDescription is in the base.
    mDescription = "Espresso";
  }
   
  double cost() const {
    return 1.99;
  }
};
 
// ----------------------------------------------------------------------------
class HouseBlend : public Beverage {
public:
  HouseBlend() {
    mDescription = "House Blend Coffee";
  }
   
  double cost() const {
    return .89;
  }
};
 
// ----------------------------------------------------------------------------
class Mocha : public CondimentDecorator {
 
private:
  const Beverage *mBeverage;
   
public :
  explicit Mocha(Beverage *beverage) : mBeverage(beverage) {
  }
 
  ~Mocha() {
    delete mBeverage;
  }
   
  string getDescription() const {
    return mBeverage->getDescription() + ", Mocha";
  }
 
  double cost() const {
    return .20 + mBeverage->cost();
  }
};
 
// ----------------------------------------------------------------------------
class Soy : public CondimentDecorator {
 
private:   
  const Beverage *mBeverage;
 
public:
  explicit Soy(Beverage *beverage) : mBeverage(beverage) {
  }
 
  ~Soy() {
    delete mBeverage;
  }
 
  string getDescription() const {
    return mBeverage->getDescription() + ", Soy";
  }
 
  double cost() const {
    return .50 + mBeverage->cost();
  }
};
 
// ----------------------------------------------------------------------------
static void Test(void){
   
  Espresso MyExpresso;
  cout << MyExpresso.getDescription() << " $" << MyExpresso.cost() << endl;
 
  Beverage *beverage = new Espresso;
  cout << beverage->getDescription() << " $" << beverage->cost() << endl;
  delete beverage;
 
  Beverage *OtherBeverage = new HouseBlend;
  OtherBeverage = new Soy(OtherBeverage);
  OtherBeverage = new Mocha(OtherBeverage);
  cout << OtherBeverage->getDescription() << " $" << OtherBeverage->cost() << endl;
  delete OtherBeverage;
}
 
// ----------------------------------------------------------------------------
int main(){
 
#ifdef _MSC_VER
  _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
 
  Test(); // Putting all code in Test that way, allow to check memory leaks
 
#ifdef _MSC_VER
  _CrtMemDumpAllObjectsSince(NULL); // Begins the dump from the start of program execution
  _CrtDumpMemoryLeaks();
#endif
 
  cout << endl << "Strike ENTER to exit : ";
  cin.ignore((numeric_limits<streamsize>::max)(), '\n');
}

La suite au prochain épisode...

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.