Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit feaa8da

Browse files
committed
Implemented Bridge Pattern
1 parent 978825c commit feaa8da

File tree

8 files changed

+130
-0
lines changed

8 files changed

+130
-0
lines changed

Bridge/Assemble.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace DesignPatterns\Bridge;
4+
5+
class Assemble implements Workshop {
6+
7+
public function work() {
8+
print 'Assembled';
9+
}
10+
11+
}

Bridge/Car.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace DesignPatterns\Bridge;
4+
5+
/**
6+
* Refined Abstraction
7+
*/
8+
class Car extends Vehicle {
9+
10+
public function __construct(Workshop $workShop1, Workshop $workShop2) {
11+
parent::__construct($workShop1, $workShop2);
12+
}
13+
14+
public function manufacture() {
15+
print 'Car ';
16+
$this->workShop1->work();
17+
$this->workShop2->work();
18+
}
19+
20+
}

Bridge/Motorcycle.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace DesignPatterns\Bridge;
4+
5+
/**
6+
* Refined Abstraction
7+
*/
8+
class Motorcycle extends Vehicle {
9+
10+
public function __construct(Workshop $workShop1, Workshop $workShop2) {
11+
parent::__construct($workShop1, $workShop2);
12+
}
13+
14+
public function manufacture() {
15+
print 'Motorcycle ';
16+
$this->workShop1->work();
17+
$this->workShop2->work();
18+
}
19+
20+
}

Bridge/Produce.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace DesignPatterns\Bridge;
4+
5+
/**
6+
* Concrete Implementation
7+
*/
8+
class Produce implements Workshop {
9+
10+
public function work() {
11+
print 'Produced ';
12+
}
13+
14+
}

Bridge/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Purpose
2+
3+
Decouple an abstraction from its implementation so that the two can vary independently (http://en.wikipedia.org/wiki/Bridge_pattern)
4+
5+
6+

Bridge/Vehicle.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace DesignPatterns\Bridge;
4+
5+
/**
6+
* Abstraction
7+
*/
8+
abstract class Vehicle {
9+
10+
protected $workShop1;
11+
protected $workShop2;
12+
13+
protected function __construct(Workshop $workShop1, Workshop $workShop2) {
14+
$this->workShop1 = $workShop1;
15+
$this->workShop2 = $workShop2;
16+
}
17+
18+
public function manufacture() {
19+
20+
}
21+
22+
}

Bridge/Workshop.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace DesignPatterns\Bridge;
4+
5+
/**
6+
* Implementer
7+
*/
8+
interface Workshop {
9+
10+
public function work();
11+
}

Tests/Bridge/BridgeTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace DesignPatterns\Tests\Bridge;
4+
5+
use DesignPatterns\Bridge\Assemble;
6+
use DesignPatterns\Bridge\Car;
7+
use DesignPatterns\Bridge\Motorcycle;
8+
use DesignPatterns\Bridge\Produce;
9+
use DesignPatterns\Bridge\Vehicle;
10+
use DesignPatterns\Bridge\Workshop;
11+
12+
class BridgeTest extends \PHPUnit_Framework_TestCase {
13+
14+
public function testCar() {
15+
$vehicle = new Car(new Produce(), new Assemble());
16+
$this->expectOutputString('Car Produced Assembled');
17+
$vehicle->manufacture();
18+
}
19+
20+
public function testMotorcycle() {
21+
$vehicle = new Motorcycle(new Produce(), new Assemble());
22+
$this->expectOutputString('Motorcycle Produced Assembled');
23+
$vehicle->manufacture();
24+
}
25+
26+
}

0 commit comments

Comments
 (0)