Abstract Factory Design Pattern in C#

Abstract Factory
Share this

In my previous article Factory Method Design Pattern in C#, I had explained about Factory Method Design Pattern. In this article, I will explain Abstract Factory Design Pattern in C#.

Source Code: Complete source code can be downloaded from the following GitHub repo –
https://github.com/sudipta-chaudhari/AbstractFactoryDesignPattern_CSharp

This design pattern is also a Creational Design Pattern like the Factory Method Design Pattern.

Abstract Factory Pattern expands the Factory Method Design Pattern by unifying similar Factory Method classes under a unified abstract class. Thus, it can also be referred to as Factory of Factories. In a nutshell, Abstract Factory Pattern unifies related Factory Method Patterns as displayed in the high level UML diagram.

Abstract Factory Factory of Factories

As the Factory Pattern was responsible for object creation without exposing instantiation logic to the client, the Abstract Factory Pattern is responsible for creating a set of dependent or related objects without exposing instantiation logic to the client.

Let us take the Factory Method Design Pattern code from our previous article to build the code for Abstract Factory Design Pattern.

In the previous article, I had created a ButtonFactory. In this article, I will create a TextBoxFactory and a UIFactory that houses both the ButtonFactory and TextBoxFactory.

Let’s begin by creating two UI controls – TextBox and RichTextBox. Create two concrete classes TextBox.cs and RichTextBox.cs with a Display() method as shown follows. Both these classes need to implement this interface IControl as shown follows.

Next create a TextBoxFactory.cs class as shown follows in the same way as ButtonFactory.cs class was created in the previous article Factory Method Design Pattern.

Now create the AbstractFactory class UIFactory.cs as follows.

The method GetUIElement within this class accepts the first parameter which selects from the two factories – ButtonFactory and TextBoxFactory. The second parameter selects the corresponding concrete type from each factory Button or RadioButton and TextBox or RichTextBox and creates the corresponding object respectively.

Now, in the client which is Program.cs class having the Main() method, code is shown as follows:-

First the user is prompted to select the from the two factories – ButtonFactory and TextBoxFactory. Next the user is prompted to select the corresponding concrete type from each factory Button or RadioButton and TextBox or RichTextBox respectively.

Finally, the GetUIElement method from the Abstract Factory class named UIFactory is invoked by passing the two parameters mentioned above to get the Factory object. Client only refers to IControl type and Abstract Factory class UIFactory also gives the same type of reference. Client is completely detached from the concrete Factory classes. When new Factories are added like ScrollBarFactory with concrete classes like HorizontalScrollBar and VerticalScrollBar etc, we don’t need to recompile the client again.

Output of the above code looks as follows.

Abstract Factory Output1

Abstract Factory Output2

Abstract Factory Output3

Abstract Factory Output4

Class Diagram

Abstract Factory Class Diagram

UML diagram

Abstract Factory UML Diagram

This concludes the article – Abstract Factory Design Pattern in C#. I hope you liked this article. If you have any comments, questions or suggestions, please post them in the comments section below this article. I will try to respond at my earliest or somebody else reading the article and your comment will try to respond.


Share this

Leave a Reply