I was reading about creational design patterns and have managed to utterly confuse myself between Factory , Abstract Factory and Factory method.
I am posting a code snippet below. Would someone be kind enough to let me know which one is this and (if possible) what changes could be made to the code to make it fall under the other categories?
#include "iostream.h"
#define QUIT 2
class Shape {
public:
virtual void draw() = 0;
};
class Circle : public Shape {
public:
void draw() {
cout << "circle : draw" << endl;
}
};
class Square : public Shape {
public:
void draw() {
cout << "square : draw" << endl;
}
};
class Factory {
public:
virtual Shape* createInstance(int id) = 0;
};
class SimpleShapeFactory : public Factory {
public:
Shape* createInstance( int id) {
if(id == 0)
return new Circle;
else if(id == 1)
return new Square;
else
return new Circle; //as a default
}
};
int main() {
Factory* factory = new SimpleShapeFactory();
int choice = 0;
Shape* shape;
do
{
cout<<"\n 0. Circle";
cout<<"\n 1. Square";
cout<<"\n 2. Quit";
cout<<"\n Enter your choice : ";
cin>>choice;
if(choice == QUIT)
break;
shape = factory->createInstance(choice);
shape->draw();
} while (choice !=QUIT);
}