Template method pattern

The Template Method (english template method pattern ) is employed in the software development design patterns that can be variable with the sub-steps of an algorithm. It belongs to the category of behavior patterns ( engl. behavioral patterns ). The pattern is one of the so-called quad -band design patterns ( GoF ).

Operation

In stencil methods design pattern the skeleton of an algorithm is defined in an abstract class. The concrete transformation of the individual steps is delegated to subclasses. This provides the possibility to change the individual steps of the algorithm or overwrite without the underlying structure of the algorithm needs to be modified. The template method (English template method) calls abstract methods that are defined only in the subclasses. These methods are also referred to as insertion methods.

In addition Hook - operations can be invoked in the template method at certain locations, whose default implementation in the abstract class does nothing. In this way, you can at predefined points in the algorithm to insert additional functionality.

As a variant of the insertion methods or operations hook may also have a default implementation that can be used by the concrete classes, but do not necessarily.

An example of this is found in the I / O data flow programming of Java. There implements an output stream containing a concrete methodology for writing a byte array. This method uses a method to write a single byte to write the entire array one by one. However, the method for the single byte is still abstract because an OutputStream itself is not specific. Classes like FileOutputStream can implement this method. Then you inherit an already implemented method for writing a byte array.

Policy -Based Design

Policy -Based Design is a more general design patterns, in which not only algorithms but all classes are built stereotyped. Both methods or algorithms as well as stored data, base classes and interfaces are then interchangeable within the skeletal structure. This generally requires Templatemetaprogrammierung as they are in C and D, however, is theoretically also constructs some scripting languages ​​( eval, macros, autoloading of source code etc.)., Although rarely in languages ​​like C # or Java feasible.

Example

The rough process of a board game like chess or Monopoly always looks the same: It is a very welcome and the board is set up. The players are in turn so long on the train until the game is finished. Finally, the winner is determined.

The skeleton always this same algorithm can be implemented in its fundamentals in a template method of an abstract class Game. To implement now a specific game, the abstract methods of the template method have ( that differ for each board game itself ) are in a concrete child class implements. Here is an example in C .

# include # include   using std :: cout; using std :: endl;   class game { protected:    int m_iPlayersCount;      Game ( const int iPLAYERS_COUNT )    {      m_iPlayersCount = iPLAYERS_COUNT;    }      virtual void printGreeting ()    {      cout << " Welcome to our wonderful game! " << Endl;    }      / / Plug-in method:    virtual void initializeBoard () = 0;    make virtual void move ( const int iPlayer ) = 0;    virtual bool gameFinished () = 0;    virtual void print winner () = 0;      / / Hook method:    virtual void takeADrink (const int iPlayer ) {}   public:    / / Stencil method    void playOneGame ()    {      printGreeting ();      initializeBoard ();      int i = 0;        while (! gameFinished ())      {        takeADrink (i); / / Call the Hook ( empty by default )        make move ( i);        i = (i 1) % m_iPlayersCount;      }        print winner ();    } };   Chess class: public Game { protected:    void initializeBoard ()    {      / / ...    }    make void move ( const int iPlayer )    {      cout << " Player" << iPlayer << " ' s turn in chess game " << endl;      / / ...    }    bool gameFinished ()    {      / / ...    }    void print winner ()    {      / / ...    }   public:    Chess (): Game ( 2 ) {} };   Monopoly class: public Game { protected:    void printGreeting ()    {      cout << "Welcome to monopoly! " << Endl;    }    void initializeBoard ()    {      / / ...    }    make void move ( const int iPlayer )    {      cout << " Player" << iPlayer << " ' s turn in monopoly game" << endl;      / / ...    }    bool gameFinished ()    {      / / ...    }    void print winner ()    {      / / ...    }   public:    Monopoly (const int iPLAYERS_COUNT ): Game ( iPLAYERS_COUNT ) {} };   DrinkersMonopoly class: public Monopoly { protected:    void printGreeting ()    {      Monopoly printGreeting :: ();      cout << "(The drinkers ' version)" << endl;    }    void takeADrink (const int iPlayer )    {      cout << " Player" << iPlayer << "drinks a glass of whiskey " << endl;    }   public:    DrinkersMonopoly (const int iPLAYERS_COUNT ): Monopoly ( iPLAYERS_COUNT ) {} };   int main (int iArgc, char * pa_Argv []) {    Game * p_MyGame;      if ( iArgc < = 1 | | strcmp ( pa_Argv, "Chess" ) == 0)    {      p_MyGame = new Chess ();    }    else if ( strcmp ( pa_Argv, "Monopoly" ) == 0)    {      p_MyGame = new Monopoly (4);    }    else if ( strcmp ( pa_Argv, " DrinkersMonopoly " ) == 0)    {      p_MyGame = new DrinkersMonopoly (4);    }    else    {      cout << " Unknown game. " << Endl;      return 1;    }      p_MyGame -> playOneGame ();    delete p_MyGame;    return 0; } References

711649
de