Design Mode ------ template method mode

Source: Internet
Author: User

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

Structure:


AbstractClass is an abstract template that defines and implements a template method. This template method is generally a specific method that provides a skeleton of top-level logic.

ConcreteClass is a specific template that implements one or more abstract methods defined by the parent class. Each AbstractClass can have any number of concreteclasses, and each ConcreteClass can provide the building steps of top-level logic. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + yc/release/CoaM8L3A + release/bPM0rK63Mfls/release + 8zOKjrMi7uvPV + release/release + release/svd28/release/svd28/release/ svd28/release + 7b7NysfSu7j2s + release/gzay1xLK/release + release/release + 7aOst6K4 + MO/uPbNrNGno6zL + release/release + 7brzo6y + zb + release LP2s + expires/WoaO/expires + 8wM/KpsrVxvC/expires + tPCwuKO6YiA8L3A + expires + y93bz8ysehvqG/expires + expires/svd28/expires + 0afJ + expires + expires/expires + expires/svd28/expires + expires/svd28/expires + expires/7L3dvPzKx6G + expires + ICAgIKGtoa08L3A + expires + sLroaMKPHA + expires = "brush: java; "> class TestPaper {public void TestQuestion1 () {Console. writeLine ("\ n Copy shortcut key is []. ctrl + A B. ctrl + C c. ctrl + L d. ctrl + D "); Console. writeLine ("Answer:" + Answer1 ();} public void TestQuestion2 () {Console. writeLine ("\ n all-selected shortcut key is []. ctrl + A B. ctrl + C c. ctrl + L d. ctrl + D "); Console. writeLine ("Answer:" + Answer2 ();} public void TestQuestion3 () {Console. writeLine ("\ n paste the shortcut key is []. ctrl + A B. ctrl + C c. ctrl + V d. ctrl + D "); Console. writeLine ("Answer:" + Answer3 ();} protected virtual string Answer1 () {return "" ;}protected virtual string Answer2 () {return "";} protected virtual string Answer3 () {return "" ;}}// student a's exam class TestPaperA: TestPaper {protected override string Answer1 () {return "B ";} protected override string Answer2 () {return "c";} protected override string Answer3 () {return "c" ;}// student B's test class TestPaperB: testPaper {protected override string Answer1 () {return "B";} protected override string Answer2 () {return "c";} protected override string Answer3 () {return "";}}
Client:

Static void Main (string [] args) {Console. writeLine ("\ n student a's exam:"); TestPaper studentA = new TestPaperA (); studentA. testQuestion1 (); studentA. testQuestion2 (); studentA. testQuestion3 (); Console. writeLine ("\ n student B copied exam:"); TestPaper studentB = new TestPaperB (); studentB. testQuestion1 (); studentB. testQuestion2 (); studentB. testQuestion3 ();}

Display:


Finally, let's make a summary. Looking at the small example above, there is a process that needs to be executed. This process includes a series of steps. The whole process is the same from a high-level perspective, but the details of each step are different. The template method mode is to move the same to the superclass, so as to remove repeated code in the subclass and provide a code reuse platform. In a software company, a designer is responsible for providing the outline and skeleton of an algorithm, while others are responsible for providing the logical steps of this algorithm. The methods that represent these specific logical steps are called Basic methods, and the methods that aggregate these basic methods are called template methods.

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.