The Factory method is a creational design pattern that provides an interface for creating objects without specifying their concrete classes. It defines a method that we can use to create an object instead of using its constructor. The important thing is that the subclasses can override this method and create objects of different types.

In this article, we are going to show you how to implement a Factory method design pattern. Furthermore, we are going to learn how to use the Factory method refactoring technique to hide constructor and use our own method to expose it.

We are going to start with the simple factory implementation, and we’re gradually going to improve to achieve a very usable, but readable factory for our objects.

The source code is available at the Factory Method – Source Code.

For the main page of this series check out C# Design Patterns.

This article is divided into the following sections:

Factory Method Implementation

To implement a Factory method pattern, we are going to create a simple Air conditioner application. Our app will receive an input from a user and based on that input will trigger a required action (cooling or warming the room). So let’s start with an interface:

Now, we need concrete classes to implement this interface:

Great. We have prepared our base functionality. Now let’s create a factory creator for these objects.

Factory Classes

We are going to start with the AirConditionerFactory abstract class:

This abstract class provides an interface for object creation in derived classes. That being said, let’s implement our concrete creator classes:

Excellent. Now we are ready to start using our Factory methods. In many examples, we can see the switch statement which switches through the user’s input and selects the required factory class.

That works just fine.

But imagine if we have a lot of factory classes, which is quite common in large projects. That would lead to a quite big switch case statement which is quite unreadable. Therefore, we are going to use another approach.

Factory Execution

Let’s start with a simple enumeration to define air conditioner actions:

To continue on, we are going to create the AirConditioner class where the user can specify the type of action and execute the appropriate factory. Our concrete factories inherit from the abstract class and we are going to use that structure in our further implementation:

This is a better way of implementing our factory execution than using a switch-case statement. But we can do it in another more dynamic way, where we don’t have to manually add action and factory creator for each action. Let’s introduce reflection into our project:

Whether we choose the first or the second example, the result should be the same:

Factory Method - factory class

There is one more thing we need to add to this class. And that is the method which is going to execute appropriate creation:

Now, we just have to make a call from a client. In a real-world project, we would surely check first for the current temp and then just have a factory to decide whether we should lower it or make it higher. But for sake of simplicity, we are just going to make a simple call towards our AirCOnditioner class:

Our result should be as expected:

Finished Factory Method

Using the Factory Method Refactoring Technique

We can use the Factory method to replace our constructor while creating an object. If our constructor consists of lots of code, we should replace it with the factory method. Furthermore, we can have multiple factory methods with meaningful names and parameter names as well which replace a single constructor.

This improves code readability a lot.

Finally, it helps us to implement a chaining syntax.

So let’s modify the AirConditioner class with the factory method:

Our client call should be modified as well:

Excellent. The result should be the same, but now we are using the factory method refactoring technique.

Conclusion

By reading this article, we have learned:

  • How to implement the Factory Method design pattern into the application
  • Several ways of replacing switch-case statements by using a dictionary or reflection
  • How to refactor your code by using the Factory Method Refactoring Technique

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.