If you don’t understand Spring’s 9 design patterns, the interview will suffer

Introduction : As a pillow book in work and study, design patterns are often in an embarrassing situation where we often say not to use them. It is not that we often forget, but we have never remembered. As a classic framework in the industry, Spring can be regarded as a model in the industry in terms of architecture design and code writing.

Okay, not much to say, let's start today's content. There are nine commonly used design patterns in spring, let's give an example.

If you don’t understand Spring’s 9 design patterns, the interview will suffer

 

1. Simple factory model

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.

If you don’t understand Spring’s 9 design patterns, the interview will suffer

 

Second, the factory method model

Usually the application directly uses new to create a new object. 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, the application has its own factory object to create beans. If the application's own factory object is 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:

If you don’t understand Spring’s 9 design patterns, the interview will suffer

 

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:

If you don’t understand Spring’s 9 design patterns, the interview will suffer

 

test:

If you don’t understand Spring’s 9 design patterns, the interview will suffer

 

Three, singleton mode

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, this is 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="?".

Fourth, the adapter mode

In Spring Aop, Advice is used to enhance the function of the proxied class. The principle of Spring's realization of this AOP function is to use the proxy mode (1, JDK dynamic proxy. 2, CGLib bytecode generation technology proxy.) to enhance the method-level aspect of the class, that is, to generate the proxy class of the proxy class, and Before the method of the proxy class, an interceptor is set, and the function of the proxy method is enhanced by executing the content of the interceptor, and the aspect-oriented programming is realized.

Adapter class interface: Target

If you don’t understand Spring’s 9 design patterns, the interview will suffer

 

Five, the wrapper mode

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. We used to always configure a data source in spring and hibernate frameworks, so the dataSource property of sessionFactory always points to this data source and is constant. All DAOs access the database through this data source when using sessionFactory.

But now, due to the needs of the project, our DAO has to constantly switch between multiple data sources when accessing the sessionFactory. The problem arises: how to make the sessionFactory perform data persistence and dynamically switch according to customer needs Different data sources? Can we solve it with a small amount of modification under the spring framework? Are there any design patterns that can be used?

First thought 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.

Six, agency model

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.

Seven, the observer mode

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 commonly used place of Observer pattern in spring is the realization of listener. Such as ApplicationListener.

Eight, strategy mode

Define a series of algorithms, encapsulate them one by one, and make them interchangeable. This mode allows the algorithm to change independently of the customers who use it. The Strategy mode is used when instantiating objects in spring. The following code in SimpleInstantiationStrategy illustrates the use of the strategy mode:

Nine, template method mode

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

Template Method mode generally needs to be inherited. Here I want to explore another understanding of Template Method. The JdbcTemplate in spring does not want to inherit this class when using this class, because there are too many methods in this class, but we still want to use the existing stable and public database connection of JdbcTemplate, so what should we do? We can extract the changed things as a parameter and pass it into the JdbcTemplate method. But the changed thing is a piece of code, and this piece of code will use variables in JdbcTemplate. How to do? Then let's use the callback object.

Define a method for manipulating variables in the JdbcTemplate in this callback object. When we implement this method, we will concentrate the changes here. Then we pass in this callback object to JdbcTemplate to complete the call. This may be another way to implement Template Method that does not require inheritance.

Write at the end

Pay attention, don’t get lost; continue to update Java architecture related technologies and hot articles! ! !

If you don’t understand Spring’s 9 design patterns, the interview will suffer

Guess you like

Origin blog.csdn.net/m0_46995061/article/details/114265058