mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 20:20:15 +02:00
cs TemplateMethod
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\TemplateMethod;
|
||||
|
||||
/**
|
||||
@@ -11,10 +7,11 @@ namespace DesignPatterns\TemplateMethod;
|
||||
*/
|
||||
class BeachJourney extends Journey
|
||||
{
|
||||
|
||||
/**
|
||||
* prints what to do to enjoy your vacation
|
||||
*/
|
||||
protected function enjoyVacation()
|
||||
{
|
||||
echo "Swimming and sun-bathing\n";
|
||||
}
|
||||
|
||||
}
|
@@ -1,20 +1,17 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\TemplateMethod;
|
||||
|
||||
/**
|
||||
* CityJouney is a journey in a city
|
||||
* CityJourney is a journey in a city
|
||||
*/
|
||||
class CityJouney extends Journey
|
||||
class CityJourney extends Journey
|
||||
{
|
||||
|
||||
/**
|
||||
* prints what to do in your journey to enjoy vacation
|
||||
*/
|
||||
protected function enjoyVacation()
|
||||
{
|
||||
echo "Eat, drink, take photos and sleep\n";
|
||||
}
|
||||
|
||||
}
|
@@ -1,15 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\TemplateMethod;
|
||||
|
||||
/**
|
||||
* Template Method is a behavioral design pattern.
|
||||
*
|
||||
* Perhaps you have encoutered it many times already. The idea is to let subclasses
|
||||
* Perhaps you have encountered it many times already. The idea is to let subclasses
|
||||
* of this abstract template "finish" the behavior of an algorithm.
|
||||
*
|
||||
* A.k.a the "Hollywood principle" : "" Don't call us, we call you. ""
|
||||
@@ -25,14 +21,13 @@ namespace DesignPatterns\TemplateMethod;
|
||||
*/
|
||||
abstract class Journey
|
||||
{
|
||||
|
||||
/**
|
||||
* This is the public service provided by this class and its subclasses.
|
||||
* Notice it is final to "freeze" the global behavior of algorithm.
|
||||
* If you want to override this contract, make an interface with only takeATrip()
|
||||
* and subclass it.
|
||||
*/
|
||||
public final function takeATrip()
|
||||
final public function takeATrip()
|
||||
{
|
||||
$this->buyAFlight();
|
||||
$this->takePlane();
|
||||
@@ -53,24 +48,27 @@ abstract class Journey
|
||||
*/
|
||||
protected function buyGift()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// this method will be unknown by subclasses (better)
|
||||
/**
|
||||
* this method will be unknown by subclasses (better)
|
||||
*/
|
||||
private function buyAFlight()
|
||||
{
|
||||
echo "Buying a flight\n";
|
||||
}
|
||||
|
||||
// sbclasses will get access to this method but cannot override it and
|
||||
// compromise this algorithm (warning : cause of cyclic depedencies)
|
||||
/**
|
||||
* sbclasses will get access to this method but cannot override it and
|
||||
* compromise this algorithm (warning : cause of cyclic dependencies)
|
||||
*/
|
||||
final protected function takePlane()
|
||||
{
|
||||
echo "Taking the plane\n";
|
||||
}
|
||||
|
||||
// A note regarding the keyword "final" : don't use it when you start coding :
|
||||
// add it after you narrow and know exacly what change and what remain unchanged
|
||||
// add it after you narrow and know exactly what change and what remain unchanged
|
||||
// in this algorithm.
|
||||
// [abstract] x [3 access] x [final] = 12 combinations, it can be hard !
|
||||
}
|
@@ -23,7 +23,7 @@ class JourneyTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testCity()
|
||||
{
|
||||
$journey = new TemplateMethod\CityJouney();
|
||||
$journey = new TemplateMethod\CityJourney();
|
||||
$this->expectOutputRegex('#drink#');
|
||||
$journey->takeATrip();
|
||||
}
|
||||
|
Reference in New Issue
Block a user