Design Patterns Simplified - Part 5 (Factory Method)

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:

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,

  1. interface ICar  
  2. {  
  3.    string GetOnRoadPrice(string model);  
  4. }   
And here goes the concrete implementation of the ICar.
  1. class Maruti : ICar  
  2. {  
  3.     public string GetOnRoadPrice(string model)  
  4.     {  
  5.         //Logic to get OnRoadPrice based on model  
  6.         if (model == "Alto 800 VXI")  
  7.         {  
  8.             return "3.4 Lakhs INR";  
  9.         }  
  10.         else  
  11.         {  
  12.             return "Information not available!";  
  13.         }  
  14.     }  
  15. }  
  16.   
  17. class Hyundai : ICar  
  18. {  
  19.     public string GetOnRoadPrice(string model)  
  20.     {  
  21.         //Logic to get OnRoadPrice based on model  
  22.         if (model == "Grand i10 Magna 1.2 BSV")  
  23.         {  
  24.             return "5.4 Lakhs INR";  
  25.         }  
  26.         else  
  27.         {  
  28.             return "Information not available!";  
  29.         }  
  30.     }  
  31. }  
We need to create a Factory Interface now which will sit between client and business layers and it will provide the required factory object to the client.
  1. interface ICarFactory  
  2. {  
  3.    ICar GetCar();  
  4. }  
And here goes the concrete implementation of the ICarFactory.
  1. class MarutiFactory : ICarFactory  
  2. {  
  3.     public ICar GetCar()  
  4.     {  
  5.         return new Maruti();  
  6.     }  
  7. }  
  8.   
  9. class HyundaiFactory : ICarFactory  
  10. {  
  11.     public ICar GetCar()  
  12.     {  
  13.         return new Hyundai();  
  14.     }  
  15. }  
Now let’s see, how client can use the setup we have created so far.
  1. //Client side  
  2. Console.Title = "Factory method pattern demo";  
  3. ICarFactory carFactory = null;  
  4. ICar car = null;  
  5. string model = null;  
  6.   
  7. //Maruti  
  8. carFactory = new MarutiFactory();  
  9. car = carFactory.GetCar();  
  10. model = "Alto 800 VXI";  
  11. Console.WriteLine("On-road price for {0} car is: {1} ", model, car.GetOnRoadPrice(model));  
Output

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

Output

And here goes the class diagram.

class diagram

As you can see in the above code that,
  1. Client is getting the required factory object by just creating instance of respective car factory brand.
  2. And then it gets the required car object by calling GetCar method of retrieved factory object.
  3. 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.


Similar Articles