Template Method pattern of Design Pattern

Source: Internet
Author: User

Template Method Mode

Definition: 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 an algorithm without changing the structure of an algorithm.

For example, the exam copied by student A and student B is very similar. Except for different answers, the other questions are the same. If the instructor wants to change the question, it means that both of them need to change the exam, which is easy to make mistakes. To minimize errors, the template method mode is required.

The exam code is as follows:

<Span style = "font-size: 18px;"> namespace _ 10. _ 3 _ extract code {class testpaper {// Question 1 Public void testquestion1 () {console. writeline ("Question 1"); console. writeline ("Answer:" + answer1 ();} protected virtual string answer1 () {return "" ;}// Question 2 Public void testquestion2 () {console. writeline ("Question 2"); console. writeline ("Answer:" + answer2 ();} protected virtual string answer2 () {return "" ;}// Question 3 Public void testquestion3 () {console. writeline ("Question 3"); console. writeline ("Answer:" + answer3 ();} protected virtual string answer3 () {return "" ;}}// test class testpapera copied by Student: testpaper {protected override string answer1 () {return "B";} protected override string answer2 () {return "C";} protected override string answer3 () {return "A" ;}}// student B's exam class testpaperb: testpaper {protected override string answer1 () {return "C";} protected override string answer2 () {return "A" ;}protected override string answer3 () {return "A" ;}} class program {static void main (string [] ARGs) {console. writeline ("Student A's exam:"); testpaper studenta = new testpapera (); studenta. testquestion1 (); studenta. testquestion2 (); studenta. testquestion3 (); console. writeline ("Student B's exam:"); testpaper studentb = new testpaperb (); studentb. testquestion1 (); studentb. testquestion2 (); studentb. testquestion3 (); console. read () ;}}</span>
The running result is as follows:


The template method mode includes the following two roles:
(1) abstractclass: a series of basic operations (primitiveoperations) are defined in the abstract class. These basic operations can be specific or abstract, each basic operation corresponds to a step of the algorithm, which can be redefined or implemented in its subclass. At the same time, a template method is implemented in the abstract class to define an algorithm framework. The template method can not only call the basic method implemented in the abstract class, you can also call basic methods implemented in the subclass of an abstract class, or call methods in other objects.
(2) concreteclass (specific subclass): It is a subclass of an abstract class and is used to implement basic abstract Operations declared in the parent class to complete the steps of a specific subclass algorithm, you can also override the basic operations that have been implemented in the parent class.

Template Method pattern of Design Pattern

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.