Template pattern (template Mode)

Source: Internet
Author: User
  • Template Method mode:

Prepare an abstract class, implement part of the logic in the form of a specific method and a specific constructor, and then declare 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. The template method mode is a basic technology based on inherited code reuse. The structure and usage of the template method mode is also the core of object-oriented design.

The template method mode requires collaboration between designers who develop abstract classes and specific sub-classes. One designer is responsible for providing the outline and skeleton of an algorithm, while other designers are responsible for providing the logical steps of this algorithm. The method representing these specific logical steps is called the basic method. The method that aggregates these basic methods is called the template method ), the name of this design pattern is from here.

  • Template Method Mode Construction:
-The abstract template role has the following responsibilities:

Defines one or more abstract operations to implement sub-classes. These abstract operations are called basic operations, which constitute a step of top-level logic.

Defines and implements a template method. This template method is generally a specific method, which provides a skeleton of top-level logic, and the logical composition steps are postponed to subclass implementation in the corresponding abstract operations. The top-level logic may also call some specific methods.

-The role of a specific template (concreteclass) has the following responsibilities:

Implement one or more abstract methods defined by the parent class. They constitute a step of top-level logic.

Each abstract template role can have any number of specific template roles, and each specific template role can provide these abstract methods (that is, the composition steps of top-level Logic) so that the implementation of the top-level logic is different.

 

Template. h

 1 //Template.h 2 # ifndef _TEMPLATE_H_ 3 # define _TEMPLATE_H_ 4  5 class AbstractClass 6 { 7 public: 8     AbstractClass(); 9     virtual ~AbstractClass();10     void TemplateMethod();11 protected:12     virtual void PrimitiveOperation1() = 0;13     virtual void PrimitiveOperation2() = 0;14 private:15 };16 17 class ConcreteClass1:public AbstractClass18 {19 public:20     ConcreteClass1();21     ~ConcreteClass1();22 protected:23     void PrimitiveOperation1();24     void PrimitiveOperation2();25 private:26 };27 28 class ConcreteClass2:public AbstractClass29 {30 public:31     ConcreteClass2();32     ~ConcreteClass2();33 protected:34     void PrimitiveOperation1();35     void PrimitiveOperation2();36 private:37 };38 39 # endif
View code

 

Template. cpp

 1 //Template.cpp 2 # include <iostream> 3 # include "Template.h" 4 using namespace std; 5  6 AbstractClass::AbstractClass() 7 { 8     cout << "Construct AbstractClass" << endl; 9 }10 AbstractClass::~AbstractClass()11 {12     cout << "Destruct AbstractClass" << endl;13 }14 void AbstractClass::TemplateMethod()15 {16     this->PrimitiveOperation1();17     this->PrimitiveOperation2();18 }19 20 ConcreteClass1::ConcreteClass1()21 {22     cout << "Construct ConcreteClass1" << endl;23 }24 ConcreteClass1::~ConcreteClass1()25 {26     cout << "Destruct ConcreteClass1" << endl;27 }28 void ConcreteClass1::PrimitiveOperation1()29 {30     cout << "ConcreteClass1::PrimitiveOperation1" << endl;31 }32 void ConcreteClass1::PrimitiveOperation2()33 {34     cout << "ConcreteClass1::PrimitiveOperation2" << endl;35 }36 37 ConcreteClass2::ConcreteClass2()38 {39     cout << "Construct ConcreteClass2" << endl;40 }41 ConcreteClass2::~ConcreteClass2()42 {43     cout << "Destruct ConcreteClass2" << endl;44 }45 void ConcreteClass2::PrimitiveOperation1()46 {47     cout << "ConcreteClass2::PrimitiveOperation1" << endl;48 }49 void ConcreteClass2::PrimitiveOperation2()50 {51     cout << "ConcreteClass2::PrimitiveOperation2" << endl;52 }
View code

 

Main. cpp

 1 //main.cpp 2 # include <iostream> 3 # include "Template.h" 4 using namespace std; 5  6 int main() 7 { 8     AbstractClass *p1 = new ConcreteClass1(); 9     AbstractClass *p2 = new ConcreteClass2();10     p1->TemplateMethod();11     p2->TemplateMethod();12     delete p1;13     delete p2;14     return 0;15 }
View code

 

Result:

[Email protected]: ~ /Projects/template> G ++-o a *. cpp &./
Construct abstractclass
Construct concreteclass1
Construct abstractclass
Construct concreteclass2
Concreteclass1: primitiveoperation1
Concreteclass1: primitiveoperation2
Concreteclass2: primitiveoperation1
Concreteclass2: primitiveoperation2
Destruct concreteclass1
Destruct abstractclass
Destruct concreteclass2
Destruct abstractclass

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.