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 TripathiTOP 100Posted Apr 9, 2016, 6:41 AM EDT
Thnx Nagaraj.
Kuppurasu NagarajTOP 100Posted Apr 9, 2016, 6:35 AM EDT
Nice article..
Prakash TripathiTOP 100Posted Mar 6, 2016, 6:10 PM EST
Thnx Kumaresh.
Prakash TripathiTOP 100Posted Mar 6, 2016, 6:10 PM EST
Thnx Sr Karthiga.
Kumaresh RajalingamPosted Mar 6, 2016, 3:30 PM EST
good one
Kumaresh RajalingamPosted Mar 6, 2016, 3:30 PM EST
Nice explanation
Sr KarthigaPosted Mar 6, 2016, 3:20 PM EST
good one
Sr KarthigaPosted Mar 6, 2016, 3:20 PM EST
Nice explanation
Prakash TripathiTOP 100Posted Dec 30, 2015, 7:49 AM EST
Thnx Rupesh.
Rupesh KahanePosted Dec 30, 2015, 7:38 AM EST
good one
Prakash TripathiTOP 100Posted Dec 28, 2015, 4:09 AM EST
Thnx Humayun
Humayun Kabir MamunPosted Dec 13, 2015, 8:15 PM EST
Nice...
Prakash TripathiTOP 100Posted Dec 13, 2015, 7:45 PM EST
Thanks Raja.
Raja TPosted Dec 13, 2015, 6:12 PM EST
Good,thank for sharing