1. Usage scenarios
scene 1
When we want to complete a process or a series of steps that are consistent at a certain level of detail, but the implementation of individual steps at a more detailed level may be different, we usually consider the template method pattern to deal with
For example: referring the method that is repeatedly declared in the subclass to the superclass to declare, using polymorphism to achieve code reuse (subclass rewriting)
scene 2
When a process consisting of a series of steps needs to be executed, the process is the same at a high level, but the implementation of some steps may not work. At this time, we can usually consider using the template method pattern.
2. Definition
Template Method Pattern: Define the stock price of an algorithm in an operation, while deferring some steps to subclasses.
Template methods allow subclasses to redefine certain steps of an algorithm without changing its structure
3. Features
By moving the invariant behavior (fixed data loading) to the superclass and removing the repetitive code in the subclass to reflect his advantages (even if there is no abstract behavior here, as long as the abstract method is fine).
He provides a good code reuse platform.
When constant and mutable behaviors are mixed in a subclass implementation of a method, the constant behavior is repeated in the subclass, and we need to move the constant behavior to a single place, so that It can help subclasses to get rid of the entanglement of repeated invariant behavior, resulting in repeated code
4. Structure diagram
5. Cases
AbstractClass
It is an abstract class, which is actually an "abstract template", which defines and implements a template method.
This template method is generally a concrete method, which gives the skeleton of a top-level logic, and the logical composition steps are deferred to the subclass implementation in the corresponding abstract operation.
Top-level logic may also call some specific methods (used in 1.0 projects, mainly for public data initialization)
package com.template.templateVersionTwo; /** * abstract class * * @author Wang Ziwei * @date 2022/8/2 */ public abstract class AbstractClass { /** * abstract behavior * into subclass implementation */ public abstract void primitiveOperationOne(); /** * abstract behavior * into subclass implementation */ public abstract void primitiveOperationTwo(); /** * Template method * give the logical skeleton * defer subclass implementation * Personal feeling: very similar to the cut surface */ public void templateMethod() { primitiveOperationOne(); primitiveOperationTwo(); System.out.println("wzw"); } }
ConcreteClass
Implements one or more abstract methods defined by the parent class
Each AbstractClass can have any number of ConcreteClass corresponding to it, and each ConcreteClass can give different implementations of these abstract methods (that is, the composition steps of the top-level logic), so that the implementation of the top-level logic is different.
ConcreteClassA
package com.template.templateVersionTwo; /** * concrete implementation class * * @author Wang Ziwei * @date 2022/8/2 */ public class ConcreteClassA extends AbstractClass { @Override public void primitiveOperationOne() { System.out.println("specific A Implementation of class method 1"); } @Override public void primitiveOperationTwo() { System.out.println("specific A Implementation of class method 2"); } }
ConcreteClassB
package com.template.templateVersionTwo; /** * concrete implementation class * * @author Wang Ziwei * @date 2022/8/2 */ public class ConcreteClassB extends AbstractClass { @Override public void primitiveOperationOne() { System.out.println("specific B Implementation of class method 1"); } @Override public void primitiveOperationTwo() { System.out.println("specific B Implementation of class method 2"); } }
TestTemplate
Test method testVersionTwo
@Test public void testVersionTwo() { AbstractClass abstractClass; abstractClass = new ConcreteClassA(); abstractClass.templateMethod(); abstractClass = new ConcreteClassB(); abstractClass.templateMethod(); }
Test Results