1. Simple Factory
Also called the Static Factory method (Staticfactory) mode, but is not part of one of the 23 gof design patterns.
The essence of the simple factory model is that a factory class dynamically determines which product class should be created, based on the parameters passed in.
The beanfactory in spring is the embodiment of the simple factory pattern , which obtains the Bean object based on the passing of a unique identity, but whether it is created before the parameter is passed in or passed in to the parameter, depending on the situation.
2. Factory methods (Factory method)
Defines an interface for creating objects, letting subclasses decide which class to instantiate. The Factory method defers the instantiation of a class to its subclasses.
The Factorybean in spring is a typical factory method pattern. Such as:
3. Single case (Singleton)
Ensure that a class has only one instance and provides a global access point to access it.
The singleton pattern in spring completes the second half of the sentence, which provides the global access point beanfactory. However, the Singleton is not controlled from the constructor level, because spring manages arbitrary Java objects.
4. Adapter (Adapter)
Transforms the interface of one class into another interface that the customer wants. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.
In spring, there are examples of adapter patterns in the processing of AOP, as shown in:
Add some extra responsibilities to an object dynamically. For added functionality, the decorator mode is more flexible than generating subclasses.
5. Packaging Device (Decorator)
Add some extra responsibilities to an object dynamically. For added functionality, the decorator mode is more flexible than generating subclasses.
The wrapper pattern used in spring has two manifestations on the class name: one is that the class name contains wrapper, and the other is that the class name contains decorator. Basically, it's a dynamic way of adding some extra responsibilities to an object.
6. Agent (proxy)
Provides a proxy for other objects to control access to this object.
Structurally, the decorator model is similar, but the proxy is control, more like a function of limitations, and decorator is to increase responsibility.
Spring's proxy patterns are embodied in AOP, such as Jdkdynamicaopproxy and Cglib2aopproxy.
7. Observers (Observer)
Defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified and automatically updated.
The common place for observer patterns in spring is the implementation of listener. such as Applicationlistener.
8. Strategy (Strategy)
Define a series of algorithms, encapsulate them one by one, and make them interchangeable with each other. This mode allows the algorithm to be independent of the customers who use it.
In spring, when instantiating an object, use the strategy mode, as shown in:
In Simpleinstantiationstrategy, the following code illustrates the use of the policy pattern:
9. Template method
Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. The Template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.
Template method mode is generally required to inherit. Here we want to explore another understanding of template method. JdbcTemplate in spring does not want to inherit this class when using this class, because there are too many methods for this class, but we still want to use the stable, common database connection that JdbcTemplate already have, so what do we do? We can pull things out of change as a parameter into the JdbcTemplate method. But the change is a piece of code, and this code uses the variables in the JdbcTemplate. What to do? Then let's use the callback object. In this callback object, we define a method to manipulate the variables in the JdbcTemplate, so that we can implement this method, we will focus on the things that have changed. We then pass this callback object to JdbcTemplate, which completes the call. This may be another way to implement the template method without inheriting it.
Here is a specific example:
The Execute method in JdbcTemplate:
JdbcTemplate executes the Execute method:
Design Patterns in Spring use