@@ -16,8 +16,8 @@ using std::string;
16
16
17
17
enum Type
18
18
{
19
- SIMPLE = 0 ,
20
- GOOD
19
+ PROTOTYPE_1 = 0 ,
20
+ PROTOTYPE_2
21
21
};
22
22
23
23
/* *
@@ -31,28 +31,26 @@ enum Type
31
31
class Prototype {
32
32
33
33
protected:
34
- string bullet_name_;
35
- float speed_;
36
- float fire_power_;
37
- float direction_;
34
+ string prototype_name_;
35
+ float prototype_field_;
38
36
39
37
public:
40
38
Prototype () {}
41
- Prototype (string bullet_name, float speed, float fire_power )
42
- : bullet_name_(bullet_name), speed_(speed), fire_power_(fire_power )
39
+ Prototype (string prototype_name )
40
+ : prototype_name_(prototype_name )
43
41
{
44
42
}
45
43
virtual ~Prototype () {}
46
44
virtual Prototype *Clone () const = 0;
47
- virtual void Fire (float direction ){
48
- this ->direction_ = direction ;
49
- std::cout << " fire " << bullet_name_ <<" with direction : " << direction << std::endl;
45
+ virtual void Method (float prototype_field ){
46
+ this ->prototype_field_ = prototype_field ;
47
+ std::cout << " Call Method from " << prototype_name_ <<" with field : " << prototype_field << std::endl;
50
48
}
51
49
};
52
50
53
51
/* *
54
- * EN: Simple Bullet is a Sub-Class of Bullet and implement the Clone Method
55
- * In this example all data members of Bullet Class are in the Stack. If you
52
+ * EN: ConcretePrototype1 is a Sub-Class of Prototype and implement the Clone Method
53
+ * In this example all data members of Prototype Class are in the Stack. If you
56
54
* have pointers in your properties for ex: String* name_ ,you will need to
57
55
* implement the Copy-Constructor to make sure you have a deep copy from the
58
56
* clone method
@@ -63,16 +61,16 @@ class Prototype{
63
61
class ConcretePrototype1 : public Prototype
64
62
{
65
63
private:
66
- float damage_power_ ;
64
+ float concrete_prototype_field1_ ;
67
65
68
66
public:
69
- ConcretePrototype1 (string bullet_name , float speed, float fire_power, float damage_power )
70
- : Prototype(bullet_name, speed / 2 , fire_power ), damage_power_(damage_power )
67
+ ConcretePrototype1 (string prototype_name , float concrete_prototype_field )
68
+ : Prototype(prototype_name ), concrete_prototype_field1_(concrete_prototype_field )
71
69
{
72
70
}
73
71
74
72
/* *
75
- * EN: Notice that Clone method return a Pointer to a new Bullet replica. so, the client
73
+ * EN: Notice that Clone method return a Pointer to a new ConcretePrototype1 replica. so, the client
76
74
* (who call the clone method) has the responsability to free that memory. I you have
77
75
* smart pointer knowledge you may prefer to use unique_pointer here.
78
76
*
@@ -87,11 +85,11 @@ class ConcretePrototype1 : public Prototype
87
85
class ConcretePrototype2 : public Prototype
88
86
{
89
87
private:
90
- float damage_area_ ;
88
+ float concrete_prototype_field2_ ;
91
89
92
90
public:
93
- ConcretePrototype2 (string bullet_name , float speed, float fire_power, float damage_power, float damage_area )
94
- : Prototype(bullet_name, speed, fire_power ), damage_area_(damage_area )
91
+ ConcretePrototype2 (string prototype_name , float concrete_prototype_field )
92
+ : Prototype(prototype_name ), concrete_prototype_field2_(concrete_prototype_field )
95
93
{
96
94
}
97
95
Prototype *Clone () const override
@@ -101,8 +99,9 @@ class ConcretePrototype2 : public Prototype
101
99
};
102
100
103
101
/* *
104
- * EN: In Bullet Factory you have two protorypes of each bullet, so each time
105
- * you want to create a bullet , you can use the existing ones and clone those.
102
+ * EN: In PrototypeFactory you have two concrete prototypes, one for each concrete
103
+ * prototype class, so each time you want to create a bullet ,
104
+ * you can use the existing ones and clone those.
106
105
*
107
106
* RU:
108
107
*/
@@ -111,14 +110,14 @@ class PrototypeFactory
111
110
{
112
111
113
112
private:
114
- std::unordered_map<Type, Prototype *, std::hash<int >> bullets_ ;
113
+ std::unordered_map<Type, Prototype *, std::hash<int >> prototypes_ ;
115
114
116
115
public:
117
116
PrototypeFactory ()
118
117
{
119
118
120
- bullets_ [Type::SIMPLE ] = new ConcretePrototype1 (" Simple Bullet " , 50 . f , 75 . f , 75 .f );
121
- bullets_ [Type::GOOD ] = new ConcretePrototype2 (" Good Bullet " , 60 . f , 95 . f , 95 . f , 200 .f );
119
+ prototypes_ [Type::PROTOTYPE_1 ] = new ConcretePrototype1 (" PROTOTYPE_1 " , 50 .f );
120
+ prototypes_ [Type::PROTOTYPE_2 ] = new ConcretePrototype2 (" PROTOTYPE_2 " , 60 .f );
122
121
}
123
122
124
123
/* *
@@ -130,46 +129,46 @@ class PrototypeFactory
130
129
131
130
~PrototypeFactory ()
132
131
{
133
- delete bullets_ [Type::SIMPLE ];
134
- delete bullets_ [Type::GOOD ];
132
+ delete prototypes_ [Type::PROTOTYPE_1 ];
133
+ delete prototypes_ [Type::PROTOTYPE_2 ];
135
134
}
136
135
137
136
/* *
138
- * EN: Notice here that you just need to specify the type of the bullet you want and the method
137
+ * EN: Notice here that you just need to specify the type of the prototype you want and the method
139
138
* will create from the object with this type.
140
139
*
141
140
* RU:
142
141
*/
143
- Prototype *CreateBullet (Type Type )
142
+ Prototype *CreatePrototype (Type type )
144
143
{
145
- return bullets_[Type ]->Clone ();
144
+ return prototypes_[type ]->Clone ();
146
145
}
147
146
};
148
147
149
- void Client (PrototypeFactory &bullet_factory )
148
+ void Client (PrototypeFactory &prototype_factory )
150
149
{
151
150
152
- std::cout << " Let's create a simple bullet \n " ;
151
+ std::cout << " Let's create a Prototype 1 \n " ;
153
152
154
- Prototype *prototype = bullet_factory. CreateBullet (Type::SIMPLE );
155
- prototype->Fire (90 );
153
+ Prototype *prototype = prototype_factory. CreatePrototype (Type::PROTOTYPE_1 );
154
+ prototype->Method (90 );
156
155
delete prototype;
157
156
158
157
std::cout << " \n " ;
159
158
160
- std::cout << " Let's create a Good bullet \n " ;
159
+ std::cout << " Let's create a Prototype 2 \n " ;
161
160
162
- prototype = bullet_factory. CreateBullet (Type::GOOD );
163
- prototype->Fire (10 );
161
+ prototype = prototype_factory. CreatePrototype (Type::PROTOTYPE_2 );
162
+ prototype->Method (10 );
164
163
165
164
delete prototype;
166
165
}
167
166
168
167
int main ()
169
168
{
170
- PrototypeFactory *bullet_factory = new PrototypeFactory ();
171
- Client (*bullet_factory );
172
- delete bullet_factory ;
169
+ PrototypeFactory *prototype_factory = new PrototypeFactory ();
170
+ Client (*prototype_factory );
171
+ delete prototype_factory ;
173
172
174
173
return 0 ;
175
174
}
0 commit comments