Preface
The eight design patterns in the Spring framework are:1. Simple factory. 2. Factory method. 3. Singleton mode. 4. Adapter module
Mode. 5. Decorator mode. 6. Agent mode. 7. Observer mode. 8. Strategy mode.
1. Simple factory
BeanFactory in Spring is the embodiment of the simple factory pattern. passes in a unique identifier To get the Bean object,
But whether to create it after passing in the parameters or before passing in the parameters depends on the specific situation.
2. Factory method
The bean that implementsFactoryBean interface is a type of bean called factory. Its characteristic is that Spring will use getBean() to call
When the user obtains the bean, the getObject() method of the bean will be automatically called, so what is returned is not the factory bean, but
ThisThe return value of the bean.getOjbect() method.
3. Singleton mode
Spring Dependency Injection Bean instanceThe default is singleton.
Spring's dependency injection (including lazy-init method) all occurs in getBean of AbstractBeanFactory. getBean
doGetBean method calls getSingleton to create a bean.
4. Adapter mode
Adapter in SpringMVCHandlerAdatper.
HandlerAdatper makes it easy to extend Handler, just add a new Handler and a corresponding
HandlerAdapter。
Therefore, Spring defines an adaptation interface so that each type of Controller has a corresponding adapter implementation class, allowing the adapter to
executes the corresponding method for the controller. In this way, when extending the Controller, only needs to add an adapter class
SpringMVC has been extended.
5. Decorator mode
The wrapper pattern used in Spring has two manifestations in the class name:
One is that the class name contains Wrapper, and the other is that the class name contains Decorator .
Dynamically add some additional responsibilities to an object.
In terms of adding functionality, the Decorator pattern is more flexible than generating subclasses.
6. Agent mode
SpringThe bottom layer of AOP is dynamic proxy Implementation of the pattern.
7. Observer mode
Spring is based onobserver pattern and implements its own event mechanism, which isevent-driven model< /span>, the event-driven model is usually also considered
Resolve intoobserver or publish/subscribe model.
8. Strategy mode
Spring frameworkResource access Resource interface. This interface provides stronger resource access capabilities, and the Spring framework itself has a lot of
The Resource interface is used to access underlying resources.
The Rsource interface isan abstraction of a specific resource access strategy, and is also the interface implemented by all resource access classes.
The Resource interface itself does not provide implementation logic for accessing any underlying resources.For different underlying resources, Spring will provide
Provides different Resource implementation classes, and different implementation classes are responsible for different resource access logic.