C# Design Patterns: Factory and Abstract Factory Pattern

Biswajit Sahoo
3 min readNov 21, 2022

--

Factory and Abstract Factory.C# Design Patterns.

Thanks for visiting this article. Please scroll down for the bite-sized explanation of this pattern(s).

Factory and Abstract Factory belong to Creational Design Patterns and provide a better way to create instances of “classes” or “families of classes” present in your code base or project.

Definition:

  1. Factory Method: Creates an instance of several derived classes
  2. Abstract Factory: Creates an instance of several families of classes

Factory Method

It defines an interface to create an object. And subclass implementing this interface can decide which class object to instantiate.

Example

Note: The code examples may or may not have syntax, or compilation errors, and are for representational purposes only

Consider we have an abstract class of “AudioManager”, and our project has different types of “AudioManager” that handle the playing of different audio clips. e.g. “SubAudioManagerA”, “SubAudioManagerB”….

And we want to create multiple instances of “AudioManager” without worrying about the type of the “SubAudioManager” to instantiate.

And for that, we can :

  1. create a “Factory” interface or an abstract class and,
  2. a set of subclasses implementing the interface, that can instead create those instances for us.

And our main method can look like this.

Abstract Factory Method

Provides an interface for creating related objects without specifying their concrete classes.

In simple words, unlike the “Factory Method”, design pattern, where we have specified concrete-type factories for creating the respective instances, here we can pass the type of interface we need without having to specify which concrete class type we need to instantiate.

Example

Note: The code examples may or may not have syntax, or compilation errors, and are for representational purposes only

Let’s extend the same example we have with Audio Managers to understand the use of this pattern.

Now, as we have defined the various “AudioManager Factories” to instantiate respective audio managers to play respective clips, we will use them to:

  1. play “AudioClip A” when a user enters the app,
  2. and play “AudioClip C”, when a user exits the app.

So our main method will look like this, using the current implementation

But what if! we don’t like the “Audio Clip A” in the Intro and want to switch the audio to “Audio Clip C” from Outro to Intro and vice-versa.

In that case, we have to change the concrete implementation of the constructors in both classes (i.e IntroAudio and OutroAudio) to be able to accept the appropriate factory instances they need to play the audio clips.

And that’s the exact issue we can solve with Abstract Factory Pattern. Where we can receive the abstracted factory type, instead of the concrete factory instance, as a parameter to the constructors.

Hope this helped with giving you some clarity on how these patterns can be implemented and what is the relation between these two creational patterns.

On the last note. If you like the article please do like and share, and also feel free to provide your valuable comments and feedback.

--

--

Biswajit Sahoo

Professional XR Developer and Gamer. Writes about system designs, and design patterns in C#.