4.1.2 Factory Pattern

The Factory pattern is a kind of creational pattern. The key intent of this pattern is to create or instantiate an object using a responding creational method instead of specifying the exact class of this object. In other words, a class can defer instantiation to subclasses (Gamma 1994).

The basic principles of this pattern are inheritance and polymorphism. Figure 4.1.3 illustrates the structure of this pattern showing the two parts, the abstract creator (abstract factory) and abstract product. The abstract factory obtains an abstract creation method, and the concrete factory extends this method to create the responding concrete products.

Code segment - A the implementation of factory method pattern in my project. In my project, a template class help to achieve this goal. A template class called ‘ShapeFactory’ is declared. These two typenames, ‘Product’ and ‘ConcreteProduct’, refer to the abstract product and concrete product. The method, ‘create_a_shape’, which is declared inside this class is a creational method. The code segment - B illustrates the process for all the different shapes. Each line creates a specific shape. For example, the first line creates a circle whose father type is curve, and the third line creates a triangle whose father type is polygon. Indeed, in order to create a new shape, I just need to make a declaration of father type and sub type in the template method respectively. Overall, this pattern provides a creational method to instantiate an object avoiding the direct use of the new keyword. However, too many creational methods could cause confusion.

Figure 4.1.3: The structure of Factory method pattern (Vanderjoe 2016)
template <typename Product, typename ConcreteProduct>
class ShapeFactory
{
  public:
      static Product* create_a_shape()
      {
        return new ConcreteProduct();  
      }
}
Code Segment - A
Curve* a_circle = ShapeFactory<Curve,Circle>::create_a_shape();
Curve* a_ellipse = ShapeFactory<Curve,Ellipse>::create_a_shape();
Polygon* a_triangle = ShapeFactory<Polygon,Triangle>::create_a_shape();
Polygon* a_quadrangle = ShapeFactory<Polygon,Quadrangle>::create_a_shape();
Polygon* a_pentagon = ShapeFactory<Polygon,Pentagon>::create_a_shape();
Polygon* a_hexagon = ShapeFactory<Polygon,Hexagon>::create_a_shape();
Triangle* isosceles = ShapeFactory<Triangle,IsoscelesTriangle>::create_a_shape();
Triangle* right = ShapeFactory<Triangle,RightTriangle>::create_a_shape();
Triangle* equilater = ShapeFactory<Triangle,EquilateralTriangle>::create_a_shape();
Quadrange* para = ShapeFactory<Quadrange,Parallelogram>::create_a_shape();
Quadrange* rectangle = ShapeFactory<Quadrange,Rectangle>::create_a_shape();
Quadrange* trap = ShapeFactory<Quadrange,Trapezoid>::create_a_shape();
Quadrange* rbom = ShapeFactory<Quadrange,Rhombus>::create_a_shape();
Hexagon* r_hex = ShapeFactory<Hexagon,RegularHexagon>::create_a_shape();
Pentagon* r_pen = ShapeFactory<Pentagon,RegularPentagon>::create_a_shape();
Code Segment - B