Factory pattern is also one of the more commonly used 23 design patterns, which can be divided into: creation pattern, structural pattern and behavior pattern. Factory pattern is classified as creation pattern.

In general, factory mode can be divided into simple factory mode, factory method mode and abstract factory mode. Simple factory mode is not strictly a kind of factory mode, but a programming habit. In this article, simple factories are used as a model for comparative understanding

Simple Factory model

The Simple Factory pattern is a Factory class that dynamically decides which instance of a product class that inherits from a parent class or interface should be created based on the parameters passed in. The simple Factory pattern is also known as the Static Factory Method pattern, which belongs to the class creation pattern.

Let’s demonstrate the simple factory pattern with a simple example. We first define an interface for drawing shapes, Shape, which defines a method draw() that can be drawn. Circle, Square, and Rectangle all implement Shape classes and then implement their own draw() methods.

We’ll return the various implementations of Shape through a factory class called ShapeFactory.

In addition to passing in a type of type, there is another way to pass in a Class that needs to create the type, and then return a Shape directly by reflection.

The above two different methods of creating objects are similar in that each creates an object of a different type through an input parameter.

The benefit factory class is key to the entire pattern. Contains the logical judgment necessary to determine which object of a concrete class should be created based on the information given outside. By using the factory class, the outside world is freed from the embarrassment of directly creating concrete product objects and is simply responsible for “consuming” them. Regardless of how the objects are created and organized. It makes clear their respective responsibilities and rights, which is conducive to the optimization of the whole software architecture.

Because the factory class centralizes the creation logic of all instances, it violates the principle of highly cohesive responsibility allocation and centralizes all creation logic into one factory class. The classes it can create can only be considered in advance, and if new classes need to be added, the factory class needs to be changed. As the number of specific product classes in the system increases, there may be a requirement for the factory class to create different instances based on different conditions. This kind of judgment to the condition and the judgment to the specific product type are interlaced together, it is difficult to avoid the spread of module function, which is very unfavorable to the maintenance and expansion of the system.

Usage scenarios

  • The factory class is responsible for creating fewer objects;
  • The customer only knows the parameters passed into the factory class and doesn’t care how the object is created (logically);

Factory method pattern

Let subclasses decide which class to instantiate by defining an interface for creating objects. The factory method delays instantiation of a class to subclasses.

The factory method class diagram is as follows:

We know that in both Android and Java Swing frameworks, the theme of a view can be set by itself, and ScrollBar looks different in dark and light themes. Assume that the Theme’s entity class is a Theme class, and then define an interface ThemeFactory for creating the Theme. The interface has a method createTheme() that returns a Theme instance. ScrollBar implements the Shape interface to override the DRAW method, similar to the simple factory definition above.

Based on the above class diagram, the abstract layer of the Theme is simplified in this example and the ConcreteProduct is used directly as the Theme.

By implementing ThemeFactory, we define two themes: a LightThemeFactory light theme and a DarkThemeFactory dark theme.

Next, test the implementation of the ScrollBar.

If our theme is through the LightThemeFactory, the output would be a Draw Light scrollbar.

The factory method pattern is one of the most used in the factory pattern, and there is a typical engineering method definition in the JDK:

Android AsyncTask and ThreadPoolExecutor have implementations of this factory method, so the code is not posted here.

The subclass implementation of ThemeFactory looks a lot like the simple factory pattern. The simple factory pattern does everything in one place, whereas the factory approach creates a framework and lets subclasses decide how to implement it. For example, the createTheme() method in the factory method provides a generic framework for creating a Theme, and the createTheme() method relies on the concrete class created by the factory method and creates a concrete Theme. You can decide what Theme to create by implementing the ThemeFactory. A simple factory approach can encapsulate object creation, but it does not have the flexibility of a factory approach because a simple factory cannot change the product being created.

In the above example of the factory method pattern, a factory is defined using an interface and may not be used very often in real development. But the example introduced in the book “Head First Design Patterns” is more realistic. For those interested, the book designs PizzaStore as an abstract class and defines the creation of Pizza as an abstract method, letting the subclass decide how to produce Pizza.

Usage scenarios

  • When a class does not know the class of the object it must create.
  • When a class wants its subclasses to specify the objects it creates.

Abstract Factory pattern

In the factory approach described above, only one product is generated in the factory. The abstract factory pattern is a step further of the factory approach, in which a factory class can create not just one product, but a set of products.

Again, here is an example of a UI presentation style. In the factory method mode above, ScrollBar can display different styles according to different themes. The composition of ScrollBar is more detailed. It is assumed that ScrollBar needs to draw its Background Background and Border. We then define a ThemeFactory, which is slightly different from the factory method by creating a Background and Border.

Then define the ScrollBar, in the ScrollBar draw() method only need to call the Background and Border draw() method to complete the drawing.

ThemeFactory implementation class DarkThemeFactory:

Usage scenarios

  • A system is independent of the creation, composition, and presentation of its products.
  • When a system is to be configured by one of multiple production systems.
  • When you want to emphasize the design of a series of related product objects for joint use.
  • When you provide a product class library and only want to display their interfaces rather than implementations.

All factory patterns are used to encapsulate object creation. Factory methods primarily delegate object creation to subclasses that implement factory methods to create objects. Abstract factories use object composition, which provides an interface for creating families of related or dependent objects without specifying explicit classes.

The resources

Abstract Factory Patterns for JAVA design patterns

Java Factory Pattern – Simple Factory pattern

Deep understanding of the factory model