Working with Abstract Factory Pattern

Hello friends.

Today I am here to go though another variant of Factory design pattern called Abstract Factory. 

In case, you have not had a look at my previous article about Factory pattern, I would suggest you to look for them in ARCHIVES section.

Before talking about its implementation let’s try to understand the purpose of the abstract factory pattern

Abstract factory pattern is next level to Factory Method 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 Factory Method 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();  
  • }  

Now since we need to show the car details by brand, we need to create concrete product classes using the interface defined above.

  • 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”;  
  •     }  
  • }  

Now we need to define an abstract factory interface, which can accommodate the abstract product interfaces created to define car details as in the following:

  • interface ICarFactory  
  • {  
  •    IEconomy GetEconomy();  
  •    IPremium GetPremium();  
  • }  

Now the time has come to create the concrete family of classes to support different brands, of course by implementing the ICarFactory.

  • 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();  
  •     }  
  • }  

And now we have reached a point where we can use the setup created so far. Let’s do it as in the following:

  • 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()); 

Output:

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

Output:

Output

 And here goes the class diagram. 

As you can see in the above code that client is getting the required car factory object by just creating the instance of respective car factory brand. And then it gets the required interface object by calling it’s respective car factory method in order to further call the product interface methods i.e. ShowEconomy and ShowPremium. 

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