Skip to content

Commit

Permalink
code refactoring: fix memory leaks, code style, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubVojvoda committed Jan 31, 2019
1 parent a868155 commit a0b0ea4
Show file tree
Hide file tree
Showing 24 changed files with 957 additions and 563 deletions.
102 changes: 73 additions & 29 deletions abstract-factory/AbstractFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* Source code is licensed under MIT License
* (for more details see LICENSE)
*
*
*/

#include <iostream>
Expand All @@ -15,26 +15,38 @@
* products implement the same interface so that the classes can refer
* to the interface not the concrete product
*/
class ProductA {
class ProductA
{
public:
virtual std::string getName() = 0;
virtual ~ProductA() {}

virtual const char* getName() = 0;
// ...
};

/*
* ConcreteProductAX and ConcreteProductAY
* define objects to be created by concrete factory
*/
class ConcreteProductAX : public ProductA {
class ConcreteProductAX : public ProductA
{
public:
std::string getName() {
~ConcreteProductAX() {}

const char* getName()
{
return "A-X";
}
// ...
};

class ConcreteProductAY : public ProductA {
std::string getName() {
class ConcreteProductAY : public ProductA
{
public:
~ConcreteProductAY() {}

const char* getName()
{
return "A-Y";
}
// ...
Expand All @@ -45,25 +57,38 @@ class ConcreteProductAY : public ProductA {
* same as Product A, Product B declares interface for concrete products
* where each can produce an entire set of products
*/
class ProductB {
class ProductB
{
public:
virtual std::string getName() = 0;
virtual ~ProductB() {}

virtual const char* getName() = 0;
// ...
};

/*
* ConcreteProductBX and ConcreteProductBY
* same as previous concrete product classes
*/
class ConcreteProductBX : public ProductB {
std::string getName() {
class ConcreteProductBX : public ProductB
{
public:
~ConcreteProductBX() {}

const char* getName()
{
return "B-X";
}
// ...
};

class ConcreteProductBY : public ProductB {
std::string getName() {
class ConcreteProductBY : public ProductB
{
public:
~ConcreteProductBY() {}

const char* getName()
{
return "B-Y";
}
// ...
Expand All @@ -73,8 +98,11 @@ class ConcreteProductBY : public ProductB {
* Abstract Factory
* provides an abstract interface for creating a family of products
*/
class AbstractFactory {
class AbstractFactory
{
public:
virtual ~AbstractFactory() {}

virtual ProductA *createProductA() = 0;
virtual ProductB *createProductB() = 0;
};
Expand All @@ -84,23 +112,33 @@ class AbstractFactory {
* each concrete factory create a family of products and client uses
* one of these factories so it never has to instantiate a product object
*/
class ConcreteFactoryX : public AbstractFactory {
class ConcreteFactoryX : public AbstractFactory
{
public:
ProductA *createProductA() {
~ConcreteFactoryX() {}

ProductA *createProductA()
{
return new ConcreteProductAX();
}
ProductB *createProductB() {
ProductB *createProductB()
{
return new ConcreteProductBX();
}
// ...
};

class ConcreteFactoryY : public AbstractFactory {
class ConcreteFactoryY : public AbstractFactory
{
public:
ProductA *createProductA() {
~ConcreteFactoryY() {}

ProductA *createProductA()
{
return new ConcreteProductAY();
}
ProductB *createProductB() {
ProductB *createProductB()
{
return new ConcreteProductBY();
}
// ...
Expand All @@ -109,14 +147,20 @@ class ConcreteFactoryY : public AbstractFactory {

int main()
{
ConcreteFactoryX *factoryX = new ConcreteFactoryX();
ConcreteFactoryY *factoryY = new ConcreteFactoryY();

ProductA *p1 = factoryX->createProductA();
std::cout << "Product: " << p1->getName() << std::endl;
ConcreteFactoryX *factoryX = new ConcreteFactoryX();
ConcreteFactoryY *factoryY = new ConcreteFactoryY();

ProductA *p2 = factoryY->createProductA();
std::cout << "Product: " << p2->getName() << std::endl;

return 0;
ProductA *p1 = factoryX->createProductA();
std::cout << "Product: " << p1->getName() << std::endl;

ProductA *p2 = factoryY->createProductA();
std::cout << "Product: " << p2->getName() << std::endl;

delete p1;
delete p2;

delete factoryX;
delete factoryY;

return 0;
}
24 changes: 16 additions & 8 deletions adapter/ClassAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
* Target
* defines specific interface that Client uses
*/
class Target {
class Target
{
public:
virtual ~Target() {}

virtual void request() = 0;
// ...
};
Expand All @@ -25,11 +28,14 @@ class Target {
* all requests get delegated to the Adaptee which defines
* an existing interface that needs adapting
*/
class Adaptee {
class Adaptee
{
public:
void specificRequest() {
~Adaptee() {}

void specificRequest()
{
std::cout << "specific request" << std::endl;
// ...
}
// ...
};
Expand All @@ -40,11 +46,12 @@ class Adaptee {
* to request on a Target by extending both classes
* ie adapts the interface of Adaptee to the Target interface
*/
class Adapter : public Target, private Adaptee {
class Adapter : public Target, private Adaptee
{
public:
virtual void request() {
virtual void request()
{
specificRequest();
// ...
}
// ...
};
Expand All @@ -54,6 +61,7 @@ int main()
{
Target *t = new Adapter();
t->request();

delete t;

return 0;
}
30 changes: 19 additions & 11 deletions adapter/ObjectAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
* Target
* defines specific interface that Client uses
*/
class Target {
class Target
{
public:
virtual ~Target() {}

virtual void request() = 0;
// ...
};
Expand All @@ -26,11 +29,12 @@ class Target {
* to Adapter it will get calls that client makes on the Target
*
*/
class Adaptee {
class Adaptee
{
public:
void specificRequest() {
void specificRequest()
{
std::cout << "specific request" << std::endl;
// ...
}
// ...
};
Expand All @@ -40,17 +44,20 @@ class Adaptee {
* implements the Target interface and when it gets a method call it
* delegates the call to a Adaptee
*/
class Adapter : public Target {
class Adapter : public Target
{
public:
Adapter() : adaptee() {}

~Adapter() {

~Adapter()
{
delete adaptee;
}

void request() {

void request()
{
adaptee->specificRequest();
// ...
// ...
}
// ...

Expand All @@ -64,6 +71,7 @@ int main()
{
Target *t = new Adapter();
t->request();

delete t;

return 0;
}
Loading

0 comments on commit a0b0ea4

Please sign in to comment.