Abstract Factory Pattern Using C# - Real World Example
While learning about design patterns, I came to understand the most frequently used term, Factory Pattern as well as Abstract factory pattern. I searched the internet and came across numerous learning points. After a lot of search and study, I endeavored to find an actual need for the abstract design pattern.
In this article, I will explore this pattern in an easy way so that everyone can have a better understanding of it. Down the level, I'm using a Car (Vehicle) example to demonstrate abstract factory pattern.
An important aspect of software design is the manner in which objects are created. Thus, it is not only important what an object does or what it models, but also in what manner it was created.
Reference: http://www.dofactory.com/
Definition
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Definition
I'd keep the structure in such a way as depicted below.
Participants
The classes and objects participating in this pattern are:
- AbstractFactory (IVehicleFactory)
- declares an interface for operations that create abstract products.
- ConcreteFactory (MarutiFactory, TataFactory)
- implements the operations to create concrete product objects.
- AbstractProduct (ICarEngine, ICarLight)
- declares an interface for a type of product object.
- Product (DDis, LED, Revtron, Helogan)
- defines a product object to be created by the corresponding concrete factory.
- implements the AbstractProduct interface.
- Client (Clinet)
- uses interfaces declared by AbstractFactory and AbstractProduct classes.
Implementation
- using System;
- namespace AbstractFactoryPattern
- {
- interface IVehicleFactory
- {
- ICarEngine GetCarEngine();
- ICarLight GetCarLight();
- }
- class MarutiFactory : IVehicleFactory
- {
- public ICarEngine GetCarEngine()
- {
- return new DDiS();
- }
- public ICarLight GetCarLight()
- {
- return new LED();
- }
- }
- class TataFactory : IVehicleFactory
- {
- public ICarEngine GetCarEngine()
- {
- return new Revtron();
- }
- public ICarLight GetCarLight()
- {
- return new Helogen();
- }
- }
- class LED : ICarLight
- {
- public string GetLightInfo()
- {
- return "Led lights";
- }
- }
- class DDiS : ICarEngine
- {
- public string GetEngineInfo()
- {
- return "DDis engine for their diesal cars...";
- }
- }
- internal class Helogen : ICarLight
- {
- public string GetLightInfo()
- {
- return "Helogan Light...";
- }
- }
- internal class Revtron : ICarEngine
- {
- public string GetEngineInfo()
- {
- return "Revtron engine for their diesal/petrol cars...";
- }
- }
- internal interface ICarLight
- {
- string GetLightInfo();
- }
- internal interface ICarEngine
- {
- string GetEngineInfo();
- }
- class Client
- {
- private IVehicleFactory vehicleFactory = null;
- public void CreateCarWithLight(string carName)
- {
- if (carName.ToLower() == "maruti")
- {
- vehicleFactory = new MarutiFactory();
- Console.Write(
- $"{carName} uses {vehicleFactory.GetCarEngine().GetEngineInfo()} with {vehicleFactory.GetCarLight().GetLightInfo()} as headlight");
- }
- else if (carName.ToLower() == "tata")
- {
- vehicleFactory = new TataFactory();
- Console.Write(
- $"{carName} uses {vehicleFactory.GetCarEngine().GetEngineInfo()} with {vehicleFactory.GetCarLight().GetLightInfo()} as headlight");
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.Write("**** Welcome to Abstract Factory pattern By DotnetPiper.com *******\n Kinldy enter your car name...");
- Client client = new Client();
- string carName = Console.ReadLine();
- client.CreateCarWithLight(carName);
- Console.ReadLine();
- }
- }
- }
Run your application now. The following window will appear.
Put the desired car name you would like to know the information about, like Maruti, Tata etc.
Kindly refer to the following window as a result after you put Maruti.
Link to download sample application Abstract Factory Pattern Using C# - Real World Example