I am here to continue the discussion around Design Patterns. Today we will discuss another variant of Factory design pattern called Abstract Factory.
In case, you have not had a look at our previous article about Factory pattern, go through the following link:
Before talking about its implementation let’s try to understand the purpose of the abstract factory pattern as follows.
Abstract factory pattern is next level to Simple Factory pattern where we define related factories or families of classes.
To understand this properly, let’s take the same example we took when going through Simple Factory pattern.
In Simple Factory pattern, we managed all the car brands with one interface. Now let’s assume consumer or client of the car wants next level of categorization by picking any car brand. Then how will you manage?
Here comes the Abstract Factory. It creates family of classes or related factories to address the need.
Enough theory. Now let’s talk about implementation.
How to use Abstract Factory Pattern?
Let’s try to understand using simple example.
Suppose client wants to know the car details of different kinds of brands. Car detail here is about knowing economy and premium models for any brand. For that we need to define the abstract product interface as in the following.
- interface IEconomy
- {
- string ShowEconomy();
- }
- interface IPremium
- {
- string ShowPremium();
- }
- class Alto: IEconomy
- {
- public string ShowEconomy()
- {
- return "Alto";
- }
- }
- class SCross: IPremium
- {
- public string ShowPremium()
- {
- return "S-Cross";
- }
- }
- class Eon: IEconomy
- {
- public string ShowEconomy()
- {
- return "Eon";
- }
- }
- class Elantra: IPremium
- {
- public string ShowPremium()
- {
- return "Elantra";
- }
- }
- interface ICarFactory
- {
- IEconomy GetEconomy();
- IPremium GetPremium();
- }
- class MarutiFactory: ICarFactory
- {
- public IEconomy GetEconomy()
- {
- return new Alto();
- }
- public IPremium GetPremium()
- {
- return new SCross();
- }
- }
- class HyundaiFactory: ICarFactory
- {
- public IEconomy GetEconomy()
- {
- return new Eon();
- }
- public IPremium GetPremium()
- {
- return new Elantra();
- }
- }
- Console.Title = "Abstract Factory pattern demo";
- ICarFactory carFactory = null;
- IEconomy economy = null;
- IPremium premium = null;
- //Maruti
- carFactory = new MarutiFactory();
- economy = carFactory.GetEconomy();
- premium = carFactory.GetPremium();
- Console.WriteLine("Normal car is: " + economy.ShowEconomy() + "\nPremium Car is: " + premium.ShowPremium());

- //Hyundai
- carFactory = new HyundaiFactory();
- economy = carFactory.GetEconomy();
- premium = carFactory.GetPremium();
- Console.WriteLine("Normal car is: " + economy.ShowEconomy() + "\nPremium Car is: " + premium.ShowPremium());

Hope you have liked the article. Look forward for your comments/suggestions.

Prakash TripathiPosted Jun 23, 2016, 1:53 PM
Thanks a lot vidya sir.
Vidya Vrat AgarwalPosted Jun 23, 2016, 1:47 PM
Great work Prakash Tripathi very well explained. Looking forward for more.
Prakash TripathiPosted Apr 9, 2016, 10:40 AM
Thnx Nagaraj.
Kuppurasu NagarajPosted Apr 9, 2016, 10:35 AM
Nice article..
Prakash TripathiPosted Mar 6, 2016, 11:13 PM
Thnx Kumaresh.
Prakash TripathiPosted Mar 6, 2016, 11:13 PM
Thnx Sr Karthiga.
Kumaresh RajalingamPosted Mar 6, 2016, 8:31 PM
good one
Kumaresh RajalingamPosted Mar 6, 2016, 8:31 PM
Nice explanation
Sr KarthigaPosted Mar 6, 2016, 8:21 PM
good one
Sr KarthigaPosted Mar 6, 2016, 8:21 PM
Nice explanation
Prakash TripathiPosted Jan 16, 2016, 11:52 PM
Thnx Ankur.
Prakash TripathiPosted Dec 30, 2015, 12:50 PM
Agree Rupesh. Thnx.
Rupesh KahanePosted Dec 30, 2015, 12:39 PM
very helpful... many times this pattern asked in interview
Ankur MistryPosted Dec 30, 2015, 9:02 AM
Nice one
Prakash TripathiPosted Dec 28, 2015, 9:07 AM
Thnx Humayun
Humayun Kabir MamunPosted Dec 14, 2015, 1:23 AM
Nice...
Prakash TripathiPosted Dec 6, 2015, 2:25 PM
Thnx sreenivasa.
Prakash TripathiPosted Dec 6, 2015, 2:25 PM
Thnx Santhakumar Munuswamy
sreenivasa kPosted Dec 4, 2015, 5:27 PM
good
Santhakumar MunuswamyPosted Dec 4, 2015, 6:59 AM
Good one