Design patterns: The Factory pattern
The factory pattern is one of the most widely used creational patterns. It's a creational pattern as its provides of the best ways to instantiate an object. With this pattern we create an object without exposing the underlying logic to the client and refer to the newly created object using a common interface.
The factory "method" pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses.
The Problem with "new"
When you use the new operator you are certainly instantiating a concrete class, tying your code to a concrete class can make it more fragile and less flexible when new use cases and scenarios arise. Let's illustrate this with an example in the following code snippet.
Enters the factory method
When we have several concrete classes beign instantiated, and the decision of which to instantiate is made at runtime depending on a set of conditions, the factory method shines in case like these, because code like this is difficult to change. by coding to an interface, we can insulate ourselves from the many changes that might happen to a system.
Because our code is written to an interface, it'll work with any new classes implementing that interface through polymorphism.
Let's use the example of a pizza store to illustrate the use of the factory method. Our pizza store implements a method called OrderPizza, where the client can order any types of pizza
领英推荐
Our store needs more than one type of pizza. We could modify our OrderPizza function to handle various pizza types like this.
The problem with this kind of impelentation is that when the requirement to add more pizza types will present itself, we are always going to dive into the code to make changes that meet the new requirement. This goes against the "Open Closed" design principle that states that an object can be opened for extension but closed for modification. We need to encapsulate the pizza object creation. To achieve this we will need a factory.
Factories handle the details of object creation. We will create a "PizzaFactory" object and the "OrderPizza" function will become a client of that object. Any time the Pizza store needs to make a new pizza it just ask the pizza factory to make one.
The implementation above is often referred to as the simple factory method. Sometimes there are cases where one might need to group more complex set of related objects for that we will recourse to the abstract factory pattern, we will explore this pattern in our next article.
Conclusion
In this article, we've explored the factory design pattern. This pattern is a widely used, creational design pattern that can be used in situations where multiple concrete implementations of an interface exist.
The pattern removes complex logical code that is hard to maintain, and replaces it with a design that is reusable and extensible. The pattern avoids modifying existing code to support new requirements. This is important because changing the existing code can introduce changes in behavior or subtle bugs.