mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-10 08:54:03 +02:00
start a restructure
This commit is contained in:
17
Behavioral/TemplateMethod/BeachJourney.php
Normal file
17
Behavioral/TemplateMethod/BeachJourney.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\TemplateMethod;
|
||||
|
||||
/**
|
||||
* BeachJourney is vacation at the beach
|
||||
*/
|
||||
class BeachJourney extends Journey
|
||||
{
|
||||
/**
|
||||
* prints what to do to enjoy your vacation
|
||||
*/
|
||||
protected function enjoyVacation()
|
||||
{
|
||||
echo "Swimming and sun-bathing\n";
|
||||
}
|
||||
}
|
17
Behavioral/TemplateMethod/CityJourney.php
Normal file
17
Behavioral/TemplateMethod/CityJourney.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\TemplateMethod;
|
||||
|
||||
/**
|
||||
* CityJourney is a journey in a city
|
||||
*/
|
||||
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";
|
||||
}
|
||||
}
|
60
Behavioral/TemplateMethod/Journey.php
Normal file
60
Behavioral/TemplateMethod/Journey.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
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.
|
||||
*/
|
||||
final public function takeATrip()
|
||||
{
|
||||
$this->buyAFlight();
|
||||
$this->takePlane();
|
||||
$this->enjoyVacation();
|
||||
$this->buyGift();
|
||||
$this->takePlane();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method must be implemented, this is the key-feature of this pattern
|
||||
*/
|
||||
abstract protected function enjoyVacation();
|
||||
|
||||
/**
|
||||
* This method is also part of the algorithm but it is optional.
|
||||
* This is an "adapter" (do not confuse with the Adapter pattern, not related)
|
||||
* You can override it only if you need to.
|
||||
*/
|
||||
protected function buyGift()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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 exactly what change and what remain unchanged
|
||||
// in this algorithm.
|
||||
// [abstract] x [3 access] x [final] = 12 combinations, it can be hard !
|
||||
}
|
14
Behavioral/TemplateMethod/README.md
Normal file
14
Behavioral/TemplateMethod/README.md
Normal file
@@ -0,0 +1,14 @@
|
||||
# Template Method
|
||||
|
||||
## Purpose
|
||||
|
||||
Template Method is a behavioral design pattern.
|
||||
|
||||
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." This class is not called by subclasses but the inverse.
|
||||
How? With abstraction of course.
|
||||
|
||||
In other words, this is a skeleton of algorithm, well-suited for framework libraries. The user has just to implement one method and the superclass do the job.
|
||||
|
||||
It is an easy way to decouple concrete classes and reduce copy-paste, that's why you'll find it everywhere.
|
45
Behavioral/TemplateMethod/Test/JourneyTest.php
Normal file
45
Behavioral/TemplateMethod/Test/JourneyTest.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Tests\TemplateMethod;
|
||||
|
||||
use DesignPatterns\TemplateMethod;
|
||||
|
||||
/**
|
||||
* JourneyTest tests all journeys
|
||||
*/
|
||||
class JourneyTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testBeach()
|
||||
{
|
||||
$journey = new TemplateMethod\BeachJourney();
|
||||
$this->expectOutputRegex('#sun-bathing#');
|
||||
$journey->takeATrip();
|
||||
}
|
||||
|
||||
public function testCity()
|
||||
{
|
||||
$journey = new TemplateMethod\CityJourney();
|
||||
$this->expectOutputRegex('#drink#');
|
||||
$journey->takeATrip();
|
||||
}
|
||||
|
||||
/**
|
||||
* How to test an abstract template method with PHPUnit
|
||||
*/
|
||||
public function testLasVegas()
|
||||
{
|
||||
$journey = $this->getMockForAbstractClass('DesignPatterns\TemplateMethod\Journey');
|
||||
$journey->expects($this->once())
|
||||
->method('enjoyVacation')
|
||||
->will($this->returnCallback(array($this, 'mockUpVacation')));
|
||||
$this->expectOutputRegex('#Las Vegas#');
|
||||
$journey->takeATrip();
|
||||
}
|
||||
|
||||
public function mockUpVacation()
|
||||
{
|
||||
echo "Fear and loathing in Las Vegas\n";
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user