sunny wrote:
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>
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;
}
Factory design patterns can be a pain to work with, specially when
their allocations are leaked / ignored. I have no respect for any code
that uses reinterpret_cast flagrantly (and in many cases - the same
goes for dynamic_cast).
Here is one based on boost::shared_ptr with Shape having protected
ctors, disabled copy-ctors and a factory friend. Note: STL container is
*not* copying Shapes here.
A Square is_a Rectangle
Factory has a create() that produces the shape required via specified
template parameter.
#include <iostream>
#include <ostream>
#include <vector>
#include <boost/shared_ptr.hpp>
struct Shape
{
Shape() { }
Shape(const Shape& copy); // disabled
virtual ~Shape() = 0;
virtual double area() const = 0;
virtual void description() const = 0;
};
Shape::~Shape() { std::cout << "~Shape()\n"; }
class Rectangle : public Shape
{
double width, height;
friend class ShapeFactory;
protected:
Rectangle(double w, double h)
: width(w), height(h)
{
std::cout << "Rectangle()\n";
}
public:
~Rectangle()
{
std::cout << "~Rectangle()\n";
}
double area() const { return width * height; }
void description() const
{
std::cout << "Rectangle with area = ";
std::cout << area() << std::endl;
}
};
class Square : public Rectangle
{
friend class ShapeFactory;
protected:
Square(double w, double dummy)
: Rectangle(w, w)
{
std::cout << "Square()\n";
}
public:
~Square()
{
std::cout << "~Square()\n";
}
double area() const { return Rectangle::area(); }
void description() const
{
std::cout << "Square with area = ";
std::cout << area() << std::endl;
}
};
class Triangle : public Shape
{
double width, height;
friend class ShapeFactory;
protected:
Triangle(double w, double h)
: width(w), height(h)
{
std::cout << "Triangle()\n";
}
public:
~Triangle()
{
std::cout << "~Triangle()\n";
}
double area() const { return 0.5 * width * height; }
void description() const
{
std::cout << "Triangle with area = ";
std::cout << area() << std::endl;
}
};
class ShapeFactory
{
public:
template< typename ShapeType >
boost::shared_ptr< ShapeType >
create(const double w = 0.0, const double h = 0.0) const
{
return boost::shared_ptr< ShapeType >(new ShapeType(w, h));
}
};
int main()
{
ShapeFactory factory;
typedef boost::shared_ptr< Shape SP_Shapes;
std::vector< SP_Shapes vshapes;
vshapes.push_back( factory.create< Triangle >(10.1, 10.2) );
vshapes.push_back( factory.create< Rectangle >(4.2, 25.1) );
vshapes.push_back( factory.create< Square >(9.3) );
typedef std::vector< SP_Shapes >::iterator VIter;
for( VIter viter = vshapes.begin();
viter != vshapes.end();
++viter )
{
(*viter)->description();
}
// std::vector< SP_Shapes vshapes2(vshapes); // ok
}
/*
Triangle()
Rectangle()
Rectangle() // <- Square
Square()
Triangle with area = 51.51
Rectangle with area = 105.42
Square with area = 86.49
~Triangle()
~Shape()
~Rectangle()
~Shape()
~Square()
~Rectangle()
~Shape()
*/