Template Method Mode

Source: Internet
Author: User

Inheritance:

As an inheritance of one of the three object-oriented features, the function is not generally powerful. On the 344 page of the book "big talk design mode", we can see that the content of cat and dog code is basically the same, it's just that the sound is different when we call it. If we want to add other animals, such as rabbits and pig... and so on, you need to write the same code (copy) to change the call.

We know that the principle of programming is to avoid repetition as much as possible and compare four classes. The animal class can be abstracted as the parent class to put the same code in the parent class, then the sub-classes rewrite the shout () method through polymorphism to implement their respective calls.

Benefits of inheritance: subclass has non-private attributes and functions of the parent class; subclass is the specialization of the parent class; subclass can override the methods of the parent class.

 

Template Method mode:

Defines the skeleton of an algorithm in an operation and delays some steps to the subclass. The template method allows the subclass to redefine certain steps of the Algorithm without changing the structure of an algorithm.

To put it bluntly, the main code of several classes is basically the same, but the algorithms of individual steps are different. We use the template mode to refactor the code.

 

Take the instructor copying questions on the blackboard, and the students copying the questions and writing the corresponding answers as an example.

Code structure


// Classtestpapera {// Question 1 Public void testquestion1 () {console. writeline ("2 + 1 = [] a.0 B .1 C.2 D.3") console. writeline ("Answer: C")} // Question 2 Public vodi testquestion2 () {console. writeline ("2-1 = [] a.0 B .1 C.2 D.3") console. writeline ("Answer: C")} // Question 3 Public vodi testquestion3 () {console. writeline ("2*1 = [] a.0 B .1 C.2 D.3") console. writeline ("Answer: C ")}}

// Student B's classtestpapera {// Question 1 Public void testquestion1 () {console. writeline ("2 + 1 = [] a.0 B .1 C.2 D.3") console. writeline ("Answer: D")} // Question 2 Public vodi testquestion2 () {console. writeline ("2-1 = [] a.0 B .1 C.2 D.3") console. writeline ("Answer: D")} // Question 3 Public vodi testquestion3 () {console. writeline ("2-1 = [] a.0 B .1 C.2 D.3") console. writeline ("Answer: D ")}}

Compared with the code for copying a test paper, we know that the test questions in the test paper are the same, but the answers of A and B are different from those of console. writeline. Specifically, the output answer (A. B. C. D) is different, while the console. writeline () method is still common to sub-classes.

We already know the dangers of repetition, which are prone to errors and difficult to modify. So what we need to do is extract the code and mention the same code to a common parent class.

After Optimization

Code structure


Parent class

<Span style = "font-size: 14px;"> class testpaper {// Question 1 Public void testquestion1 () {console. writeline ("2 + 1 = [] a.0 B .1 C.2 D.3") console. writeline ("Answer: + answer1 ()")/answer1 () is a virtual method,} protected virtual stringanswer1 () /ask the subclass to override this method {return "" }}// Question 2 Public vodi testquestion2 () {console. writeline ("2-1 = [] a.0 B .1 C.2 D.3") console. writeline ("Answer: + answer2 ()")} protected virtual string answer2 () {return ""} // Question 3 Public vodi testquestion3 () {console. writeline ("2*1 = [] a.0 B .1 C.2 D.3") console. writeline ("Answer: + answer3 ()")} protected virtual string answer () {return "} </span>

Subclass code

<Span style = "font-size: 14px;"> // Class A classtestpapera: testpaper {protected override string answer1 {return C;} protected override string answer2 {return C ;} protected override string answer3 {return C ;}</span>
The same class B code and Class A code are different only when the answer is output.

Client code:

<Span style = "font-size: 14px;"> static void main (string [] ARGs) {console. writeline ("Student A's exam:"); testpaper studenta = new testpapera (); studenta. testqusetion1 (); studenta. testqusetion2 (); studenta. testqusetion3 (); console. writeline ("Student B's exam:"); testpaper studentb = new testpapera (); studentb. testqusetion1 (); studentb. testqusetion2 (); studentb. testqusetion3 () ;}</span>

The template method mode provides a good code reuse platform by moving the unchanged behavior to the superclass to remove the repeated code in the subclass.


Summary:The template mode fully embodies the benefits of inheritance. By extracting unchanged behavior abstractions as parent classes, we can simplify code duplication and optimize the system structure. It is a very common mode.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.