2.2. Bridge

2.2.1. Purpose

Decouple an abstraction from its implementation so that the two can vary independently.

2.2.1.1. Sample:

2.2.2. UML Diagram

Alt Bridge UML Diagram

2.2.3. Code

You can also find these code on GitHub

Workshop.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?php

namespace DesignPatterns\Structural\Bridge;

/**
 * Implementer
 */
interface Workshop
{

    public function work();
}

Assemble.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?php

namespace DesignPatterns\Structural\Bridge;

class Assemble implements Workshop
{

    public function work()
    {
        print 'Assembled';
    }
}

Produce.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php

namespace DesignPatterns\Structural\Bridge;

/**
 * Concrete Implementation
 */
class Produce implements Workshop
{

    public function work()
    {
        print 'Produced ';
    }
}

Vehicle.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php

namespace DesignPatterns\Structural\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()
    {
    }
}

Motorcycle.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

namespace DesignPatterns\Structural\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();
    }
}

Car.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

namespace DesignPatterns\Structural\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();
    }
}

2.2.4. Test

Tests/BridgeTest.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php

namespace DesignPatterns\Structural\Bridge\Tests;

use DesignPatterns\Structural\Bridge\Assemble;
use DesignPatterns\Structural\Bridge\Car;
use DesignPatterns\Structural\Bridge\Motorcycle;
use DesignPatterns\Structural\Bridge\Produce;

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();
    }
}