Factory Method Design Pattern In C#
The Factory method pattern is a way of creating objects but allows subclasses to instantly determine which class. Simply, in this method, we create a special factory for creating objects.
Factory method pattern UML diagram:
The list of Players is :
IProduct
The interface for products. Within the interface, we can include different subclass methods that might implement the interface
ProductA, ProductB
These are classes that implement the IProduct interface. In this example, we implement two subclasses but if we want, we can create more subclasses according to the client’s requirements.
Creator
Provides the FactoryMethod. This class includes the “FactoryMethod” method. The FactoryMethod will choose to supply either ProductA or ProductB. FactoryMethod decides which class to instantiate.
Client
The client declares a product variable but calls a “FactoryMethod” to instantiate it.
The design of this pattern enables the decision-making about which product to be instantiated to be handled in one place. If the client knew about all the options, the decisions would be dispersed in its code. As it is, the client need only be concerned with getting products; it does not even have to bind in all different subclasses of products.
Implementation with an example
Design a system to handle the shapes: circles, ellipses, rectangles, and squares. Design system for computing the area and perimeter of the shapes.
1 st step
Create interface. The interface should have two method prototypes for computing the area and perimeter of the shapes.
interface IShape
{
double GetArea();
double GetPerimeter();
}
2 nd step
Create classes for each of the shapes. You should use inheritance appropriately.
class Circle : IShape
{
protected double a;
public Circle(double a)
{
this.a = a;
}
public virtual double GetArea()
{
return Math.PI*Math.Pow(a,2);
}
public virtual double GetPerimeter()
{
return 2*Math.PI*a;
}
}
class Ellipse: Circle
{
double b;
public Ellipse(double a,double b) : base(a)
{
this.b = b;
}
public override double GetArea()
{
return Math.PI*a*b;
}
public override double GetPerimeter()
{
return 2*Math.PI*Math.Sqrt((Math.Pow(a,2)+ Math.Pow(b,2))/2);
}
}
class Square : IShape
{
protected double a;
public Square(double a)
{
this.a = a;
}
public virtual double GetArea()
{
return Math.Pow(a, 2);
}
public virtual double GetPerimeter()
{
return 4*a;
}
}
class Rectangle : Square
{
double b;
public Rectangle(double a,double b) : base(a)
{
this.b = b;
}
public override double GetArea()
{
return a*b;
}
public override double GetPerimeter()
{
return (2*a) + (2*b);
}
}
3 rd step
Create a creator class. The object should be created based on user inputs. We can get user input as a symbol as shown in the table below.
class Creator
{
public IShape FactoryMethod(char ch, double a, double b)
{
if(ch == 'c')
{
return new Circle(a);
}
else if(ch == 's')
{
return new Square(a);
}
else if(ch == 'e')
{
return new Ellipse(a,b);
}
else if(ch == 'r')
{
return new Rectangle(a,b);
}
return null;
}
}
4th step
Create the main method. The client creates an object and gives input to get perimeter and area.
If you want to calculate the area and perimeter of the circle (radius = 7 )
public static void Main()
{
Creator c = new Creator();
IShape shape;
shape = c.FactoryMethod('c',7,0);
Console.WriteLine("Circle area : {0}" , shape.GetArea());
Console.WriteLine("Circle perimeter : {0}, shape.GetPerimeter());
}
you will get results:
Use of Factory Method pattern
The Factory Method pattern is a lightweight pattern that achieves independence from application-specific classes. The client programs to the interface and lets the pattern sort out the rest.
A particular advantage of the Factory Method pattern is that it can connect parallel class hierarchies.
I think you got the basic idea about the Factory Method design pattern.