1. Simple factory

Also known as the StaticFactory Method pattern, but not one of the 23 GOF design patterns.

The essence of the simple factory pattern is that a factory class dynamically decides which product class should be created based on the parameters passed in.

The BeanFactory in Spring is an embodiment of the simple factory pattern. Bean objects are obtained by passing in a unique identifier, but whether this is created after or before the parameters are passed in depends on the case.

2. Factory Method

Define an interface for creating objects and let subclasses decide which class to instantiate. The Factory Method delays the instantiation of a class to its subclasses.

Factorybeans in Spring are the typical factory method pattern. The diagram below:

3. Singleton

Ensure that a class has only one instance and provide a global access point to access it.

The singleton pattern in Spring completes the latter part of the sentence, providing a global access point to the BeanFactory. But there is no control over singletons at the constructor level, because Spring manages arbitrary Java objects.

4. Adapter

Translate the interface of a class into another interface that the customer wants. The Adapter pattern enables classes to work together that would otherwise not work together due to interface incompatibilities.

Spring has examples of the Adapter pattern in its handling of AOP, as shown in the figure below

Because the Advisor chain requires a MethodInterceptor object, the Advice in each Advisor needs to be adapted to the corresponding MethodInterceptor object.

5. Decorators

Add some extra responsibilities to an object dynamically. The Decorator pattern is more flexible in terms of adding functionality than subclassing.

The Wrapper pattern used in Spring has two representations on class names: one with a Wrapper in the class name and one with a Decorator in the class name. Basically, adding some extra responsibilities to an object on the fly.

6. Proxy

Provide a proxy for other objects to control access to that object.

The structure is similar to the Decorator pattern, but a Proxy is control, more like a restriction on functionality, whereas a Decorator adds responsibility.

Spring’s Proxy pattern is reflected in AOP, such as JdkDynamicAopProxy and Cglib2AopProxy.

7. Observers

Defines a one-to-many dependency between objects in which all dependent objects are notified and automatically updated when an object’s state changes.

A common place in Spring for the Observer pattern is the listener implementation. Such as ApplicationListener.

8. Strategy

Define a set of algorithms, encapsulate them one by one, and make them interchangeable. This pattern allows the algorithm to change independently of the customers using it.

Spring uses the Strategy pattern when instantiating objects, as shown in the following figure:

The following code in SimpleInstantiationStrategy illustrates the usage of the strategy pattern:

9. Template Method

Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method allows subclasses to redefine specific steps of an algorithm without changing its structure.

The Template Method pattern is generally inherited. Here I want to explore another understanding of the Template Method. JdbcTemplate in Spring does not want to inherit from this class because it has too many methods, but we still want to use the stable, common database connection that JdbcTemplate already has. So what do we do? We can pull out the change and pass it as a parameter to the JdbcTemplate method. But what changes is a piece of code, and that code uses variables in the JdbcTemplate. How to do? So let’s use callback objects. Define a method in the callback object that manipulates the variables in the JdbcTemplate, and we implement this method to concentrate the changes here. We then pass the callback object to the JdbcTemplate to complete the call. This might be another implementation of the Template Method that doesn’t need to inherit.

Here’s a concrete example:

Execute from JdbcTemplate:

JdbcTemplate executes the execute method: