Template pattern in Go design pattern

Yuxian: CSDN content partner, CSDN new star mentor, full-stack creative star creator, 51CTO (Top celebrity + expert blogger), github open source enthusiast (go-zero source code secondary development, game back-end architecture https: https://github.com/Peakchen)

 

The principle of template pattern is explained in detail:
template pattern (Template Pattern) is a behavioral design pattern that defines an algorithm framework and delays the implementation of some of the steps to subclasses. The template mode achieves code reuse and scalability by placing the general algorithm process in the parent class, and allowing subclasses to implement specific steps through abstract methods and hook methods.

The core idea of ​​the template pattern is to fix the structure of the algorithm, but to defer the implementation of some specific steps to subclasses. A template method is defined in the parent class, which encapsulates the overall structure of the algorithm. It is usually a concrete method, which contains multiple abstract methods and concrete methods. The abstract method is implemented by the subclass to define the specific steps of the algorithm, while the specific method has been implemented in the parent class to provide the general logic in the algorithm.

Underlying structure diagram:
The following is a classic structure diagram of the template pattern:

+----------------------------+
|         AbstractClass      |
+----------------------------+
| # TemplateMethod()         |
| - AbstractMethod()         |
| - HookMethod()             |
| + ConcreteMethod()         |
+----------------------------+

        ^
        |
        |
        |
        |
        v

+----------------------------+
|        ConcreteClass       |
+----------------------------+
| - AbstractMethod()         |
| - HookMethod()             |
+----------------------------+

In the above structure diagram, AbstractClass it is an abstract class that defines the overall structure of the algorithm and includes template methods  TemplateMethod(). It can be a concrete class, which contains multiple abstract methods  AbstractMethod() and a concrete method  ConcreteMethod(), as well as an optional hook method  HookMethod().

ConcreteClass Is a concrete subclass, inherits from  AbstractClass, and implements the abstract method in it. It defines the concrete steps of the algorithm by implementing abstract methods, can override hook methods to add or modify behavior in the algorithm, and can inherit concrete methods to reuse common logic.

Scenario Explanation:
Template mode is suitable for the following scenarios:

  1. The template pattern can be used when the overall structure of an algorithm has been determined, but the specific implementation of some of the steps may vary. The template mode fixes the structure of the algorithm, and delays the specific implementation to the subclass, so as to realize the reuse and extension of the code.

  2. When you want to customize or extend some steps without changing the structure of the algorithm, you can use the template pattern. By defining hook methods in an abstract class, subclasses can selectively override the hook methods to add or modify the behavior of the algorithm.

  3. The template pattern can be used when there are multiple classes with similar behavioral logic, but the specific implementation may be different. By abstracting the same behavioral logic into the parent class, subclasses only need to implement specific steps, thereby realizing code reuse.

Code example implementation:
The following is an example of using the Go language to implement the template pattern:

package main

import "fmt"

// AbstractClass 抽象类
type AbstractClass interface {
	TemplateMethod()
	AbstractMethod()
	HookMethod()
}

// ConcreteClass 具体子类
type ConcreteClass struct{}

// TemplateMethod 模板方法
func (c *ConcreteClass) TemplateMethod() {
	fmt.Println("TemplateMethod: Start")

	c.AbstractMethod()
	c.HookMethod()

	fmt.Println("TemplateMethod: End")
}

// AbstractMethod 抽象方法
func (c *ConcreteClass) AbstractMethod() {
	fmt.Println("AbstractMethod: Implemented by ConcreteClass")
}

// HookMethod 钩子方法
func (c *ConcreteClass) HookMethod() {
	fmt.Println("HookMethod: Default implementation in ConcreteClass")
}

func main() {
	abstractClass :=&ConcreteClass{}

	abstractClass.TemplateMethod()
}

In the above example, we defined an  AbstractClass interface as an abstract class, which contains  TemplateMethod()the , AbstractMethod() and  HookMethod() methods. Then, we implement the concrete subclass  ConcreteClassand implement the abstract method and hook method in it. In  main() the function, we create an  ConcreteClass instance of , and call  TemplateMethod() the method to execute the algorithm.

Documentation Links:
Here are some links to documentation on the template pattern for further study and reference:

  1. Design Patterns: Elements of Reusable Object-Oriented Software (GoF book)

  2. Refactoring Guru - Template Method

  3. TutorialsPoint - Template Pattern

  4. SourceMaking - Template Method Pattern

Which products are currently in use:
Template mode is a commonly used design mode and is widely used in software development. Here are some examples of products and frameworks that use the template pattern:

  1. java.util.CollectionsThe method in the class in the Java language sort()uses the template pattern.

  2. The JdbcTemplate class in the Spring framework also uses the template pattern, where the execute()method is a template method, and the methods update()such as and query()are specific implementations.

  3. The controller class in some web frameworks usually also uses the template mode, which defines the overall structure of processing requests, and the specific request processing methods are implemented by subclasses.

Guess you like

Origin blog.csdn.net/feng1790291543/article/details/132160278