this is Bwar in 2009 to write the design pattern C + + implementation, code can be compiled to run, has been existing in their own computer, has been sharing in the team technology sharing, is now moving to the line.
1. Template Method Brief1.1 Purpose
Defines an algorithm skeleton in an operation, and delays some steps into subclasses. Templatemethod allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.
1.2 Applicability
(1) One-time implementation of an invariant part of the algorithm, and the variable homing pigeon network I left the subclass to achieve.
(2) The public behavior of each subclass should be extracted and several into a common parent class to avoid duplication of code.
(3) Control the extension of the subclass.
2. Template method Structure diagram
- AbstractClass: Defines an abstract primitive operation, in which subclasses redefine them to implement the steps of an algorithm, and implement a template method that defines the stock price of an algorithm.
- Concreteclass: Implements primitive operations to complete the steps associated with a particular subclass in the algorithm.
3. Template Method C + + Implementation Example
The template method is used to realize the game data statistic framework. Games often have a lot of clothes, called the Big Zone, MMO games also called the world. Game data statistics will have a lot of data indicators, all data indicators need both global statistics, but also need separate statistics of the regions, these data indicators statistical logical statistical method is exactly the same. We define a statistical framework that applies to all indicators (active, lost, retained, paid, etc.) using the template method, which is left to the subclass implementation. Such a game framework in the 2009 to 2013 in the industry's first game company used in more than 100 kinds of game data statistics, of course, the template method is just the most basic part of the game data statistics framework, a common game data statistics Framework is not so simple.
Run () is a template method, in which the run () method is fixed sequentially calling Clusterinit (), Stat (), Clusterstat (). Clusterinit () completes the statistical initialization, Stat () completes the zonal statistics of each region, and Clusterstat () completes all the large area results to be counted. The entire statistical framework realizes multi-threaded scheduling, but the implementation of the subclass of statistical logic does not need to focus on thread scheduling, and even developers who are completely ignorant of threading can use the statistical framework to develop multi-threaded statistics programs. The Run () template method ensures that Clusterinit () executes only once on the first thread that enters the statistical logic and executes only once (at this point, the other threads are waiting for clusterinit () to complete the blocking state); the Stat () method starts executing at the same time in each thread ; Clusterstat () executes only on the last thread that completes the stat () and executes only once. Statistical logic developers only need to focus on the implementation of these three methods, others to the framework to complete, while the framework is the three way to define the skeleton, to ensure that all statistics are fixed process to go.
Code implementation:
AbstractClass.h:
#ifndef Abstractclass_h_#defineAbstractclass_h_#include<iostream>using namespacestd;classcabstractclass{ Public: Cabstractclass (); Virtual~Cabstractclass (); intRun ();protected: Virtual intStat () =0; Virtual intClusterinit () {return 0; } Virtual intClusterstat () {return 0; } intGetworldid () {cout<<"1"<<Endl; return 1; }};#endif/* Abstractclass_h_ */
AbstractClass.cpp:
#include"AbstractClass.h"Cabstractclass::cabstractclass () {//TODO auto-generated Constructor stubcout <<"abstract class construct"<<Endl;} Cabstractclass::~Cabstractclass () {//TODO auto-generated destructor stubcout <<"Abstract class Destruct"<<Endl;}intCabstractclass::run () {clusterinit (); Stat (); Clusterstat (); return 0;}
ConcreteClass.h:
#ifndef Concreteclass_h_#defineConcreteclass_h_#include"AbstractClass.h"classCconcreteclass: Publiccabstractclass{ Public: Cconcreteclass (); Virtual~Cconcreteclass (); /*virtual int Run () {Stat (); Clusterinit (); Clusterstat (); } */protected: Virtual intStat (); Virtual intClusterinit () {cout<<"cconcreteclass clusterinit ()"<<Endl; } Virtual intClusterstat () {cout<<"cconcreteclass Clusterstat ()"<<Endl; }};#endif/* Concreteclass_h_ */
ConcreteClass.cpp:
#include"ConcreteClass.h"Cconcreteclass::cconcreteclass () {//TODO auto-generated Constructor stubcout <<"Concrete class Construct"<<Endl;} Cconcreteclass::~Cconcreteclass () {//TODO auto-generated destructor stubcout <<"Concrete Class Destruct"<<Endl;}intCconcreteclass::stat () {cout<<"Concreteclass::stat ()"<<Endl;}
TemplateMethodMain.cpp:
#include <iostream>#include"AbstractClass.h"#include"ConcreteClass.h"using namespacestd;intMain () {Cabstractclass* PStat =NewCconcreteclass (); //cconcreteclass* pStat = new Cconcreteclass ();PStat-Run (); DeletePStat; return 0;}
The template method is also widely used in the high-performance C + + asynchronous Communication Framework ( Nebula ), and the CMD, Step and session of the actor in the Nebula framework use the template method.
Design Patterns-C + + implementation of template methods