Now-a-days, if we create a software, it gets outdated soon because of the requirement changes in future. So, that time, we need to write the whole logic again from scratch. The main concern when we work with Software Architecture is how to create object of entity and pass that to some other object without depending on others. One thing we need to consider is architecture should be pluggable so that in the future more objects can be added.
What Factory Design Pattern Is
The Factory Design Pattern is a commonly used design pattern where we need to create Loosely Coupled System. Basically, it comes under Creational Pattern and it is used to create instance and reuse it. Factory Pattern is based on real time factory concept. As we know, a factory is used to manufacture something as per the requirement and if new items are added in the manufacturing process, the factory starts manufacturing those items as well. Factory class provides abstraction between Client and Car when creating the instance of the Car [Honda, BMW etc].
When to use Factory Design Pattern
It is used for creating objects to encapsulate the instantiation logic. Client doesn’t know the actual instantiation logic of entity.
Problem
See the following example of code, where we have created two different classes as Honda and BMW. Those classes are implementing the ICarSupplier interface which has one property as CarColor and one method which provides the Car Model.
On the client side, we are simply creating the objects of two classes to get their member function and behavior.
- using System;
- namespace FactoryDesignPattern
- {
- public interface ICarSupplier
- {
- string CarColor
- {
- get;
- }
- void GetCarModel();
- }
- class Honda : ICarSupplier
- {
- public string CarColor
- {
- get { return "RED"; }
- }
- public void GetCarModel()
- {
- Console.WriteLine("Honda Car Model is Honda 2014");
- }
- }
- class BMW : ICarSupplier
- {
- public string CarColor
- {
- get { return "WHITE"; }
- }
- public void GetCarModel()
- {
- Console.WriteLine("BMW Car Model is BMW 2000");
- }
- }
- class ClientProgram
- {
- static void Main(string[] args)
- {
- Honda objHonda = new Honda();
- objHonda.GetCarModel();
- BMW objBMW = new BMW();
- objBMW.GetCarModel();
- Console.ReadLine();
- }
- }
- }
Nano is a new class which also implements ICarSupplier.
- class Nano : ICarSupplier
- {
- public string CarColor
- {
- get { return "YELLOW"; }
- }
- public void GetCarModel()
- {
- Console.WriteLine("Nano Car Model is Nano 2016");
- }
- }
- Nano objNano = new Nano();
- objNano.GetCarModel();
Solution
So, in Factory Design Pattern, there we will add a Factory class where we can add a method which will return the instance of the class based on your requirement. We can see with the following code where GetCarInstance method takes one argument as Id.
On the basis of the Id, it will return the instance of the Car. As per example, if client passes 0, then it will return the instance of the Honda Car, if they pass 1, then it will return the instance of BMW car.
- static class CarFactory
- {
- public static ICarSupplier GetCarInstance(int Id)
- {
- switch (Id)
- {
- case 0:
- return new Honda();
- case 1:
- return new BMW();
- case 2:
- return new Nano();
- default:
- return null;
- }
- }
- }
case 3
- return new Suzuki();
- ICarSupplier objCarSupplier = CarFactory.GetCarInstance(3);
- objCarSupplier.GetCarModel();
- Console.WriteLine("And Coloar is " + objCarSupplier.CarColor);
- using System;
- namespace FactoryDesignPattern
- {
- public interface ICarSupplier
- {
- string CarColor
- {
- get;
- }
- void GetCarModel();
- }
- class Honda : ICarSupplier
- {
- public string CarColor
- {
- get { return "RED"; }
- }
- public void GetCarModel()
- {
- Console.WriteLine("Honda Car Model is Honda 2014");
- }
- }
- class BMW : ICarSupplier
- {
- public string CarColor
- {
- get { return "WHITE"; }
- }
- public void GetCarModel()
- {
- Console.WriteLine("BMW Car Model is BMW 2000");
- }
- }
- class Nano : ICarSupplier
- {
- public string CarColor
- {
- get { return "YELLOW"; }
- }
- public void GetCarModel()
- {
- Console.WriteLine("Nano Car Model is Nano 2016");
- }
- }
- class Suzuki : ICarSupplier
- {
- public string CarColor
- {
- get { return "Orange"; }
- }
- public void GetCarModel()
- {
- Console.WriteLine("Suzuki Car Model is Suzuki 2006");
- }
- }
- static class CarFactory
- {
- public static ICarSupplier GetCarInstance(int Id)
- {
- switch (Id)
- {
- case 0:
- return new Honda();
- case 1:
- return new BMW();
- case 2:
- return new Nano();
- case 3:
- return new Suzuki();
- default:
- return null;
- }
- }
- }
- class ClientProgram
- {
- static void Main(string[] args)
- {
- ICarSupplier objCarSupplier = CarFactory.GetCarInstance(3);
- objCarSupplier.GetCarModel();
- Console.WriteLine("And Coloar is " + objCarSupplier.CarColor);
- Console.ReadLine();
- }
- }
- }
So, today we learned what Factory Design Pattern is and why and where it can be used, with real world examples.
I hope this post will help you. Please put your feedback in the comment box which helps me improve myself for my next posts.
No comments:
Post a Comment