Factory Method
Factory method is a creational design pattern which solves the problem of creating product objects without specifying their concrete classes.
Intent
Factory Method is a creational design patterns that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
Problem
Imagine that your first version of a logistics app handle transportation only by trucks, so the bulk of your code lives inside the truck
class. After a while, you receive dozens of requests from sea transportation companies to incorporate sea logistics into the app. So you should add a ship
class into the app, which you need to make the entire codebase changed. When this situation happened again, the entire codebase changed again. As a result, you will end up with pretty nasty code.
Solution
The Factory Method pattern suggests that you replace direct object construction calls (using the new
operator) with calls to a special factory method.
Now you can override the factory method in a subclass and change the class of products being created by the method.
There’s a slight limitation though: subclasses may return different types of products only if these products have a common base class or interface. Also, the factory method in the base class should have its return type declared as this interface.
Structure
The Product declares the interface, which is common to all objects that can be produced by the creator and its subclasses.
Concrete Products are different implementations of the product interface.
The Creator class declares the factory method that returns new product objects. It’s important that the return type of this method matches the product interface.
You can declare the factory method as
abstract
to force all subclasses to implement their own versions of the method. As an alternative, the base factory method can return some default product type.Note, despite its name, product creation is not the primary responsibility of the creator. Usually, the creator class already has some core business logic related to products. The factory method helps to decouple this logic from the concrete product classes.
Concrete Creators override the base factory method so it returns a different type of product.
Note that the factory method doesn’t have to create new instances all the time. It can also return existing objects from a cache, an object pool, or another source.
Applicability
- Use the Factory Method when you don’t know beforehand the exact types and dependencies of the objects your code should work with.
- Use the Factory Method when you want to provide users of your library or framework with a way to extend its internal components.
- Use the Factory Method when you want to save system resources by reusing existing objects instead of rebuilding them each time.
Factory Method in Java
buttons
Button.java: Common product interface
1 | /** |
HtmlButton.java: Concrete product
1 | /** |
WindowsButton.java: One more concrete product
1 | /** |
factory
Dialog.java: Base creator
1 | /** |
HtmlDialog.java: Concrete creator
1 | /** |
Windows.Dialog.java: One more concrete creator
1 | /** |
Demo.java: Client code
1 | /** |