diff --git a/Bridge/Assemble.php b/Bridge/Assemble.php new file mode 100644 index 0000000..d9e25c9 --- /dev/null +++ b/Bridge/Assemble.php @@ -0,0 +1,11 @@ +<?php + +namespace DesignPatterns\Bridge; + +class Assemble implements Workshop { + + public function work() { + print 'Assembled'; + } + +} diff --git a/Bridge/Car.php b/Bridge/Car.php new file mode 100644 index 0000000..fad41c4 --- /dev/null +++ b/Bridge/Car.php @@ -0,0 +1,20 @@ +<?php + +namespace DesignPatterns\Bridge; + +/** + * Refined Abstraction + */ +class Car extends Vehicle { + + public function __construct(Workshop $workShop1, Workshop $workShop2) { + parent::__construct($workShop1, $workShop2); + } + + public function manufacture() { + print 'Car '; + $this->workShop1->work(); + $this->workShop2->work(); + } + +} diff --git a/Bridge/Motorcycle.php b/Bridge/Motorcycle.php new file mode 100644 index 0000000..1eec4e8 --- /dev/null +++ b/Bridge/Motorcycle.php @@ -0,0 +1,20 @@ +<?php + +namespace DesignPatterns\Bridge; + +/** + * Refined Abstraction + */ +class Motorcycle extends Vehicle { + + public function __construct(Workshop $workShop1, Workshop $workShop2) { + parent::__construct($workShop1, $workShop2); + } + + public function manufacture() { + print 'Motorcycle '; + $this->workShop1->work(); + $this->workShop2->work(); + } + +} diff --git a/Bridge/Produce.php b/Bridge/Produce.php new file mode 100644 index 0000000..6e98e66 --- /dev/null +++ b/Bridge/Produce.php @@ -0,0 +1,14 @@ +<?php + +namespace DesignPatterns\Bridge; + +/** + * Concrete Implementation + */ +class Produce implements Workshop { + + public function work() { + print 'Produced '; + } + +} diff --git a/Bridge/README.md b/Bridge/README.md new file mode 100644 index 0000000..fd819ad --- /dev/null +++ b/Bridge/README.md @@ -0,0 +1,6 @@ +## Purpose + +Decouple an abstraction from its implementation so that the two can vary independently (http://en.wikipedia.org/wiki/Bridge_pattern) + + + diff --git a/Bridge/Vehicle.php b/Bridge/Vehicle.php new file mode 100644 index 0000000..474805c --- /dev/null +++ b/Bridge/Vehicle.php @@ -0,0 +1,22 @@ +<?php + +namespace DesignPatterns\Bridge; + +/** + * Abstraction + */ +abstract class Vehicle { + + protected $workShop1; + protected $workShop2; + + protected function __construct(Workshop $workShop1, Workshop $workShop2) { + $this->workShop1 = $workShop1; + $this->workShop2 = $workShop2; + } + + public function manufacture() { + + } + +} diff --git a/Bridge/Workshop.php b/Bridge/Workshop.php new file mode 100644 index 0000000..8011857 --- /dev/null +++ b/Bridge/Workshop.php @@ -0,0 +1,11 @@ +<?php + +namespace DesignPatterns\Bridge; + +/** + * Implementer + */ +interface Workshop { + + public function work(); +} diff --git a/Tests/Bridge/BridgeTest.php b/Tests/Bridge/BridgeTest.php new file mode 100644 index 0000000..89df337 --- /dev/null +++ b/Tests/Bridge/BridgeTest.php @@ -0,0 +1,26 @@ +<?php + +namespace DesignPatterns\Tests\Bridge; + +use DesignPatterns\Bridge\Assemble; +use DesignPatterns\Bridge\Car; +use DesignPatterns\Bridge\Motorcycle; +use DesignPatterns\Bridge\Produce; +use DesignPatterns\Bridge\Vehicle; +use DesignPatterns\Bridge\Workshop; + +class BridgeTest extends \PHPUnit_Framework_TestCase { + + public function testCar() { + $vehicle = new Car(new Produce(), new Assemble()); + $this->expectOutputString('Car Produced Assembled'); + $vehicle->manufacture(); + } + + public function testMotorcycle() { + $vehicle = new Motorcycle(new Produce(), new Assemble()); + $this->expectOutputString('Motorcycle Produced Assembled'); + $vehicle->manufacture(); + } + +}