Does this following program implement the factory design.if not what
are things that i have to change in order to make this following
program to be designed to factory design pattern.
#include<iostream>
Quote:
using namespace std;
>
class Quad
{
public:
void Area();
void Desc();
};
>
class Square : public Quad
{
public:
Square() {};
>
void Area(int x)
{
cout<<"Area of square is = "<<x*x<<endl;
}
>
void Desc()
{
cout<<"This Derived class Square from Base Class Quad"<<endl;
}
};
>
class Rectangle : public Quad
{
public:
Rectangle() {};
>
void Area(int x, int y)
{
cout<<"Area of Rectangle is = "<<x*y<<endl;
}
>
void Desc()
{
cout<<"This Derived class Rectangle from Base Class Quad"<<endl;
}
>
};
>
class Creator
{
public:
Quad* Creator::Create(int id)
{
if (id==2)
return new Square;
else
return new Rectangle;
}
};
>
int main(int argc, char* argv[])
{
Creator mcreator;
if (argc<=2)
{
Square *square = reinterpret_cast<Square*>(mcreator.Create(2));
square->Area(3);
square->Desc();
}
else
{
Rectangle *rectangle =
reinterpret_cast<Rectangle*>(mcreator.Create(1));
rectangle->Area(3,4);
rectangle->Desc();
}
>
return 0;
}
|