Factory Method
Factory method is a widely used mechanism for creating instances of classes in any object oriented programming language. The Factory method abstracts the creation of objects from the consumer. It also provides a single place where objects can be created. I supplied the desired objects hiding the complexity of creation for the consumers.
Advantages of Factory Method
- Hides the complexity of creation from the consumer.
- Ensures the object creation logic to be in a single place
- Helps customise creation without disturbing the consumer logic
- Brings in logical separation between creation and usage
I have used a simple
CarFactory
example to demonstrate the same. The Factory manufactures swift cars of 2 types basic and featured with 3 different colors black, blue and red.
Steps for implementing the Factory Method
- Create an
abstract
class Swift Car with an attribute color and a method CaliculatePrice
as the price differs for different models:
public abstract class SwiftCar
{
public CarColor Color { get; private set; }
protected SwiftCar(CarColor color)
{
this.Color = color;
}
public abstract float CaliculatePrice( );
}
- Create 2 derived classes For Basic Swift Car and Featured Swift Car:
public class SwiftCarBasic:SwiftCar
{
public SwiftCarBasic(CarColor color):base(color )
{
}
public override float CaliculatePrice()
{
float BasicPrice = 400000F;
return BasicPrice;
}
}
public class SwiftCarFeatured : SwiftCar
{
public SwiftCarFeatured(CarColor Color):base(Color )
{
}
public override float CaliculatePrice()
{
float basicPrice = 400000F;
float otherEquipmentCosts= 200000F;
return basicPrice +otherEquipmentCosts ;
}
}
- Define Enumerations which describe the
Car
types and Car
colors
public enum SwiftCarType
{
BASIC,
FEATURED
}
public enum CarColor
{
BLACK,
RED,
BLUE
}
- Create a
static
class with a static
method which returns the Car
of desired Type.
Note: It is very important to keep the abstract
base class Swift car
as a Return Type.
public static class SwiftCarFactory
{
public static SwiftCar CreateSwiftCar(SwiftCarType carType, CarColor carColor)
{
SwiftCar car = null;
switch (carType)
{
case SwiftCarType.BASIC: car = new SwiftCarBasic(carColor);
break;
case SwiftCarType.FEATURED: car = new SwiftCarFeatured(carColor);
break;
}
return car;
}
}
- Design a Client to consume the
Car
from the Swift Car Factory.
class ClientClass
{
public static void Main(string[] args)
{
SwiftCar BlueBasicSwiftCar = SwiftCarFactory.CreateSwiftCar(SwiftCarType.BASIC, CarColor.BLUE);
float BlueBasicSwiftCarPrice = BlueBasicSwiftCar.CaliculatePrice();
Console.WriteLine("price of Blue Basic Swift car" + BlueBasicSwiftCarPrice);
SwiftCar RedFeaturedSwiftCar = SwiftCarFactory.CreateSwiftCar(SwiftCarType.FEATURED, CarColor.RED);
float RedFeaturedSwiftCarPrice = RedFeaturedSwiftCar.CaliculatePrice();
Console.WriteLine("price of Red Featured Swift car" + RedFeaturedSwiftCarPrice );
}
}
Finally, it can be observed that creation logic is unknown to the client and since the factory takes care of the creation, the Factory logic can be further customised or changed easily.