forked from Syhnn/Decorator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
151 lines (101 loc) · 2.83 KB
/
main.cpp
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
144
145
146
147
148
149
150
151
/// Includes
#include <iostream>
// Namespace
using namespace std;
/// Class declarations
// This "project" is just a simple implementation of the decorator pattern,
// so I'm not gonna bother with multiple files...
// This is our base abstract class that will define what a weapon should looks like
// Here we just put a method to get the damage the weapon will deal
class Weapon
{
public:
// We need a function to get the damage our weapon deal
virtual int damage() = 0;
// Destructor
virtual ~Weapon() {}
protected:
// An int to store the damage
int _damage;
};
// Creating an object sword that inherit of Weapon
class Sword : public Weapon
{
public:
// Constructor : initialize your sword with a base damage value
Sword(int damage = 10)
{
_damage = damage;
}
// Get the damage
virtual int damage() { return _damage; }
};
/////////////////////////////////////////////////
/// Decorator implementation : Enchant weapon ///
/////////////////////////////////////////////////
// Enchant base class
class Enchant : public Weapon
{
public:
// Constructor : take a Weapon in parameter, since our Enchant inherit from Weapon, that can also be an already Enchanted weapon
Enchant(Weapon* weapon) :
_weapon(weapon)
{}
// Free the weapon
virtual ~Enchant() { delete _weapon; }
// Just return the damage of the weapon
virtual int damage() { return _weapon->damage(); }
protected:
// Pointer to our weapon
Weapon* _weapon;
};
/// Some op magic (decorators)
// A fire enchant
class Fire : public Enchant
{
public:
Fire(Weapon* weapon) :
Enchant(weapon)
{}
// Return the damage of the weapon plus the bonus damage of the fire enchant
int damage() { return _weapon->damage() + 8; }
};
// An ice enchant
class Ice : public Enchant
{
public:
Ice(Weapon* weapon) :
Enchant(weapon)
{}
// Return the damage of the weapon plus the bonus damage of the ice enchant
int damage() { return _weapon->damage() + 5; }
};
// A lightning enchant
class Lightning : public Enchant
{
public:
Lightning(Weapon* weapon) :
Enchant(weapon)
{}
// Return the damage of the weapon with some bonus
int damage() { return _weapon->damage() * 2; }
};
/**
* Main function
*/
int main(int argc, char* argv[])
{
// Instanciate your nice sword
Weapon* ironSword = new Sword(12);
Weapon* fireSword = new Fire(new Sword(12));
Weapon* excalibur = new Lightning(new Fire(new Ice(new Lightning(new Lightning(new Ice(new Fire(new Sword(12)))))))); // cheat detected o/
// Hit something
cout << "You hit for " << ironSword->damage() << " dmg!" << endl; // You hit for 12 dmg!
cout << "You hit for " << fireSword->damage() << " dmg!" << endl; // You hit for 20 dmg!
cout << "You hit for " << excalibur->damage() << " dmg!" << endl; // You hit for 372 dmg!
// Free memory
delete ironSword;
delete fireSword;
delete excalibur;
return 0; // End
}