Application of Template method template mode in C + + design pattern programming _c language

Source: Internet
Author: User

Prepare an abstract class, implement part of the logic in the form of concrete methods and concrete constructs, and then declare some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. This is the purpose of the template method pattern.

Many people may not think that the template method pattern is actually one of the most common patterns in all patterns, and that many people may have used the template method pattern without realizing that they have used the pattern. Template method pattern is based on the basic technology of inheritance code reuse, the structure and usage of template method pattern is also the core of object-oriented design.

Template method patterns require the development of collaboration between abstract classes and designers of specific subclasses. A designer is responsible for the outline and skeleton of an algorithm, while others are responsible for the logical steps of the algorithm. The methods that represent these specific logical steps are called the basic methods (primitive method), and the method of combining these basic methods is called the template approach (template method), which is the name of the design pattern.

Methods in Template Method patterns

The methods in the template method can be divided into two main categories: the Template Method (Template methods) and the basic method (primitive).

Stencil method

A template method is defined in an abstract class, combining the basic operation method together to form a total algorithm or a method of total behavior. This template method is generally defined in an abstract class and is completely inherited by subclasses without modification.

Basic methods

The basic methods can be divided into three kinds: abstract methods (abstraction method), specific methods (concrete method) and hook method (hook methods).

    • Abstract method: An abstract method is declared by an abstract class and implemented by a specific subclass. An abstract method in the C # language is marked with the abstract keyword.
    • Concrete method: A concrete method is declared and implemented by an abstract class, and subclasses are not implemented or displaced. In the C # language, a specific method does not have an abstract keyword.
    • Hook method: A hook method is declared and implemented by an abstract class, and subclasses are extended. Typically, an implementation given by an abstract class is an empty implementation, as the default implementation of a method. (Projects created by the Project Wizard in Visual FoxPro use a Apphook class to implement the task of monitoring project member changes and adjusting the system structure.) The name of the hook method is usually started with do.


implementation of template method pattern

The complete code example: the template method pattern realization is very simple, here in order to facilitate the beginner's study and the reference, will give the complete implementation code (all code uses C + + to implement, and under VC 6.0 test runs).

Code Snippets 1:template.h

Template.h
#ifndef _template_h_
#define _TEMPLATE_H_
class abstractclass{public
  :
  virtual ~ AbstractClass ();
  void Templatemethod ();
  Protected:
  virtual void PrimitiveOperation1 () = 0;
  virtual void PrimitiveOperation2 () = 0;
  AbstractClass ();
  Private:
};
Class Concreteclass1:public abstractclass{public
  :
  ConcreteClass1 ();
  ~concreteclass1 ();
  Protected:
  void PrimitiveOperation1 ();
  void PrimitiveOperation2 ();
  Private:
};
Class Concreteclass2:public abstractclass{public
  :
   ConcreteClass2 ();
  ~concreteclass2 ();
  Protected:
  void PrimitiveOperation1 ();
  void PrimitiveOperation2 ();
  Private:
};
#endif//~_template_h_

Code Snippets 2:template.cpp

#include "Template.h"
#include <iostream>
using namespace std;
Abstractclass::abstractclass () {
}
Abstractclass::~abstractclass () {
}
void AbstractClass:: Templatemethod () {
   this->primitiveoperation1 ();
  This->primitiveoperation2 ();
}
Concreteclass1::concreteclass1 () {
}
Concreteclass1::~concreteclass1 () {
}
void ConcreteClass1::P rimitiveOperation1 () {
  cout<< "ConcreteClass1 ... Primitiveoperat
  ion1 "<<endl;
}
" void ConcreteClass1::P rimitiveOperation2 () {
  cout<< "ConcreteClass1 ... Primitiveoperat
  ion2 "<<endl;
}
" Concreteclass2::concreteclass2 () {
}
Concreteclass2::~concreteclass2 () {
}
void ConcreteClass2::P rimitiveOperation1 () {
cout<< "ConcreteClass2 ... Primitiveoperat
ion1 "<<endl;
}
" void ConcreteClass2::P rimitiveOperation2 () {
  cout<< "ConcreteClass2 ... Primitiveoperat
  ion2 "<<endl;
}"

Code Snippets 3:main.cpp

#include "Template.h"
#include <iostream>
using namespace std;
int main (int argc,char* argv[]) {
  abstractclass* p1 = new ConcreteClass1 ();
  abstractclass* P2 = new ConcreteClass2 ();
  P1->templatemethod ();
  P2->templatemethod ();
  return 0;
}

Code Description: Because the implementation code of the template method pattern is very simple, the explanation is superfluous. The key is to encapsulate the common algorithm (logic) and to implement the algorithm details into subclasses (polymorphic).

The only thing to note is that we define the primitive operations (detail algorithm) as unprotected (Protected) members, only for template method calls (subclasses can).


Applicable Scenarios
implement an invariant part of an algorithm at once and leave the variable behavior to subclasses.
Public behavior in each subclass should be extracted and lumped into a common parent class to avoid code duplication. This is a good example of the "factorization with generalization" described in O p D y k e and J o h n s o n [o J 9 3]. First identify the differences in the existing code and separate the differences into new operations. Finally, replace these different code with a template method that invokes the new operations.
Controls child class extensions. The template method calls only the "H o K" action at a specific point (see the section on effects) so that only extensions are allowed at those points.

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.