-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlyweight.cpp
More file actions
114 lines (98 loc) · 2.71 KB
/
Flyweight.cpp
File metadata and controls
114 lines (98 loc) · 2.71 KB
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
//
// Created by osher on 13/02/2022.
//
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Character {
protected:
int _health;
public:
Character() { _health = 0; }
void setHealth(int health) {
_health = health;
}
int getHealth() const {
return _health;
}
virtual void print(int i, int j) = 0;
};
class Soldier : public Character {
public:
Soldier(int health) {
_health = health;
}
void print(int i, int j) override {
cout << "soldier=" << this << " is at [" << i << ", " << j << "] with health=" << _health << endl;
}
};
class Tank : public Character {
public:
Tank(int health) {
_health = health;
}
void print(int i, int j) override {
cout << "tank=" << this << " is at [" << i << ", " << j << "] with health=" << _health << endl;
}
};
class Flyweight {
protected:
vector<Character *> characters;
virtual Character *newCharacter(int health) = 0;
public:
Character *getCharacter(int health) {
int i;
for (i = 0; i < characters.size(); i++) {
if (characters.at(i)->getHealth() == health)
return characters.at(i);
}
characters.push_back(newCharacter(health));
return characters.at(i);
}
~Flyweight() {
for_each(characters.begin(), characters.end(), [](Character* c) {delete c;});
}
};
class SoldierFlyweight : public Flyweight {
Character *newCharacter(int health) override {
return new Soldier(health);
}
};
class TankFlyweight : public Flyweight {
Character *newCharacter(int health) override {
return new Tank(health);
}
};
int main() {
Character *table[4][4];
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
table[i][j] = nullptr;
}
}
Flyweight *soldierFlyweight = new SoldierFlyweight();
Flyweight *tankFlyweight = new TankFlyweight();
table[0][0] = soldierFlyweight->getCharacter(100);
table[1][0] = soldierFlyweight->getCharacter(100);
table[0][2] = tankFlyweight->getCharacter(50);
table[1][2] = tankFlyweight->getCharacter(50);
table[1][3] = tankFlyweight->getCharacter(50);
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (table[i][j])
table[i][j]->print(i, j);
}
}
cout << "A tank was hit: Tank [1,2] health=10" << endl;
table[1][2] = tankFlyweight->getCharacter(10);
cout << "After change:" << endl;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (table[i][j])
table[i][j]->print(i, j);
}
}
delete soldierFlyweight;
delete tankFlyweight;
}