Skip to content

Commit a0b0ea4

Browse files
committed
code refactoring: fix memory leaks, code style, etc.
1 parent a868155 commit a0b0ea4

File tree

24 files changed

+957
-563
lines changed

24 files changed

+957
-563
lines changed

Diff for: abstract-factory/AbstractFactory.cpp

+73-29
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Source code is licensed under MIT License
77
* (for more details see LICENSE)
8-
*
8+
*
99
*/
1010

1111
#include <iostream>
@@ -15,26 +15,38 @@
1515
* products implement the same interface so that the classes can refer
1616
* to the interface not the concrete product
1717
*/
18-
class ProductA {
18+
class ProductA
19+
{
1920
public:
20-
virtual std::string getName() = 0;
21+
virtual ~ProductA() {}
22+
23+
virtual const char* getName() = 0;
2124
// ...
2225
};
2326

2427
/*
2528
* ConcreteProductAX and ConcreteProductAY
2629
* define objects to be created by concrete factory
2730
*/
28-
class ConcreteProductAX : public ProductA {
31+
class ConcreteProductAX : public ProductA
32+
{
2933
public:
30-
std::string getName() {
34+
~ConcreteProductAX() {}
35+
36+
const char* getName()
37+
{
3138
return "A-X";
3239
}
3340
// ...
3441
};
3542

36-
class ConcreteProductAY : public ProductA {
37-
std::string getName() {
43+
class ConcreteProductAY : public ProductA
44+
{
45+
public:
46+
~ConcreteProductAY() {}
47+
48+
const char* getName()
49+
{
3850
return "A-Y";
3951
}
4052
// ...
@@ -45,25 +57,38 @@ class ConcreteProductAY : public ProductA {
4557
* same as Product A, Product B declares interface for concrete products
4658
* where each can produce an entire set of products
4759
*/
48-
class ProductB {
60+
class ProductB
61+
{
4962
public:
50-
virtual std::string getName() = 0;
63+
virtual ~ProductB() {}
64+
65+
virtual const char* getName() = 0;
5166
// ...
5267
};
5368

5469
/*
5570
* ConcreteProductBX and ConcreteProductBY
5671
* same as previous concrete product classes
5772
*/
58-
class ConcreteProductBX : public ProductB {
59-
std::string getName() {
73+
class ConcreteProductBX : public ProductB
74+
{
75+
public:
76+
~ConcreteProductBX() {}
77+
78+
const char* getName()
79+
{
6080
return "B-X";
6181
}
6282
// ...
6383
};
6484

65-
class ConcreteProductBY : public ProductB {
66-
std::string getName() {
85+
class ConcreteProductBY : public ProductB
86+
{
87+
public:
88+
~ConcreteProductBY() {}
89+
90+
const char* getName()
91+
{
6792
return "B-Y";
6893
}
6994
// ...
@@ -73,8 +98,11 @@ class ConcreteProductBY : public ProductB {
7398
* Abstract Factory
7499
* provides an abstract interface for creating a family of products
75100
*/
76-
class AbstractFactory {
101+
class AbstractFactory
102+
{
77103
public:
104+
virtual ~AbstractFactory() {}
105+
78106
virtual ProductA *createProductA() = 0;
79107
virtual ProductB *createProductB() = 0;
80108
};
@@ -84,23 +112,33 @@ class AbstractFactory {
84112
* each concrete factory create a family of products and client uses
85113
* one of these factories so it never has to instantiate a product object
86114
*/
87-
class ConcreteFactoryX : public AbstractFactory {
115+
class ConcreteFactoryX : public AbstractFactory
116+
{
88117
public:
89-
ProductA *createProductA() {
118+
~ConcreteFactoryX() {}
119+
120+
ProductA *createProductA()
121+
{
90122
return new ConcreteProductAX();
91123
}
92-
ProductB *createProductB() {
124+
ProductB *createProductB()
125+
{
93126
return new ConcreteProductBX();
94127
}
95128
// ...
96129
};
97130

98-
class ConcreteFactoryY : public AbstractFactory {
131+
class ConcreteFactoryY : public AbstractFactory
132+
{
99133
public:
100-
ProductA *createProductA() {
134+
~ConcreteFactoryY() {}
135+
136+
ProductA *createProductA()
137+
{
101138
return new ConcreteProductAY();
102139
}
103-
ProductB *createProductB() {
140+
ProductB *createProductB()
141+
{
104142
return new ConcreteProductBY();
105143
}
106144
// ...
@@ -109,14 +147,20 @@ class ConcreteFactoryY : public AbstractFactory {
109147

110148
int main()
111149
{
112-
ConcreteFactoryX *factoryX = new ConcreteFactoryX();
113-
ConcreteFactoryY *factoryY = new ConcreteFactoryY();
114-
115-
ProductA *p1 = factoryX->createProductA();
116-
std::cout << "Product: " << p1->getName() << std::endl;
150+
ConcreteFactoryX *factoryX = new ConcreteFactoryX();
151+
ConcreteFactoryY *factoryY = new ConcreteFactoryY();
117152

118-
ProductA *p2 = factoryY->createProductA();
119-
std::cout << "Product: " << p2->getName() << std::endl;
120-
121-
return 0;
153+
ProductA *p1 = factoryX->createProductA();
154+
std::cout << "Product: " << p1->getName() << std::endl;
155+
156+
ProductA *p2 = factoryY->createProductA();
157+
std::cout << "Product: " << p2->getName() << std::endl;
158+
159+
delete p1;
160+
delete p2;
161+
162+
delete factoryX;
163+
delete factoryY;
164+
165+
return 0;
122166
}

Diff for: adapter/ClassAdapter.cpp

+16-8
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
* Target
1515
* defines specific interface that Client uses
1616
*/
17-
class Target {
17+
class Target
18+
{
1819
public:
20+
virtual ~Target() {}
21+
1922
virtual void request() = 0;
2023
// ...
2124
};
@@ -25,11 +28,14 @@ class Target {
2528
* all requests get delegated to the Adaptee which defines
2629
* an existing interface that needs adapting
2730
*/
28-
class Adaptee {
31+
class Adaptee
32+
{
2933
public:
30-
void specificRequest() {
34+
~Adaptee() {}
35+
36+
void specificRequest()
37+
{
3138
std::cout << "specific request" << std::endl;
32-
// ...
3339
}
3440
// ...
3541
};
@@ -40,11 +46,12 @@ class Adaptee {
4046
* to request on a Target by extending both classes
4147
* ie adapts the interface of Adaptee to the Target interface
4248
*/
43-
class Adapter : public Target, private Adaptee {
49+
class Adapter : public Target, private Adaptee
50+
{
4451
public:
45-
virtual void request() {
52+
virtual void request()
53+
{
4654
specificRequest();
47-
// ...
4855
}
4956
// ...
5057
};
@@ -54,6 +61,7 @@ int main()
5461
{
5562
Target *t = new Adapter();
5663
t->request();
57-
64+
delete t;
65+
5866
return 0;
5967
}

Diff for: adapter/ObjectAdapter.cpp

+19-11
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
* Target
1515
* defines specific interface that Client uses
1616
*/
17-
class Target {
17+
class Target
18+
{
1819
public:
20+
virtual ~Target() {}
21+
1922
virtual void request() = 0;
2023
// ...
2124
};
@@ -26,11 +29,12 @@ class Target {
2629
* to Adapter it will get calls that client makes on the Target
2730
*
2831
*/
29-
class Adaptee {
32+
class Adaptee
33+
{
3034
public:
31-
void specificRequest() {
35+
void specificRequest()
36+
{
3237
std::cout << "specific request" << std::endl;
33-
// ...
3438
}
3539
// ...
3640
};
@@ -40,17 +44,20 @@ class Adaptee {
4044
* implements the Target interface and when it gets a method call it
4145
* delegates the call to a Adaptee
4246
*/
43-
class Adapter : public Target {
47+
class Adapter : public Target
48+
{
4449
public:
4550
Adapter() : adaptee() {}
46-
47-
~Adapter() {
51+
52+
~Adapter()
53+
{
4854
delete adaptee;
4955
}
50-
51-
void request() {
56+
57+
void request()
58+
{
5259
adaptee->specificRequest();
53-
// ...
60+
// ...
5461
}
5562
// ...
5663

@@ -64,6 +71,7 @@ int main()
6471
{
6572
Target *t = new Adapter();
6673
t->request();
67-
74+
delete t;
75+
6876
return 0;
6977
}

0 commit comments

Comments
 (0)