What is the Factory Method Pattern?
Wikipedia describes it as:
In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.
The Factory Pattern moves and abstracts implementation details of similar classes or even a single class or struct that has many ways to implement or instantiate it and doesn’t require the user of that object to know about all the dependencies or how to construct it.
If the requirements for creating an object changed, and you were manually creating that object all over your application, you would have to update all usages of it if there was ever a change. Instead, leave that work up to the class that is building the object for you. Define a separate operation/method for creating the object/s.
This also enables us to create subclasses that can redefine how an object is created or even which subtype to instantiate.
Creating an object often requires complex processes not appropriate to include within a composing object. The object’s creation may lead to a significant duplication of code, may require information not accessible to the composing object, may not provide a sufficient level of abstraction, or may otherwise not be part of the composing object’s concerns. The factory method design pattern handles these problems by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.
Let’s look at a simple example where we have a struct similar to .NETs TimeSpan
object. It is just going to hold a number of ticks
, but we may want to build up this Duration wrapper from several different lengths or units.
Examples are in C#.
public struct MyDuration {
private readonly long ticks;
public long Ticks { get { return ticks; } }
// public MyDuration(long ticks) {
// this.ticks = ticks;
// }
private MyDuration(long ticks) {
this.ticks = ticks;
}
public static MyDuration FromTicks(long ticks) {
return new MyDuration(ticks);
}
public static MyDuration FromMilliseconds(long milliseconds) {
return new MyDuration(milliseconds * 10000);
}
public static MyDuration FromSeconds(long seconds) {
return new MyDuration(seconds * 10000 * 1000);
}
}
public void main() {
MyDuration duration = MyDuration.FromSeconds(5);
// duration.Ticks == 50000000
}
You will also notice that I used a private constructor. Mixing the Factory Pattern with a traditional constructor can be confusing.
new MyDuration(5) //What does that mean?
Instead, use a private constructor, and use the same pattern for all use cases.
C#6 allows us to use named parameters, but I still like to lean towards consistency.
new MyDuration(ticks: 5)
Reminder, Pluralsight has many courses on different design patterns in many different languages and implementations, and they have a FREE trial!