Introduction
This article aims at understanding the Factory Method and a basic implementation of Factory method in C#.
Background
There are many scenarios when a class needs to create several objects. The class can do so freely but it has to know about each and every class. The idea of factory method is to have the classes ability to create other classes without them knowing about all the possible classes that exists. This gives us extensibility. With factory method, we get the possibility of creating, maintaining and manipulating a collection of different objects that have some common characteristics. The main use of this method is in classes that manage a lot of objects like Document Manager, Control manager, Facility Manager.
GOF defines factory method as "Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses".
Using the Code
Let us now look at the class diagram for Factory Pattern (Reference: GoF Design Patterns):
Let us try to see what each of these classes represents:
Product: defines the interface of objects the factory method creates
ConcreteProduct: implements the Product interface
Creator: declares the factory method, which returns an object of type Product.
ConcreteCreator: overrides the factory method to return an instance of a ConcreteProduct
Now what we have tried to do in our small exercise is that we have created a system that will manage the receivable for different type of employees, i.e., Manager, Engineer etc. so if we try to map the above mentioned classes with our little program, the hierarchy can be represented as:
Creating the Abstract Product
abstract class AItems
{
public override string ToString()
{
return this.GetType().Name;
}
}
Creating the Concrete Products
class Car : AItems { }
class CellPhone : AItems { }
class Computer : AItems { }
class LapTop : AItems { }
class MotorBike : AItems { }
class EntertainmentAllowance : AItems { }
class TravelAllowance : AItems { }
class Salary : AItems { }
class Incentives : AItems { }
Creating the Abstract Creator
abstract class AEmployee
{
List<AItems> benefits;
public AEmployee()
{
benefits = new List<AItems>();
}
public void AddBenefit(AItems ben)
{
benefits.Add(ben);
}
public void PrintBenefits()
{
foreach (AItems a in benefits)
{
Console.WriteLine(a.ToString());
}
}
public override string ToString()
{
return this.GetType().Name;
}
}
Creating the Concrete Creators
class Manager : AEmployee
{
public Manager()
{
AddBenefit(new Car());
AddBenefit(new CellPhone());
AddBenefit(new LapTop());
AddBenefit(new EntertainmentAllowance());
AddBenefit(new TravelAllowance());
AddBenefit(new Salary());
AddBenefit(new Incentives());
}
}
class Engineer : AEmployee
{
public Engineer()
{
AddBenefit(new Computer());
AddBenefit(new MotorBike());
AddBenefit(new TravelAllowance());
AddBenefit(new Salary());
}
}
Testing the Application
static void Main(string[] args)
{
AEmployee man = new Manager();
Console.WriteLine("Receivables for " + man.ToString() + ": \n");
man.PrintBenefits();
Console.ReadLine();
man = new Engineer();
Console.WriteLine("Receivables for " + man.ToString() + ": \n");
man.PrintBenefits();
Console.ReadLine();
}
Points of Interest
This article covers the basics of Factory Method and provides a basic implementation in C#. Perhaps it is not very useful and exciting for programming gurus here, but it sure is a good learning for a beginner like me.
History
- 10 Feb, 2012: Simple and rudimentary implementation of Factory method in C#