#include<iostream>
#include<string>
#include<map>
#include<stdlib.h>
using namespace std;
string colors[] =
{ "Red", "Green", "Blue", "White", "Black" };
class Shape
{
public:
virtual void draw() = 0;
};
class Circle : public Shape
{
private:
string color;
int x;
int y;
int radius;
public:
Circle(string color)
{
this->color = color;
}
void setX(int x)
{
this->x = x;
}
void setY(int y)
{
this->y = y;
}
void setRadius(int radius)
{
this->radius = radius;
}
void draw()
{
cout<<"Circle: Draw() [Color : "<<color<<", x : "<<x<<", y :"<<y<<", radius :"<<radius<<endl;
}
};
class ShapeFactory
{
private:
static map<string, Shape *> circleMap;
public:
static Shape * getCircle(string color)
{
Circle * circle = (Circle*)circleMap[color];
if (circle == NULL)
{
circle = new Circle(color);
circleMap[color] = circle;
cout<<"Creating circle of color : "<<color<<endl;
}
return circle;
}
};
map<string, Shape *> ShapeFactory::circleMap;
string getRandomColor()
{
return colors[rand()%(sizeof(colors)/sizeof(colors[0]))];
}
int getRandomX()
{
return rand()%100;
}
int getRandomY()
{
return rand()%100;
}
int main()
{
for(int i=0; i < 20; ++i) {
Circle *circle =
(Circle*)ShapeFactory::getCircle(getRandomColor());
circle->setX(getRandomX());
circle->setY(getRandomY());
circle->setRadius(100);
circle->draw();
}
return 0;
}
|