File tree Expand file tree Collapse file tree 8 files changed +130
-0
lines changed Expand file tree Collapse file tree 8 files changed +130
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace DesignPatterns \Bridge ;
4
+
5
+ class Assemble implements Workshop {
6
+
7
+ public function work () {
8
+ print 'Assembled ' ;
9
+ }
10
+
11
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace DesignPatterns \Bridge ;
4
+
5
+ /**
6
+ * Implementer
7
+ */
8
+ interface Workshop {
9
+
10
+ public function work ();
11
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments