A,

We will meet in the daily code design, many fixed logic operations, so this time if not for a certain design, can appear repeatedly redundant logic, lead to code maintainability is not high, so generally encountered this kind of situation, we should try to the operation of the operation of the fixed and variable separation, every change as long as the change in variable operation. This is the template method pattern.

Second, the problem of

Since it is a template method, the question we introduce is also a template, how to design the situational operation of a questionnaire.

Third, understand

Let’s analyze the question first. In the questionnaire scenario, we can analyze that there must be a questionnaire and some people filling in the questionnaire. Among them, there is only one questionnaire, but there are many people filling in the questionnaire, and everyone has his own answer. So we can condense this question into the answer in the questionnaire, which is the questionnaire type plus the answer type. In fact, we can see that the questionnaire is the template class in the template method mode. As long as we design a questionnaire class and an answer class, each time one answer class is passed to the questionnaire class, it means that a person has filled in the questionnaire, which is convenient and concise. In the template method design pattern, we use the above to fill in the questionnaire of this kind of thought, design the parent class, all the duplicated code upgrade to the parent class, rather than allowing each subclass to repeat, when we want to finish in a same level of detail a process or a series of steps, but the level of its individual detail steps in more detail on the implementation may be different, We usually consider using the template method pattern for implementation. The structure diagram of the template method is as follows:

The technical expression of the template method pattern is to define an algorithm skeleton in an operation and defer some steps to a subclass. The template method pattern can be a subclass that can redefine some steps in an algorithm without changing the algorithm structure.

Four, implementation,

abstract class AbstractClass {
	public abstract void PrimitiveOperation1(a);// These are implemented in subclasses
	public abstract void PrimitiveOperation2(a);
	
	public void TemplateMethod(a) {
		PrimitiveOperation1();
		PrimitiveOperation2();
		// Other fixed operations}}Copy the code
public class ConcreteClass extends AbstractClass {
	@Override
	public void PrimitiveOperation1(a) {
		// The concrete implementation of the subclass
		
	}
	
	@Override
	public void PrimitiveOperation2(a) {
		// The concrete implementation of the subclass}}Copy the code
public class ConcreteClassB extends AbstractClass {

	@Override
	public void PrimitiveOperation1(a) {
		// The concrete implementation of the subclass
		
	}
	
	@Override
	public void PrimitiveOperation2(a) {
		// The concrete implementation of the subclass}}Copy the code
public class Main {

	public static void main(String[] args) {
		AbstractClass abstractClass;
		abstractClass = new ConcreteClass();
		abstractClass.TemplateMethod();
		
		abstractClass = newConcreteClassB(); abstractClass.TemplateMethod(); }}Copy the code