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 TripathiTOP 100Posted Jun 23, 2016, 9:53 AM EDT
Thanks a lot vidya sir.
Vidya Vrat AgarwalPosted Jun 23, 2016, 9:47 AM EDT
Great work Prakash Tripathi very well explained. Looking forward for more.
Prakash TripathiTOP 100Posted Apr 9, 2016, 6:40 AM EDT
Thnx Nagaraj.
Kuppurasu NagarajTOP 100Posted Apr 9, 2016, 6:35 AM EDT
Nice article..
Prakash TripathiTOP 100Posted Mar 6, 2016, 6:13 PM EST
Thnx Kumaresh.
Prakash TripathiTOP 100Posted Mar 6, 2016, 6:13 PM EST
Thnx Sr Karthiga.
Kumaresh RajalingamPosted Mar 6, 2016, 3:31 PM EST
good one
Kumaresh RajalingamPosted Mar 6, 2016, 3:31 PM EST
Nice explanation
Sr KarthigaPosted Mar 6, 2016, 3:21 PM EST
good one
Sr KarthigaPosted Mar 6, 2016, 3:21 PM EST
Nice explanation
Prakash TripathiTOP 100Posted Jan 16, 2016, 6:52 PM EST
Thnx Ankur.
Prakash TripathiTOP 100Posted Dec 30, 2015, 7:50 AM EST
Agree Rupesh. Thnx.
Rupesh KahanePosted Dec 30, 2015, 7:39 AM EST
very helpful... many times this pattern asked in interview
Ankur MistryTOP 100Posted Dec 30, 2015, 4:02 AM EST
Nice one
Prakash TripathiTOP 100Posted Dec 28, 2015, 4:07 AM EST
Thnx Humayun
Humayun Kabir MamunPosted Dec 13, 2015, 8:23 PM EST
Nice...
Prakash TripathiTOP 100Posted Dec 6, 2015, 9:25 AM EST
Thnx sreenivasa.
Prakash TripathiTOP 100Posted Dec 6, 2015, 9:25 AM EST
Thnx Santhakumar Munuswamy
sreenivasa kPosted Dec 4, 2015, 12:27 PM EST
good
Santhakumar MunuswamyPosted Dec 4, 2015, 1:59 AM EST
Good one