mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-05-16 11:29:41 +02:00
Implemented Bridge Pattern
This commit is contained in:
parent
978825cb28
commit
feaa8daafb
11
Bridge/Assemble.php
Normal file
11
Bridge/Assemble.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Bridge;
|
||||||
|
|
||||||
|
class Assemble implements Workshop {
|
||||||
|
|
||||||
|
public function work() {
|
||||||
|
print 'Assembled';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
20
Bridge/Car.php
Normal file
20
Bridge/Car.php
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
20
Bridge/Motorcycle.php
Normal file
20
Bridge/Motorcycle.php
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
14
Bridge/Produce.php
Normal file
14
Bridge/Produce.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Bridge;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Concrete Implementation
|
||||||
|
*/
|
||||||
|
class Produce implements Workshop {
|
||||||
|
|
||||||
|
public function work() {
|
||||||
|
print 'Produced ';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
6
Bridge/README.md
Normal file
6
Bridge/README.md
Normal file
@ -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)
|
||||||
|
|
||||||
|
|
||||||
|
|
22
Bridge/Vehicle.php
Normal file
22
Bridge/Vehicle.php
Normal file
@ -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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
11
Bridge/Workshop.php
Normal file
11
Bridge/Workshop.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Bridge;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementer
|
||||||
|
*/
|
||||||
|
interface Workshop {
|
||||||
|
|
||||||
|
public function work();
|
||||||
|
}
|
26
Tests/Bridge/BridgeTest.php
Normal file
26
Tests/Bridge/BridgeTest.php
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user