The template method pattern in the detailed design pattern and the use of _c language in C + +

Source: Internet
Author: User

Template method pattern is the simplest design pattern in design pattern behavior. You may even use it in practice, but you don't know it is a design pattern.
The template method pattern defines the skeleton of an algorithm in an operation and delays some steps into subclasses. Template methods enable subclasses to redefine certain steps of the algorithm without altering the structure of an algorithm.
role:
abstract Class (AbstractClass): Defines an abstract primitive operation in which specific subclasses redefine them to implement an algorithm, implement a template method, and define the skeleton of an algorithm. The template method invokes not only primitive operations, but also the definition
Specific subclasses (Concreteclass): Implements primitive operations to complete the steps associated with a particular subclass in an algorithm.
UML Diagram:

Example: If you are a teacher, now you have to give your students a final exam paper. There are dozens of students in your class, and you will consider how to design a test roll.
After analysis, it is obvious that most of the students ' papers are consistent, the only inconsistency is the name and answer. The teacher designed a good test paper, just to give a student to fill out the answer to the exam. Students don't need to copy the questions.
So we need to abstract the quiz paper into the base class and leave the students with the answers and names.

Class Testpaper 
{public 
: 
  void Dotestpaper () { 
    studentname (); 
    Testtitleone (); 
    Testtitletwo (); 
  }; 
 
  void Testtitleone () { 
    cout<< "topic One: Will the house price of X country be lowered? "<<endl; 
    Answerone (); 
  } 
 
  void Testtitletwo () { 
    cout<< "topic Two: Tell me about your news broadcast?" "<<endl; 
    Answertwo (); 
  } 
 
  virtual void Answerone () = 0; 
  virtual void answertwo () = 0; 
  virtual void studentname () = 0; 
}; 

Obviously, the above Answerone, Ansertwo,studentname is the place where students answer questions, students do not need to copy down the topic. You just need to implement our three methods.
For example: Little Red's Test paper

Class Xiaohongtestpaper:public Testpaper 
{public 
: 
  void Studentname () { 
    cout<< "name: Xiao Hong" <<endl; 
  } 
  void Answerone () { 
    cout<< "answer: Believe X, believe that the country, next year must come down." "<<endl<<endl; 
  } 
  void Answertwo () { 
    cout<< "answer: News broadcast is my favorite program ah." "<<endl<<endl; 
  } 
}; 

Xiao Zhang's test paper:

Class Xiaozhangtestpaper:public Testpaper 
{public 
: 
  void Studentname () { 
    cout<< "name: Xiao Zhang" < <endl; 
  } 
  void Answerone () { 
    cout<< "A: Oh, or to do your X country dream." "<<endl<<endl; 
  } 
  void Answertwo () { 
    cout<< "answer: I am very happy" <<endl<<endl; 
  } 
; 

Client:

int main (int argc, char* argv[]) 
{ 
  xiaohongtestpaper paper1; 
  Paper1. Dotestpaper (); 
 
  Xiaozhangtestpaper Paper2; 
  Paper2. Dotestpaper (); 
 
  System ("pause"); 
  return 0; 
} 


discussion on the template method

Template method patterns are simple patterns, but they also apply to a wide range of patterns. As explained in the above analysis and implementation of the template method is to implement the algorithm in an inherited way heterogeneous, the key point is to encapsulate the common algorithm in the abstract base class, and the different algorithm details into subclasses to implement.

The template method pattern obtains a reverse control structure effect, which is also a principle DIP in the analysis and design of object-oriented systems (dependency inversion: Dependency inversion principles). The implication is that the parent class invokes the operation of the subclass (the upper module calls the operation of the low-level module), and the low-level module implements the interface of the high-level module declaration. In this way, the control is in the parent class (High level module), but the low-level module relies on the high-level module.

The inherited mandatory constraint relationship also makes the template method pattern less available, and we can see that the primitive Method Primitive1 () for implementations in the Concreteclass class is not reused by other classes. Suppose we want to create a variant of the AbstractClass Anotherabstractclass, and the two are only generic algorithms, whose primitive operations want to reuse the implementation of AbstractClass subclasses. But this is not possible, because Concreteclass inherited from AbstractClass, also inherited the abstractclass of the general algorithm, Anotherabstractclass is not reusable concreteclass implementation, Because the latter is not inherited from the former.

The problem of the template method pattern exposure is also the inherent problem of inheritance, and the policy pattern achieves the same effect as the template method pattern by combining (delegating), the cost of which is the cost of space and time, refer to the Strategy mode resolution for a detailed discussion of the policy 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.