I am here to continue the discussion around Design Patterns. Today we will discuss another creational design pattern called Factory Method.
In case, you have not had a look at my previous articles, go through the following link:
- Design Patterns Simplified: Part 1
- Design Patterns Simplified - Part 2 (Singleton)
- Design Patterns Simplified - Part 3 (Simple Factory)
- Design Patterns Simplified - Part 4 (Abstract Factory)
Before talking about its implementation let’s begin with defining it.
Factory method pattern is similar to simple Factory pattern with a few difference. Unlike Simple factory, here client doesn’t call the factory class instead it calls the factory interface. Factory interface further have concrete implementations where object creation is performed.
Now let’s talk about implementation.
How to use Factory Method Pattern
Let’s try to understand using simple example.
Assume the client wants to know the on-road price of various brands of cars and have an interface as in the following,
- interface ICar
- {
- string GetOnRoadPrice(string model);
- }
- class Maruti : ICar
- {
- public string GetOnRoadPrice(string model)
- {
- //Logic to get OnRoadPrice based on model
- if (model == "Alto 800 VXI")
- {
- return "3.4 Lakhs INR";
- }
- else
- {
- return "Information not available!";
- }
- }
- }
- class Hyundai : ICar
- {
- public string GetOnRoadPrice(string model)
- {
- //Logic to get OnRoadPrice based on model
- if (model == "Grand i10 Magna 1.2 BSV")
- {
- return "5.4 Lakhs INR";
- }
- else
- {
- return "Information not available!";
- }
- }
- }
- interface ICarFactory
- {
- ICar GetCar();
- }
- class MarutiFactory : ICarFactory
- {
- public ICar GetCar()
- {
- return new Maruti();
- }
- }
- class HyundaiFactory : ICarFactory
- {
- public ICar GetCar()
- {
- return new Hyundai();
- }
- }
- //Client side
- Console.Title = "Factory method pattern demo";
- ICarFactory carFactory = null;
- ICar car = null;
- string model = null;
- //Maruti
- carFactory = new MarutiFactory();
- car = carFactory.GetCar();
- model = "Alto 800 VXI";
- Console.WriteLine("On-road price for {0} car is: {1} ", model, car.GetOnRoadPrice(model));

- //Hyundai
- carFactory = new HyundaiFactory();
- car = carFactory.GetCar();
- model = "Grand i10 Magna 1.2 BSV";
- Console.WriteLine("On-road price for {0} car is: {1} ", model, car.GetOnRoadPrice(model));

And here goes the class diagram.

As you can see in the above code that,
- Client is getting the required factory object by just creating instance of respective car factory brand.
- And then it gets the required car object by calling GetCar method of retrieved factory object.
- Client gets the On-road price of the car by calling GetOnRoadPrice method and passing model name.
So just to summarize, using Factory Method pattern, we achieved the following,
- Loose coupling between client and business layers.
- Client is only aware about Factory Interface (ICarFactory) and not their concrete implementations.
- Abstracted concreate classes (Maruti, Hyundai) from client.
Hope you have liked the article. Looking forward for your comments/suggestions.

Prakash TripathiPosted Apr 9, 2016, 10:41 AM
Thnx Nagaraj.
Kuppurasu NagarajPosted Apr 9, 2016, 10:35 AM
Nice article..
Prakash TripathiPosted Mar 6, 2016, 11:10 PM
Thnx Kumaresh.
Prakash TripathiPosted Mar 6, 2016, 11:10 PM
Thnx Sr Karthiga.
Kumaresh RajalingamPosted Mar 6, 2016, 8:30 PM
good one
Kumaresh RajalingamPosted Mar 6, 2016, 8:30 PM
Nice explanation
Sr KarthigaPosted Mar 6, 2016, 8:20 PM
good one
Sr KarthigaPosted Mar 6, 2016, 8:20 PM
Nice explanation
Prakash TripathiPosted Dec 30, 2015, 12:49 PM
Thnx Rupesh.
Rupesh KahanePosted Dec 30, 2015, 12:38 PM
good one
Prakash TripathiPosted Dec 28, 2015, 9:09 AM
Thnx Humayun
Humayun Kabir MamunPosted Dec 14, 2015, 1:15 AM
Nice...
Prakash TripathiPosted Dec 14, 2015, 12:45 AM
Thanks Raja.
Raja TPosted Dec 13, 2015, 11:12 PM
Good,thank for sharing