Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the nine design patterns commonly used in spring

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "what are the nine design patterns commonly used in spring". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the nine design patterns commonly used in spring.

Template method (Template Method)

Spring jdbc template, the delicacy of the Spring source code is really admired, extremely classic.

Spring is really a collection of design patterns, and the use is perfect. Template method (template method) is widely used in spring, such as: jdbcTemplate,hibernateTemplate,JndiTemplate and some surrounding wrappers undoubtedly use the template mode, but spring does not simply use the template method, but on this basis to do an innovation, with callback (callback) used together, using extremely flexible.

The so-called template is to define the main flow of the algorithm in the parent class, and delay some personalized steps to the subclass. The parent class always controls the initiative of the whole process, and the subclass only assists the parent class to implement some customizable steps.

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

The following is a concrete example of the execute method in JdbcTemplate.

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

Policy (Strategy)

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

The Strategy pattern is used in spring when instantiating objects, and the following code in SimpleInstantiationStrategy illustrates the use of the policy pattern.

Simple factory

Also known as the static factory method (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 decides which product class should be created based on the parameters passed in.

The BeanFactory in spring is the embodiment of the simple factory pattern. The bean object is obtained by passing in a unique identity, but whether to create it after passing in parameters or before passing in parameters depends on the situation. The configuration is to create an itxxzBean in the HelloItxxz class.

Factory method (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 mode is adopted, that is, the application hands over the responsibility of object creation and initialization to the factory object.

In general, applications have their own factory objects to create bean. If the application's own factory objects are handed over to Spring management, then Spring manages not the normal bean, but the factory Bean.

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

Import java.util.Random

To create a config.xm configuration file and manage it in a Spring container, you need to specify a static method name through factory-method.

Test:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report