Abstract Factory Design Pattern In C#

Introduction

In my previous article, we covered Factory Method Design Pattern In C#. In this article, we will cover another design pattern in software design of C# application development, Abstract Factory.

Abstract Factory pattern also falls under Creational Pattern of Gang of Four (GoF) Design Patterns.

Background

Before learning Abstract Factory pattern, I just want to share a little about "Gang of Four (GoF)" to which the Abstract Factory pattern belongs.

Who are the Gang of Four?

The Gang of Four are the authors of the book, "Design Patterns: Elements of Reusable Object-Oriented Software". This important book describes various development techniques and pitfalls in addition to providing 23 object-oriented programming design patterns. The four authors are Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides.

Now, let's move on to the Abstract Factory design pattern.

In this article, I have tried to cover the concept of Abstract Factory design pattern and the ways of implementing Abstract Factory design pattern.

What is it?

An interface for creating families of related or dependent objects without specifying their concrete classes. We can say it is just an object maker which can create more than one type of object.

The object it produces is known to the client only by that object's interface, not by the object's actual concrete implementation.

When to use it?

We use it when we have a requirement to create a set of related objects, or dependent objects which must be used together as families of objects. Concrete classes should be decoupled from clients.

How does it differ from Factory Method?

First of all, both of them fall under Creational category and it means both will solve the problem relating to object creation. Factory Method and Abstract Factory design pattern are about creating objects.

Factory Method Design Pattern

Here, we define an interface which will expose a method which will create objects for us. Return type of that method is never a concrete type; rather, it will be some interface (or may be an abstract class).

  • Creates object through inheritance
  • Produce only one product
  • Implements code in the abstract creator that makes use of the concrete type that sub class produces

Abstract Factory Design Pattern

Here, we define an interface which will create families of related or dependent objects. In simple words, interface will expose multiple methods each of which will create some object. Again, here method return types will be generic interfaces. All these objects will together become part of some important functionality.

  • Creates object through composition
  • Produce families of products
  • Concrete factories implements factory method to create product

UML Class Diagram

UML Class Diagram

The classes and objects participating in the above UML class diagram are as follow.

  1. AbstractFactory
    This is an interface for operations which is used to create abstract product.

  2. ConcreteFactory
    This is a class which implements the AbstractFactory interface operations to create concrete products.

  3. AbstractProduct
    This declares an interface for a type of product object

  4. Product
    This defines a product object to be created by the corresponding concrete factory also implements the AbstractProduct interface

  5. Client
    This is a class which uses AbstractFactory and AbstractProduct interfaces to create a family of related objects.

Now, let’s understand this with a real world example.

The example here has an implementation of an Abstract Factory as an Interface IMobilePhone that has methods that can create a Smart Phone object and a Normal Phone object. The client codes against IMobilePhone and gets ISmartPhone and INormalPhone interfaces.

In case of "Nokia", it creates a family of Nokia objects (SmartPhone and NormalPhone) and in case of "Samsung", creates a family of Samsung objects (SmartPhone and NormalPhone).

The client doesn't care which object (Nokia SmartPhone and NormalPhone or Samsung SmartPhone and NormalPhone), IMobilePhone interface returns as it codes against ISmartPhone and INormalPhone interface.

Who is what?

The classes and objects participating in the above class diagram can be identified as shown below.

  • AbstractFactory- IMobilePhone
  • ConcreteFactory - Nokia, Samsung
  • AbstractProduct- ISmartPhone, INormalPhone
  • Product- NokiaPixel, Nokia1600, SamsungGalaxy, SamsungGuru
  • Client- MobileClient

