From 1569db132891b8423486820535ccfcdd833f36ff Mon Sep 17 00:00:00 2001 From: Trismegiste Date: Sun, 12 May 2013 11:13:14 +0200 Subject: [PATCH 1/3] add a template method --- TemplateMethod/Journey.php | 76 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 TemplateMethod/Journey.php diff --git a/TemplateMethod/Journey.php b/TemplateMethod/Journey.php new file mode 100644 index 0000000..ce93b82 --- /dev/null +++ b/TemplateMethod/Journey.php @@ -0,0 +1,76 @@ +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 depedencies) + 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 + // in this algorithm. + // [abstract] x [3 access] x [final] = 12 combinations, it can be hard ! +} \ No newline at end of file From 1ab184fca76ec476a01f71d879df426a54b96b1e Mon Sep 17 00:00:00 2001 From: Trismegiste Date: Sun, 12 May 2013 11:13:37 +0200 Subject: [PATCH 2/3] make concrete journey --- TemplateMethod/BeachJourney.php | 20 ++++++++++++++++++++ TemplateMethod/CityJouney.php | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 TemplateMethod/BeachJourney.php create mode 100644 TemplateMethod/CityJouney.php diff --git a/TemplateMethod/BeachJourney.php b/TemplateMethod/BeachJourney.php new file mode 100644 index 0000000..cec7bab --- /dev/null +++ b/TemplateMethod/BeachJourney.php @@ -0,0 +1,20 @@ + Date: Sun, 12 May 2013 11:13:56 +0200 Subject: [PATCH 3/3] add tests for this pattern --- Tests/TemplateMethod/JourneyTest.php | 49 ++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Tests/TemplateMethod/JourneyTest.php diff --git a/Tests/TemplateMethod/JourneyTest.php b/Tests/TemplateMethod/JourneyTest.php new file mode 100644 index 0000000..fcfc4e6 --- /dev/null +++ b/Tests/TemplateMethod/JourneyTest.php @@ -0,0 +1,49 @@ +expectOutputRegex('#sun-bathing#'); + $journey->takeATrip(); + } + + public function testCity() + { + $journey = new TemplateMethod\CityJouney(); + $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"; + } + +} \ No newline at end of file