Template Method Mode

Source: Internet
Author: User
Tags mcsd certification

Preface
I believe that you have already learned a lot about the process of posting your resume. This time, I received an interview notice from XX and completed the interview. When I came back, the bird asked me how the food felt? The dish replied: "When books are used, the English language is too bad and there is no way ." Then they discussed the previous Microsoft MCSE and MCSD certification exams. At the beginning, many educational institutions had obtained the question bank and promised to pass the exam without fees. Therefore, one of the students is not a computer professional and has passed the development technology certification of the world's largest software company by answering questions.

At last, laruence said that he did not take the test because he copied the wrong questions when he was a child. His parents said that he did not study hard. In fact, the problem is not here. If you use a standardized examination paper, you will not suffer from incorrect answers.

So laruence asked me to write a copy of The question code.

The first assignment of the dish
# Include <stdio. h>

 

// Exam copied by student

Class TestPaperA

{

Public:

Void TestQuestion1 ()

{

Printf ("Yang once got it, and then gave it to Guo Jing. The Xuan iron formed by Yi tianjian and Tu longdao may be []. ball Milling Cast Iron B. tinplate c. high-speed alloy steel d. carbon-Plastic Fiber ");

Printf ("Answer: B ");

}

Void TestQuestion2 ()

{

Printf ("Yang Guo, Cheng Ying, Lu wushuang wiped out love flowers, resulting in []. make this plant no longer harm B. extinction of a rare species c. destroying the ecological balance of the biosphere d. ");

Printf ("Answer: ");

}

Void TestQuestion3 ()

{

Printf ("The Blue Phoenix caused the master of Huashan and the six gods of taogu to vomit. If you are a doctor, what medicine will you give them? []. aspirin B. niuhuang Jiedu tablets c. d. let them drink a lot of milk e. none of the above ");

Printf ("Answer: c ");

}

};

 

// Exam copied by student B

Class TestPaperB

{

Public:

Void TestQuestion1 ()

{

Printf ("Yang once got it, and then gave it to Guo Jing. The Xuan iron formed by Yi tianjian and Tu longdao may be []. ball Milling Cast Iron B. tinplate c. high-speed alloy steel d. carbon-Plastic Fiber ");

Printf ("Answer: d ");

}

Void TestQuestion2 ()

{

Printf ("Yang Guo, Cheng Ying, Lu wushuang wiped out love flowers, resulting in []. make this plant no longer harm B. extinction of a rare species c. destroying the ecological balance of the biosphere d. ");

Printf ("Answer: B ");

}

Void TestQuestion3 ()

{

Printf ("The Blue Phoenix caused the master of Huashan and the six gods of taogu to vomit. If you are a doctor, what medicine will you give them? []. aspirin B. niuhuang Jiedu tablets c. d. let them drink a lot of milk e. none of the above ");

Printf ("Answer: ");

}

};

 

Int main ()

{

Printf ("Exam copied by Student :");

TestPaperA studentA;

StudentA. TestQuestion1 ();

StudentA. TestQuestion2 ();

StudentA. TestQuestion3 ();

Printf ("Student B's exam :");

TestPaperB studentB;

StudentB. TestQuestion1 ();

StudentB. TestQuestion2 ();

StudentB. TestQuestion3 ();

Return 0;

}

"These two papers are very similar, except for different answers, and there is nothing different. It is easy to make mistakes and difficult to maintain them ."

"That's right. If the teacher suddenly wants to change the question, then both of them need to change the code. If someone makes a mistake, it's really bad. What do you do ?"

"The teacher gave a paper, printed multiple copies, and asked the students to fill in the answer. Here we should share the body and answer, abstract a parent class, let the two subclasses inherit from it, and write the public question code to the parent class ."

Template Method Mode
The template method mode prepares an abstract class, implements some logic in the form of a specific method and a specific constructor, and then declares some abstract methods to force the subclass to implement the remaining logic. Different sub-classes can implement these abstract methods in different ways to implement the rest logic differently. First, create a top-level logic framework, and leave the logic details to specific sub-classes for implementation.

Implementation Method (UML class diagram)

Implementation Code
# Include <stdio. h>

 

Class TestPaper

{

Public:

Void TestQuestion1 ()

{

Printf ("Yang once got it, and then gave it to Guo Jing. The Xuan iron formed by Yi tianjian and Tu longdao may be []. ball Milling Cast Iron B. tinplate c. high-speed alloy steel d. carbon-Plastic Fiber ");

Printf ("Answer: % c", Answer1 ());

}

Void TestQuestion2 ()

{

Printf ("Yang Guo, Cheng Ying, Lu wushuang wiped out love flowers, resulting in []. make this plant no longer harm B. extinction of a rare species c. destroying the ecological balance of the biosphere d. ");

Printf ("Answer: % c", Answer2 ());

}

Void TestQuestion3 ()

{

Printf ("The Blue Phoenix caused the master of Huashan and the six gods of taogu to vomit. If you are a doctor, what medicine will you give them? []. aspirin B. niuhuang Jiedu tablets c. d. let them drink a lot of milk e. none of the above ");

Printf ("Answer: % c", Answer3 ());

}

Protected:

Virtual char Answer1 () = 0;

Virtual char Answer2 () = 0;

Virtual char Answer3 () = 0;

};

 

Class TestPaperA: public TestPaper

{

Protected:

Virtual char Answer1 ()

{

Return B;

}

Related Article

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.