1. My description
The template method mode is to move the unchanged behavior to the superclass, remove the repeated code in the subclass to reflect its advantages, and provide good code reuse.
My understanding is like when we learned to draw pictures, the teacher said that everyone is better than this apple, and then draw a picture of their own, and then everyone is there to draw, however, what we draw is different, but what we share is that we compare the pattern with the same apple.
For another example, a gets 90 points for the same exam and B gets 80 points for the same exam. This test is the same: A and B have the same exam, but they have different scores because they have different answers. In the template method mode, the exam questions remain unchanged. We will move them to the super class. The difference is our answer in this exam.
Ii. UML diagram
For my above example, my UML diagram should be:
Iii. Implementation of my exam template code
#include<iostream>using namespace std;class Test {public: void Question() { cout << "Question1 : who is Feng‘jie?" << endl; } virtual void Answer() {}};class Answer1 : public Test {public: virtual void Answer() { cout << "Answer1‘s answer: " << "C" << endl; }};class Answer2 : public Test {public: virtual void Answer() { cout << "Answer2‘s answer: " << "B" << endl; }};int main() { Test *p_test1 = new Answer1(); p_test1->Question(); p_test1->Answer(); delete p_test1; Test *p_test2 = new Answer2(); p_test2->Question(); p_test2->Answer(); delete p_test2; return 0;}
Design Mode 7: Template Method Mode