Design mode (7)--Template mode

Source: Internet
Author: User

7. Template Mode//ver1//exam paper class Testpaper{public:void TestQuestion1 () {}void TestQuestion2 () {}virtual string Answer1 () { Return "";} Virtual String Answer2 () {return "";}}; Class Testpapera:public Testpaper{public:void TestQuestion1 () {testpaper::testquestion1 ();//a answer 1answer1 ();} void TestQuestion2 () {testpaper::testquestion2 ();//a answer 2answer2 ();} String Answer1 () {return "A";} String Answer2 () {return "B";}}; Class Testpaperb:public Testpaper{public:void TestQuestion1 () {testpaper::testquestion1 ();//b answer 1answer1 ();} void TestQuestion2 () {testpaper::testquestion2 ();//b answer 2answer2 ();} String Answer1 () {return "C";} String Answer2 () {return "D";}}; void Main1 () {testpaper * Pstua = new Testpapera ();p stua->testquestion1 ();p stua->testquestion2 (); Testpaper * pstub = new Testpaperb ();p stub->testquestion1 ();p stub->testquestion2 ();} Template mode: Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. Template mode allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm. Template mode shows its advantages by moving the invariant behavior to the superclass and removing the duplicated code in the subclass.

  

Design mode (7)-Template 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.