Before telling this model, let’s look at a case: copy the question: Two students copied the questions given by the teacher on paper and wrote the answers
Let's see a stupid way of writing first
public class TestPaperA { public void testQuestion1(){ ("What is 1+1 equal? a.1 b.2 c.3 d.4"); ("Answer: b"); } public void testQuestion2(){ ("What is 1*1 equal? a.1 b.2 c.3 d.4"); ("Answer: a"); } public void testQuestion3(){ ("What is 1/1 equal? a.1 b.2 c.3 d.4"); ("Answer: a"); } } public class TestPaperB { public void testQuestion1(){ ("What is 1+1 equal? a.1 b.2 c.3 d.4"); ("Answer: c"); } public void testQuestion2(){ ("What is 1*1 equal? a.1 b.2 c.3 d.4"); ("Answer: a"); } public void testQuestion3(){ ("What is 1/1 equal? a.1 b.2 c.3 d.4"); ("Answer: d"); } } public class Test { public static void main(String[] args) { ("The test paper copied by Student A:"); TestPaperA studentA= new TestPaperA(); studentA.testQuestion1(); studentA.testQuestion2(); studentA.testQuestion3(); ("Student B copied the test paper:"); TestPaperB studentB= new TestPaperB(); studentB.testQuestion1(); studentB.testQuestion2(); studentB.testQuestion3(); } }
Output result:
The test paper copied by students:
What is 1+1 equal? a.1 b.2 c.3 d.4
Answer: b
What is 1*1 equal? a.1 b.2 c.3 d.4
Answer: a
What is 1/1 equal? a.1 b.2 c.3 d.4
Answer: a
Student B’s test paper:
What is 1+1 equal? a.1 b.2 c.3 d.4
Answer: c
What is 1*1 equal? a.1 b.2 c.3 d.4
Answer: a
What is 1/1 equal? a.1 b.2 c.3 d.4
Answer: d
It can be seen that in addition to the different answers of Student A and Student B, they copy the same questions, and the process of copying the questions is prone to errors. Moreover, if the teacher corrects the questions, both students need to change the questions.
How to optimize? Let's first make a preliminary optimization: those who have learned inheritance will think that if the public part is placed in the parent class, the child class inherits the parent class, and naturally the public part will be owned after the child class is inherited.
public class TestPaper { public void testQuestion1(){ ("What is 1+1 equal? a.1 b.2 c.3 d.4"); } public void testQuestion2(){ ("What is 1*1 equal? a.1 b.2 c.3 d.4"); } public void testQuestion3(){ ("What is 1/1 equal? a.1 b.2 c.3 d.4"); } } public class TestPaperA extends TestPaper{ @Override public void testQuestion1(){ super.testQuestion1(); ("Answer: b"); } @Override public void testQuestion2(){ super.testQuestion2(); ("Answer: a"); } @Override public void testQuestion3(){ super.testQuestion3(); ("Answer: a"); } } public class TestPaperB extends TestPaper{ @Override public void testQuestion1(){ super.testQuestion1(); ("Answer: c"); } @Override public void testQuestion2(){ super.testQuestion2(); ("Answer: a"); } @Override public void testQuestion3(){ super.testQuestion3(); ("Answer: d"); } }
Test similar
When we looked at this preliminary optimization, we found that there are still duplicate parts, such as super.testQuestion1() and ("Answer")
Since we have used inheritance and are sure that this inheritance is meaningful, we should become a template for subclasses. All duplicate code should be upgraded to the parent class, rather than letting each subclass be repeated.
For the example of "copying questions", except for the students' answers, everything else is the same. Continue to optimize:
public abstract class TestPaper { public void testQuestion1(){ ("What is 1+1 equal? a.1 b.2 c.3 d.4"); ("Answer:"+answer1()); } public void testQuestion2(){ ("What is 1*1 equal? a.1 b.2 c.3 d.4"); ("Answer:"+answer2()); } public void testQuestion3(){ ("What is 1/1 equal? a.1 b.2 c.3 d.4"); ("Answer:"+answer3()); } public abstract String answer1(); public abstract String answer2(); public abstract String answer3(); } public class TestPaperA extends TestPaper{ @Override public String answer1() { return "b"; } @Override public String answer2() { return "a"; } @Override public String answer3() { return "a"; } } public class TestPaperB extends TestPaper{ @Override public String answer1() { return "c"; } @Override public String answer2() { return "a"; } @Override public String answer3() { return "d"; } } public class Test { public static void main(String[] args) { ("The test paper copied by Student A:"); TestPaper studentA= new TestPaperA(); studentA.testQuestion1(); studentA.testQuestion2(); studentA.testQuestion3(); ("Student B copied the test paper:"); TestPaper studentB= new TestPaperB(); studentB.testQuestion1(); studentB.testQuestion2(); studentB.testQuestion3(); } }
Output result:
The test paper copied by students:
What is 1+1 equal? a.1 b.2 c.3 d.4
Answer: b
What is 1*1 equal? a.1 b.2 c.3 d.4
Answer: a
What is 1/1 equal? a.1 b.2 c.3 d.4
Answer: a
Student B’s test paper:
What is 1+1 equal? a.1 b.2 c.3 d.4
Answer: c
What is 1*1 equal? a.1 b.2 c.3 d.4
Answer: a
What is 1/1 equal? a.1 b.2 c.3 d.4
Answer: d
The result is exactly the same as before, but much simpler. At this time, more students should answer the paper, but they just fill in the multiple-choice answers on the wooden board of the test paper. This is the only difference between everyone's test paper (who said it, the name is also different, but this approach is indeed the greatest reuse of the test paper)
The template method pattern is described below:https:///article/
Template method pattern: Defines the skeleton of an operational algorithm and delays some steps to a subclass. The template method allows the subclass to redefine certain specific steps of the algorithm without changing the structure of the algorithm.
AbstractClass is an abstract class, which is actually an abstract template that defines and implements a template method.
This template method is generally a specific method. It gives a skeleton of top-level logic, and the logic consists in the corresponding steps
In abstract operations, the implementation of subclasses is deferred.
public abstract class AbstractClass { //Some abstract behaviors are implemented in subclasses public abstract void primitiveOperation1(); public abstract void primitiveOperation2(); //The template method gives the logic skeleton, and the logic composition is some corresponding abstract operations, which are all delayed to subclass implementation public void templateMethod(){ primitiveOperation1(); primitiveOperation2(); } }
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 top-level logic), so that the implementations of top-level logic are different.
public class ConcreteClassA extends AbstractClass{ @Override public void primitiveOperation1() { //Implementation of specific class A method 1, a different method implementation from ConcreteClassB } @Override public void primitiveOperation2() { //Implementation of specific class A method 2, a different method implementation from ConcreteClassB } } public class ConcreteClassB extends AbstractClass{ @Override public void primitiveOperation1() { //Implementation of specific class B Method 1, a different method implementation from ConcreteClassA } @Override public void primitiveOperation2() { //Implementation of specific class B method 2, a different method implementation from ConcreteClassA } }
Test code
public class Test { public static void main(String[] args) { AbstractClass c = null; c = new ConcreteClassA(); (); c = new ConcreteClassB(); (); } }
The template method pattern reflects its advantages by moving the invariant behavior to the superclass and removing duplicate code from the subclass.
The template method mode provides a good code reuse platform. Because sometimes, we encounter a process composed of a series of steps that need to be performed. This process is the same at a high level, but some steps may be implemented differently. At this time, we should usually consider using the template method pattern.
When invariant and mutable behaviors are mixed together in the subclass implementation of the method, the invariant behaviors will repeat in the subclass. We move these behaviors to a single place through the template method pattern, which helps the subclass get rid of the entanglement of repeated unchanging behaviors
The above is an example to explain the detailed content of the JAVA template method mode. For more information about the JAVA template method mode, please pay attention to my other related articles!