Factory Method Design Pattern

Factory Method Design Pattern

In today’s episode of Software Design Patterns you will learn everything about the Factory Method Design Pattern. The Factory Method is a creational pattern. That means that the pattern has something to do with object creation. There are different types of design patterns. The other types are structural and behavioral. Those types will have a dedicated blog post in the future.

For reference – the last article covered the Singleton Design Pattern – which is also a creational pattern. All the examples, source code and unit tests are in this GitHub repository.

What’s the purpose of the Factory Method Design Pattern?

The Factory Method is a very common pattern, but strongly misused. I have seen many examples where methods were named factory, but did something completely different. The quote below – taken from the Gang of Four – is very precise about the Factory Method.

The Factory Method Design Pattern defines an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. [1]

Don’t fall into the trap and use it for everything. If you are not sure about when to use, you probably don’t need it anyway.

How does the Factory Method looks like?

In the picture below you find the Unified Modeling Language (UML) diagram.

Factory Method Design Pattern UML
Factory Method Design Pattern UML

As mentioned at the beginning you find all the code examples in a working project on GitHub.

In this example we have an interface ICar.cs, which provides basic car operations like accelerate and break. In addition to that we have an GetName() method, were the concrete implementation should return the actual name of the car.

Our Factory, called CarFactory.cs, is responsible for creating instances of concrete cars. We provide a dedicated CreateCar(string type) method, where the desired type can be passed as an argument. The method maps the type to a concrete implementation. This can be done via multiple if clauses or a switch statement. Anyway, if there is no matching type we throw an NotSupportedException to make it clear that the factory does not support the desired type.

To see the proper use of the CarFactory look into the test project.

If you have any questions or comments feel free to leave them below.

That’s it. Happy coding as always :).

References

[1] –Design patterns, software engineering, object-oriented programming

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.