Template method pattern: Difference between revisions

Content deleted Content added
Line 45:
 
==PHP Example==
<syntaxhighlight lang="php">
abstract class Game
{
abstract protected function initialize();
abstract protected function startPlay();
abstract protected function endPlay();
 
/** Template method */
public final function play()
{
/** Primitive */
$this->initialize();
 
/** Primitive */
$this->startPlay();
 
/** Primitive */
$this->endPlay();
}
}
 
class Mario extends Game
{
protected function initialize()
{
print_r("Mario Game Initialized! Start playing.\n");
}
 
protected function startPlay()
{
print_r("Mario Game Started. Enjoy the game!\n");
}
 
protected function endPlay()
{
print_r("Mario Game Finished!\n");
}
 
}
 
class Tankfight extends Game
{
protected function initialize()
{
print_r("Tankfight Game Initialized! Start playing.\n");
}
 
protected function startPlay()
{
print_r("Tankfight Game Started. Enjoy the game!\n");
}
 
protected function endPlay()
{
print_r("Tankfight Game Finished!\n");
}
 
}
 
$game = new Tankfight();
$game->play();
 
$game = new Mario();
$game->play();
</syntaxhighlight>
 
== See also ==