Skip to content

Commit 655e764

Browse files
Eder DuranEder Duran
authored andcommitted
delete unused files and add prototypes pattern
1 parent 7004f58 commit 655e764

File tree

4 files changed

+43
-170
lines changed

4 files changed

+43
-170
lines changed

src/Prototype/Conceptual/Output.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Let's create a simple bullet
2-
fire Simple bullet with direction : 90
1+
Let's create a Prototype 1
2+
Call Method from PROTOTYPE_1 with field : 90
33

4-
Let's create a Good bullet
5-
fire Good Bullet with direction :10
4+
Let's create a Prototype 2
5+
Call Method from PROTOTYPE_2 with field : 10

src/Prototype/Conceptual/main.cc

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ using std::string;
1616

1717
enum Type
1818
{
19-
SIMPLE = 0,
20-
GOOD
19+
PROTOTYPE_1 = 0,
20+
PROTOTYPE_2
2121
};
2222

2323
/**
@@ -31,28 +31,26 @@ enum Type
3131
class Prototype{
3232

3333
protected:
34-
string bullet_name_;
35-
float speed_;
36-
float fire_power_;
37-
float direction_;
34+
string prototype_name_;
35+
float prototype_field_;
3836

3937
public:
4038
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)
4341
{
4442
}
4543
virtual ~Prototype() {}
4644
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;
5048
}
5149
};
5250

5351
/**
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
5654
* have pointers in your properties for ex: String* name_ ,you will need to
5755
* implement the Copy-Constructor to make sure you have a deep copy from the
5856
* clone method
@@ -63,16 +61,16 @@ class Prototype{
6361
class ConcretePrototype1 : public Prototype
6462
{
6563
private:
66-
float damage_power_;
64+
float concrete_prototype_field1_;
6765

6866
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)
7169
{
7270
}
7371

7472
/**
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
7674
* (who call the clone method) has the responsability to free that memory. I you have
7775
* smart pointer knowledge you may prefer to use unique_pointer here.
7876
*
@@ -87,11 +85,11 @@ class ConcretePrototype1 : public Prototype
8785
class ConcretePrototype2 : public Prototype
8886
{
8987
private:
90-
float damage_area_;
88+
float concrete_prototype_field2_;
9189

9290
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)
9593
{
9694
}
9795
Prototype *Clone() const override
@@ -101,8 +99,9 @@ class ConcretePrototype2 : public Prototype
10199
};
102100

103101
/**
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.
106105
*
107106
* RU:
108107
*/
@@ -111,14 +110,14 @@ class PrototypeFactory
111110
{
112111

113112
private:
114-
std::unordered_map<Type, Prototype *, std::hash<int>> bullets_;
113+
std::unordered_map<Type, Prototype *, std::hash<int>> prototypes_;
115114

116115
public:
117116
PrototypeFactory()
118117
{
119118

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);
122121
}
123122

124123
/**
@@ -130,46 +129,46 @@ class PrototypeFactory
130129

131130
~PrototypeFactory()
132131
{
133-
delete bullets_[Type::SIMPLE];
134-
delete bullets_[Type::GOOD];
132+
delete prototypes_[Type::PROTOTYPE_1];
133+
delete prototypes_[Type::PROTOTYPE_2];
135134
}
136135

137136
/**
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
139138
* will create from the object with this type.
140139
*
141140
* RU:
142141
*/
143-
Prototype *CreateBullet(Type Type)
142+
Prototype *CreatePrototype(Type type)
144143
{
145-
return bullets_[Type]->Clone();
144+
return prototypes_[type]->Clone();
146145
}
147146
};
148147

149-
void Client(PrototypeFactory &bullet_factory)
148+
void Client(PrototypeFactory &prototype_factory)
150149
{
151150

152-
std::cout << "Let's create a simple bullet\n";
151+
std::cout << "Let's create a Prototype 1\n";
153152

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);
156155
delete prototype;
157156

158157
std::cout << "\n";
159158

160-
std::cout << "Let's create a Good bullet\n";
159+
std::cout << "Let's create a Prototype 2 \n";
161160

162-
prototype = bullet_factory.CreateBullet(Type::GOOD);
163-
prototype->Fire(10);
161+
prototype = prototype_factory.CreatePrototype(Type::PROTOTYPE_2);
162+
prototype->Method(10);
164163

165164
delete prototype;
166165
}
167166

168167
int main()
169168
{
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;
173172

174173
return 0;
175174
}

src/Singleton/Conceptual/Output.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Singleton/Conceptual/main.cc

Lines changed: 0 additions & 125 deletions
This file was deleted.

0 commit comments

Comments
 (0)