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,
- interface ICar
- {
- string GetOnRoadPrice(string model);
- }
And here goes the concrete implementation of the ICar.
- class Maruti : ICar
- {
- public string GetOnRoadPrice(string model)
- {
-
- if (model == "Alto 800 VXI")
- {
- return "3.4 Lakhs INR";
- }
- else
- {
- return "Information not available!";
- }
- }
- }
-
- class Hyundai : ICar
- {
- public string GetOnRoadPrice(string model)
- {
-
- if (model == "Grand i10 Magna 1.2 BSV")
- {
- return "5.4 Lakhs INR";
- }
- else
- {
- return "Information not available!";
- }
- }
- }
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.
- interface ICarFactory
- {
- ICar GetCar();
- }
And here goes the concrete implementation of the ICarFactory.
- class MarutiFactory : ICarFactory
- {
- public ICar GetCar()
- {
- return new Maruti();
- }
- }
-
- class HyundaiFactory : ICarFactory
- {
- public ICar GetCar()
- {
- return new Hyundai();
- }
- }
Now let’s see, how client can use the setup we have created so far.
-
- Console.Title = "Factory method pattern demo";
- ICarFactory carFactory = null;
- ICar car = null;
- string model = null;
-
-
- carFactory = new MarutiFactory();
- car = carFactory.GetCar();
- model = "Alto 800 VXI";
- Console.WriteLine("On-road price for {0} car is: {1} ", model, car.GetOnRoadPrice(model));
Output
-
- 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));
Output
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.