code refactoring: fix memory leaks, code style, etc.

This commit is contained in:
Jakub Vojvoda
2019-01-31 19:57:40 +01:00
parent a8681552c4
commit a0b0ea4f8e
24 changed files with 960 additions and 566 deletions

View File

@@ -15,8 +15,11 @@
* products implement the same interface so that the classes can refer
* to the interface not the concrete product
*/
class Product {
class Product
{
public:
virtual ~Product() {}
virtual std::string getName() = 0;
// ...
};
@@ -25,9 +28,13 @@ public:
* Concrete Product
* define product to be created
*/
class ConcreteProductA : public Product {
class ConcreteProductA : public Product
{
public:
std::string getName() {
~ConcreteProductA() {}
std::string getName()
{
return "type A";
}
// ...
@@ -37,9 +44,13 @@ public:
* Concrete Product
* define product to be created
*/
class ConcreteProductB : public Product {
class ConcreteProductB : public Product
{
public:
std::string getName() {
~ConcreteProductB() {}
std::string getName()
{
return "type B";
}
// ...
@@ -50,10 +61,16 @@ public:
* contains the implementation for all of the methods
* to manipulate products except for the factory method
*/
class Creator {
class Creator
{
public:
virtual ~Creator() {}
virtual Product* createProductA() = 0;
virtual Product* createProductB() = 0;
virtual void removeProduct( Product *product ) = 0;
// ...
};
@@ -63,15 +80,25 @@ public:
* one or more concrete products ie. it is class that has
* the knowledge of how to create the products
*/
class ConcreteCreator : public Creator {
class ConcreteCreator : public Creator
{
public:
Product* createProductA() {
~ConcreteCreator() {}
Product* createProductA()
{
return new ConcreteProductA();
}
Product* createProductB() {
Product* createProductB()
{
return new ConcreteProductB();
}
void removeProduct( Product *product )
{
delete product;
}
// ...
};
@@ -79,12 +106,15 @@ public:
int main()
{
Creator *creator = new ConcreteCreator();
Product *p1 = creator->createProductA();
std::cout << "Product: " << p1->getName() << std::endl;
creator->removeProduct( p1 );
Product *p2 = creator->createProductB();
std::cout << "Product: " << p2->getName() << std::endl;
creator->removeProduct( p2 );
delete creator;
return 0;
}