Interviewer: Resume writing familiar with Spring source code? Do you know what design patterns it uses?

As a classic framework in the industry, Spring can be regarded as a model in the industry both in terms of architecture design and code writing. Okay, not much to say, start today's content.

There are nine design patterns commonly used in spring.

Template Method

Spring's jdbc template, I really admire the subtlety of the Spring source code, and it is extremely classic.

Spring is really a masterpiece of design patterns, and it is used thoroughly. The template method (template method) is widely used in spring, such as: jdbcTemplate, hibernateTemplate, JndiTemplate and some surrounding packages, etc., undoubtedly use the template mode, but spring does not simply use the template method, but on this basis Made an innovation, used with callback (callback), it is extremely flexible.

The so-called template board style is to define the main process of the algorithm in the parent class, and delay some personalized steps to the subclass to implement. The parent always controls the initiative of the entire process, and the subclass just assists the parent to implement some Customizable steps.

The so-called callback is to pass an interface in the method parameters. When the parent class calls this method, it must call the implementation class of the interface passed in the method.

The following is a specific example, the execute method in JdbcTemplate.

Interviewer: Resume writing familiar with Spring source code?  Do you know what design patterns it uses?

 

Interviewer: Resume writing familiar with Spring source code?  Do you know what design patterns it uses?

 

The above is only the basic principle of Spring JdbcTemplte implementation. Spring JdbcTemplate has done more things internally, such as encapsulating all basic operations into the JdbcOperations interface, and using JdbcAccessor to manage DataSource and conversion exceptions.

Strategy (Strategy)

The strategy mode is the packaging of the algorithm, separating the responsibility of using the algorithm from the algorithm itself, and delegating to different object management. The strategy pattern usually packs a series of algorithms into a series of strategy classes as a subclass of an abstract strategy class.

The Strategy mode is used when instantiating objects in spring. The following code in SimpleInstantiationStrategy illustrates the use of the strategy mode.

Interviewer: Resume writing familiar with Spring source code?  Do you know what design patterns it uses?

 

Simple factory

It is also called the StaticFactory Method pattern, but it is not one of the 23 GOF design patterns.

The essence of the simple factory pattern 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 model. The bean object is obtained by passing in a unique identifier, but whether it is created after passing in parameters or before passing in parameters depends on the specific situation. The following configuration is to create an itxxzBean in the HelloItxxz class.

<beans>

Factory Method

Usually the application directly uses new to create new objects. In order to separate the creation and use of the object, the factory model is adopted, that is, the application delegates the creation and initialization of the object to the factory object.

Under normal circumstances, applications have their own factory objects to create beans. If the application's own factory objects are handed over to Spring for management, then Spring manages not ordinary beans, but factory beans.

Take the static method in the factory method as an example to explain.

import java.util.Random;

Create a config.xm configuration file and include it in the Spring container for management. You need to specify the static method name through factory-method.

<bean id="random"

test:

public static void main(String[] args) {

Singleton mode (Singleton)

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

The singleton mode in spring completes the second half of the sentence, which provides a global access point BeanFactory. But there is no singleton control from the constructor level, because spring manages arbitrary java objects.

Core reminder: The default beans under Spring are singleton, which can be specified by singleton="true|false" or scope="?".

Adapter

In Spring Aop, Advice is used to enhance the function of the proxy class.

The principle that Spring realizes this AOP function uses the proxy mode.

1. JDK dynamic proxy.

2. CGLib bytecode generation technology agent.

The method-level aspect enhancement is performed on the class, that is, the proxy class of the proxy class is generated, and the interceptor is set before the method of the proxy class. The function of the proxy method is enhanced by executing the interceptor content, and the aspect-oriented programming is realized.

public interface AdvisorAdapter {
class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {

Proxy

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

From a structural point of view, it is similar to the Decorator mode, but Proxy is a control, more like a restriction on functions, and Decorator is to increase responsibilities.

Spring's Proxy mode is reflected in aop, such as JdkDynamicAopProxy and Cglib2AopProxy.

Decoration mode (Decorator)

Also called the wrapper mode (Wrapper). The definition given by GOF in the book "Design Patterns" is: to dynamically add some additional responsibilities to an object. In terms of increasing functionality, Decorator mode is more flexible than generating subclasses.

In our project, we encountered such a problem: Our project needs to connect to multiple databases, and different customers will visit different databases according to their needs during each visit.

Here, you will first think of configuring all dataSources in spring's applicationContext. These dataSources may be of various types, such as different databases: Oracle, SQL Server, MySQL, etc., or they may be different data sources: such as org.apache.commons.dbcp.BasicDataSource provided by apache, and org provided by spring. springframework.jndi.JndiObjectFactoryBean etc. Then sessionFactory sets the dataSource attribute to a different data source according to each request of the customer to achieve the purpose of switching the data source.

The wrapper pattern used in Spring has two manifestations in class names: one is that the class name contains Wrapper, and the other is that the class name contains Decorator. Basically add some extra responsibilities to an object dynamically.

Observer

Define a one-to-many dependency relationship between objects. When the state of an object changes, all objects that depend on it are notified and automatically updated.

The common place for Observer mode in spring is the implementation of listener. Such as ApplicationListener.

Guess you like

Origin blog.csdn.net/weixin_45132238/article/details/108537573