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!";
- }
- }
- }




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