Opentopia Directory Encyclopedia Tools

Template method pattern

Encyclopedia : T : TE : TEM : Template method pattern


In software engineering, the template method pattern is a design pattern.

A template method defines the skeleton of an algorithm in terms of abstract operations which subclasses override to provide concrete behavior.

First a class is created that provides the basic steps of an algorithm by using abstract methods. Later on, subclasses change the abstract methods to implement real actions. Thus the general algorithm is saved in one place but the concrete steps may be changed by the subclasses.

Example in Java

/**
* An abstract class that is common to several games in
* which players play against the others, but only one is
* playing at a given time.
*/

abstract class Game printWinner(); } }

Now we can extend this class in order to implement existing games:

class Monopoly extends Game

void makePlay(int player)

boolean endOfGame()

void printWinner()

/* Specific declarations for the Monopoly game. */

// ...

}

class Chess extends Game

void makePlay(int player)

boolean endOfGame()

void printWinner()

/* Specific declarations for the Chess game. */

// ...

}

Example in C++

C++ code below let's the client sort in ascending or descending order using BubbleSort. Deriving more classes from BubbleSort may cause the objects to lose communicating behavior. Use Strategy pattern to avoid this problem.

Output:

Archduke
Ceasar
Emperor
President
Prime Minister
Queen
Warlord

Warlord Queen Prime Minister President Emperor Ceasar Archduke

// Template Method.cpp : Defines the entry point for the console application.

#include <string> #include <cstddef> #include <iostream>
class BubbleSort public:   virtual void Sort()          }     }   }
private:     void Swap(const unsigned a, const unsigned b)      virtual bool OutOfOrder(const std::string&, const std::string& )=0; private:     std::string* _Array;   const std::size_t _Size; };
//---------------------------------------------------------------------
class BubbleAscSort : public BubbleSort public:   bool OutOfOrder(const std::string& a, const std::string& b)    };
//----------------------------------------------------------------------
class BubbleDescSort : public BubbleSort public:   bool OutOfOrder(const std::string& a, const std::string& b)    };
//----------------------------------------------------------------------
int main() ;   const std::size_t size = sizeof sLeaderTypes / sizeof *sLeaderTypes;
  //Use BubbleSort and sort in ascending order.   BubbleAscSort bas(sLeaderTypes, size);   bas.Sort();   //Show the results.   for(std::size_t i = 0; i < size; ++i)      std::cout<<std::endl;
  //Use BubbleSort and sort in descending order.   BubbleDescSort bdsc(sLeaderTypes, size);   bdsc.Sort();   //Show the results.   for(std::size_t i = 0; i < size; ++i)      std::cout<<std::endl;

  return 0; }

External links

 


From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.

Search Titles
0123456789
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ?

E-mail this article to:

Personal Message: