Template defines the boundary of the problem, and subclasses define the concrete implementation. You just have to play within the scope of the Template.

This article is shared from huawei cloud community “Template in Spring a full understanding, analysis of the nature of the problem”, author: Coriander chat game.

In the process of learning Spring, I often see various templates. If I can understand the core idea of this, I wonder if I can understand all the templates at once. That is to say, I can master the essence of things and do everything in one way

1. Template pattern of design pattern

The template pattern, also known as the template method pattern, is the main framework for defining problems. The detailed implementation is left to subclasses, that is, to define the rules of the matter, and each part can be customized in the subclass. Keep the big frame, keep the flexibility.

The template pattern, which allows subclasses to redefine certain steps without changing the results of the algorithm, is a behavioral design pattern.

The template mode is applicable to the following scenarios:

Define immutable parts of the process and leave the mutable behavior to subclasses to implement.

Abstract the common behavior of the subclasses, leaving the common code in the parent class and putting what needs to be implemented separately in the subclasses to avoid code duplication.

Here’s an example:

For example, unity and Cocos now have scripts. The engine has a built-in script execution process, and the developer implements specific game behaviors based on the needs of the game.

Let’s take Unity as an example

Unity’s template class is MonoBehaviour. The most common methods in the template class are:

  • Awake: When a script is instantiated, Awake is called. Most of our member variables are initialized in this class.

  • Start: called only before the Update function is called for the first time. Since it is called after Awake, we can initialize some variables that need to depend on Awake in Start. We also mostly execute StartCoroutine in this class to do some coroutine firing. Note that when writing a script in C#, you must start a coroutine using StartCoroutine, but you don’t need to do this if you’re using JavaScript.

  • Update: When a game frame starts playing (at which point the GameObject has been instantiated), its Update is called every frame.

  • LateUpdate: LateUpdate is called after all the Update functions have been called.

  • FixedUpdate: When MonoBehaviour is enabled, its FixedUpdate is called every fixed frame.

  • OnDestroy: This function is called when MonoBehaviour will be destroyed.

2. Advantages and disadvantages of the template pattern

Advantages: Using the template pattern to place code with the same processing logic in an abstract parent class improves code reuse.

You can make your code more extensible by putting different code in different subclasses and adding new behavior by extending the subclasses.

Disadvantages: Each abstract class requires a subclass implementation, resulting in an increase in the number of classes.

Increasing the number of classes indirectly increases the complexity of the system.

Because of the inherent disadvantages of inheritance, if the parent class adds a new abstract method, all subclasses have to change it.

3. Templates in Spring

In the process of learning Spring, I found several templates encapsulated by Spring, which are listed here for comparison and learning.

SimpleJdbcTemplate This template is out of date

JDBCTemplate JDBCTemplate is a Spring encapsulation of JDBC. Developers write their own SQL and need to inject dataSource.

HibernateTemplate (sessionFactory) is used to operate on objects regardless of the underlying database

NamedParameterJdbcTemplate

Also jDBC-based encapsulation, but not for parameter writing? Placeholders are used instead: parameter names

5.RestTemplate is a client provided by Spring to access Rest services

RestTemplate is a client provided by Spring for accessing Rest services. RestTemplate provides multiple convenient methods for accessing remote Http services, which greatly improves client writing efficiency

6.AmqpTemplate

The AmqpTemplate interface defines all the basic operations for sending and receiving messages

4, summarize

Template defines the boundary of the problem, and subclasses define the concrete implementation. Just play within the scope of the Template, just like Tang Seng standing in the circle drawn by Sun Wukong. Inside the circle keeps you safe, but outside the circle is a little dangerous

Click to follow, the first time to learn about Huawei cloud fresh technology ~