Here are the code blocks for each participant

  1. AbstractFactory

    1. namespace AbstractFactoryDesignPatternInCSharp  
    2. {  
    3.     /// <summary>  
    4.     /// The 'AbstractFactory' interface.  
    5.     /// </summary>  
    6.     interface IMobilePhone  
    7.     {  
    8.         ISmartPhone GetSmartPhone();  
    9.         INormalPhone GetNormalPhone();  
    10.     }  
    11. }  
  1. ConcreteFactory

    Nokia

    1. namespace AbstractFactoryDesignPatternInCSharp  
    2. {  
    3.     /// <summary>  
    4.     /// The 'ConcreteFactory1' class.  
    5.     /// </summary>  
    6.     class Nokia : IMobilePhone  
    7.     {  
    8.         public ISmartPhone GetSmartPhone()  
    9.         {  
    10.             return new NokiaPixel();  
    11.         }  
    12.   
    13.         public INormalPhone GetNormalPhone()  
    14.         {  
    15.             return new Nokia1600();  
    16.         }  
    17.     }  
    18. }  
    Samsung
    1. namespace AbstractFactoryDesignPatternInCSharp  
    2. {  
    3.     /// <summary>  
    4.     /// The 'ConcreteFactory2' class.  
    5.     /// </summary>  
    6.     class Samsung : IMobilePhone  
    7.     {  
    8.         public ISmartPhone GetSmartPhone()  
    9.         {  
    10.             return new SamsungGalaxy();  
    11.         }  
    12.   
    13.         public INormalPhone GetNormalPhone()  
    14.         {  
    15.             return new SamsungGuru();  
    16.         }  
    17.     }  
    18. }  
  1. AbstractProduct

    ISmartPhone

    1. namespace AbstractFactoryDesignPatternInCSharp  
    2. {  
    3.     /// <summary>  
    4.     /// The 'AbstractProductA' interface  
    5.     /// </summary>  
    6.     interface ISmartPhone  
    7.     {  
    8.         string GetModelDetails();  
    9.     }  
    10. }  
    INormalPhone
    1. namespace AbstractFactoryDesignPatternInCSharp  
    2. {  
    3.     /// <summary>  
    4.     /// The 'AbstractProductB' interface  
    5.     /// </summary>  
    6.     interface INormalPhone  
    7.     {  
    8.         string GetModelDetails();  
    9.     }  
    10. }  
  1. Product

    NokiaPixel

    1. namespace AbstractFactoryDesignPatternInCSharp  
    2. {  
    3.     /// <summary>  
    4.     /// The 'ProductA1' class  
    5.     /// </summary>  
    6.     class NokiaPixel : ISmartPhone  
    7.     {  
    8.         public string GetModelDetails()  
    9.         {  
    10.             return "Model: Nokia Pixel\nRAM: 3GB\nCamera: 8MP\n";  
    11.         }  
    12.     }  
    13. }  
    SamsungGalaxy
    1. namespace AbstractFactoryDesignPatternInCSharp  
    2. {  
    3.     /// <summary>  
    4.     /// The 'ProductA2' class  
    5.     /// </summary>  
    6.     class SamsungGalaxy : ISmartPhone  
    7.     {  
    8.         public string GetModelDetails()  
    9.         {  
    10.             return "Model: Samsung Galaxy\nRAM: 2GB\nCamera: 13MP\n";  
    11.         }  
    12.     }  

    Nokia1600
    1. namespace AbstractFactoryDesignPatternInCSharp  
    2. {  
    3.     /// <summary>  
    4.     /// The 'ProductB1' class  
    5.     /// </summary>  
    6.     class Nokia1600 : INormalPhone  
    7.     {  
    8.         public string GetModelDetails()  
    9.         {  
    10.             return "Model: Nokia 1600\nRAM: NA\nCamera: NA\n";  
    11.         }  
    12.     }  
    13. }
    14.  
    SamsungGuru
    1. namespace AbstractFactoryDesignPatternInCSharp  
    2. {  
    3.     /// <summary>  
    4.     /// The 'ProductB2' class  
    5.     /// </summary>  
    6.     class SamsungGuru : INormalPhone  
    7.     {  
    8.         public string GetModelDetails()  
    9.         {  
    10.             return "Model: Samsung Guru\nRAM: NA\nCamera: NA\n";  
    11.         }  
    12.     }  
    13. }  
  1. Client
    1. namespace AbstractFactoryDesignPatternInCSharp  
    2. {  
    3.     /// <summary>  
    4.     /// The 'Client' class  
    5.     /// </summary>  
    6.     class MobileClient  
    7.     {  
    8.         ISmartPhone smartPhone;  
    9.         INormalPhone normalPhone;  
    10.   
    11.         public Client(IMobilePhone factory)  
    12.         {  
    13.             smartPhone = factory.GetSmartPhone();  
    14.             normalPhone = factory.GetNormalPhone();  
    15.         }  
    16.   
    17.         public string GetSmartPhoneModelDetails()  
    18.         {  
    19.             return smartPhone.GetModelDetails();  
    20.         }  
    21.   
    22.         public string GetNormalPhoneModelDetails()  
    23.         {  
    24.             return normalPhone.GetModelDetails();  
    25.         }  
    26.     }  
    27. }  
    Factory Pattern Client Demo
    1. using System;  
    2.   
    3. namespace AbstractFactoryDesignPatternInCSharp  
    4. {  
    5.     /// <summary>  
    6.     /// Abstract Factory Pattern Demo  
    7.     /// </summary>  
    8.     class Program  
    9.     {  
    10.         static void Main()  
    11.         {  
    12.             IMobilePhone nokiaMobilePhone = new Nokia();  
    13.             MobileClient nokiaClient = new MobileClient(nokiaMobilePhone);  
    14.   
    15.             Console.WriteLine("********* NOKIA **********");  
    16.             Console.WriteLine(nokiaClient.GetSmartPhoneModelDetails());  
    17.             Console.WriteLine(nokiaClient.GetNormalPhoneModelDetails());  
    18.               
    19.             IMobilePhone samsungMobilePhone = new Samsung();  
    20.             MobileClient samsungClient = new MobileClient(samsungMobilePhone);  
    21.   
    22.             Console.WriteLine("******* SAMSUNG **********");  
    23.             Console.WriteLine(samsungClient.GetSmartPhoneModelDetails());  
    24.             Console.WriteLine(samsungClient.GetNormalPhoneModelDetails());  
    25.   
    26.             Console.ReadKey();  
    27.         }  
    28.     }  
    29. }  

Output

Output

Summary

In this article, we have covered the basic concept of Abstract Factory design pattern and how to implement this in real world application development. I have attached the sample code for your reference.

I hope you enjoyed this article. Please share your valuable suggestions and feedback.

Happy Coding!!!

Up Next
    Ebook Download
    View all
    Learn
    View all