PHP7 Bridge

This commit is contained in:
Dominik Liebler
2016-09-22 20:33:24 +02:00
parent d151e309c5
commit 72f32359c6
16 changed files with 790 additions and 629 deletions

View File

@@ -1,11 +0,0 @@
<?php
namespace DesignPatterns\Structural\Bridge;
class Assemble implements Workshop
{
public function work()
{
echo 'Assembled';
}
}

View File

@@ -1,21 +0,0 @@
<?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()
{
echo 'Car ';
$this->workShop1->work();
$this->workShop2->work();
}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace DesignPatterns\Structural\Bridge;
interface FormatterInterface
{
public function format(string $text);
}

View File

@@ -0,0 +1,11 @@
<?php
namespace DesignPatterns\Structural\Bridge;
class HelloWorldService extends Service
{
public function get()
{
return $this->implementation->format('Hello World');
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace DesignPatterns\Structural\Bridge;
class HtmlFormatter implements FormatterInterface
{
public function format(string $text)
{
return sprintf('<p>%s</p>', $text);
}
}

View File

@@ -1,21 +0,0 @@
<?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()
{
echo 'Motorcycle ';
$this->workShop1->work();
$this->workShop2->work();
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace DesignPatterns\Structural\Bridge;
class PlainTextFormatter implements FormatterInterface
{
public function format(string $text)
{
return $text;
}
}

View File

@@ -1,14 +0,0 @@
<?php
namespace DesignPatterns\Structural\Bridge;
/**
* Concrete Implementation.
*/
class Produce implements Workshop
{
public function work()
{
echo 'Produced ';
}
}

View File

@@ -25,39 +25,33 @@ Code
You can also find these code on `GitHub`_ You can also find these code on `GitHub`_
Workshop.php FormatterInterface.php
.. literalinclude:: Workshop.php .. literalinclude:: FormatterInterface.php
:language: php :language: php
:linenos: :linenos:
Assemble.php PlainTextFormatter.php
.. literalinclude:: Assemble.php .. literalinclude:: PlainTextFormatter.php
:language: php :language: php
:linenos: :linenos:
Produce.php HtmlFormatter.php
.. literalinclude:: Produce.php .. literalinclude:: HtmlFormatter.php
:language: php :language: php
:linenos: :linenos:
Vehicle.php Service.php
.. literalinclude:: Vehicle.php .. literalinclude:: Service.php
:language: php :language: php
:linenos: :linenos:
Motorcycle.php HelloWorldService.php
.. literalinclude:: Motorcycle.php .. literalinclude:: HelloWorldService.php
:language: php
:linenos:
Car.php
.. literalinclude:: Car.php
:language: php :language: php
:linenos: :linenos:

View File

@@ -0,0 +1,29 @@
<?php
namespace DesignPatterns\Structural\Bridge;
abstract class Service
{
/**
* @var FormatterInterface
*/
protected $implementation;
/**
* @param FormatterInterface $printer
*/
public function __construct(FormatterInterface $printer)
{
$this->implementation = $printer;
}
/**
* @param FormatterInterface $printer
*/
public function setImplementation(FormatterInterface $printer)
{
$this->implementation = $printer;
}
abstract public function get();
}

View File

@@ -2,24 +2,19 @@
namespace DesignPatterns\Structural\Bridge\Tests; namespace DesignPatterns\Structural\Bridge\Tests;
use DesignPatterns\Structural\Bridge\Assemble; use DesignPatterns\Structural\Bridge\HelloWorldService;
use DesignPatterns\Structural\Bridge\Car; use DesignPatterns\Structural\Bridge\HtmlFormatter;
use DesignPatterns\Structural\Bridge\Motorcycle; use DesignPatterns\Structural\Bridge\PlainTextFormatter;
use DesignPatterns\Structural\Bridge\Produce;
class BridgeTest extends \PHPUnit_Framework_TestCase class BridgeTest extends \PHPUnit_Framework_TestCase
{ {
public function testCar() public function testCanPrintUsingThePlainTextPrinter()
{ {
$vehicle = new Car(new Produce(), new Assemble()); $service = new HelloWorldService(new PlainTextFormatter());
$this->expectOutputString('Car Produced Assembled'); $this->assertEquals('Hello World', $service->get());
$vehicle->manufacture();
}
public function testMotorcycle() // now change the implemenation and use the HtmlFormatter instead
{ $service->setImplementation(new HtmlFormatter());
$vehicle = new Motorcycle(new Produce(), new Assemble()); $this->assertEquals('<p>Hello World</p>', $service->get());
$this->expectOutputString('Motorcycle Produced Assembled');
$vehicle->manufacture();
} }
} }

View File

@@ -1,20 +0,0 @@
<?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;
}
abstract public function manufacture();
}

View File

@@ -1,11 +0,0 @@
<?php
namespace DesignPatterns\Structural\Bridge;
/**
* Implementer.
*/
interface Workshop
{
public function work();
}

View File

@@ -1,50 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Diagram> <Diagram>
<ID>PHP</ID> <ID>PHP</ID>
<OriginalElement>\DesignPatterns\Structural\Bridge\Vehicle</OriginalElement> <OriginalElement>\DesignPatterns\Structural\Bridge\HtmlFormatter</OriginalElement>
<nodes> <nodes>
<node x="53.5" y="272.0">\DesignPatterns\Structural\Bridge\Workshop</node> <node x="-111.0" y="-111.0">\DesignPatterns\Structural\Bridge\PlainTextFormatter</node>
<node x="290.0" y="159.0">\DesignPatterns\Structural\Bridge\Car</node> <node x="-194.0" y="-212.0">\DesignPatterns\Structural\Bridge\FormatterInterface</node>
<node x="107.0" y="369.0">\DesignPatterns\Structural\Bridge\Produce</node> <node x="88.0" y="-239.0">\DesignPatterns\Structural\Bridge\Service</node>
<node x="0.0" y="159.0">\DesignPatterns\Structural\Bridge\Motorcycle</node> <node x="121.0" y="-93.0">\DesignPatterns\Structural\Bridge\HelloWorldService</node>
<node x="0.0" y="369.0">\DesignPatterns\Structural\Bridge\Assemble</node> <node x="-278.0" y="-111.0">\DesignPatterns\Structural\Bridge\HtmlFormatter</node>
<node x="187.5" y="0.0">\DesignPatterns\Structural\Bridge\Vehicle</node> </nodes>
</nodes> <notes />
<notes /> <edges>
<edges> <edge source="\DesignPatterns\Structural\Bridge\HelloWorldService" target="\DesignPatterns\Structural\Bridge\Service">
<edge source="\DesignPatterns\Structural\Bridge\Car" target="\DesignPatterns\Structural\Bridge\Vehicle"> <point x="0.0" y="-25.5" />
<point x="0.0" y="-34.0" /> <point x="0.0" y="48.0" />
<point x="425.0" y="134.0" /> </edge>
<point x="326.25" y="134.0" /> <edge source="\DesignPatterns\Structural\Bridge\PlainTextFormatter" target="\DesignPatterns\Structural\Bridge\FormatterInterface">
<point x="46.25" y="54.5" /> <point x="0.0" y="-25.5" />
</edge> <point x="-35.5" y="-136.0" />
<edge source="\DesignPatterns\Structural\Bridge\Assemble" target="\DesignPatterns\Structural\Bridge\Workshop"> <point x="-83.0" y="-136.0" />
<point x="0.0" y="-23.5" /> <point x="37.0" y="25.5" />
<point x="43.5" y="344.0" /> </edge>
<point x="75.25" y="344.0" /> <edge source="\DesignPatterns\Structural\Bridge\HtmlFormatter" target="\DesignPatterns\Structural\Bridge\FormatterInterface">
<point x="-21.75" y="23.5" /> <point x="0.0" y="-25.5" />
</edge> <point x="-204.5" y="-136.0" />
<edge source="\DesignPatterns\Structural\Bridge\Motorcycle" target="\DesignPatterns\Structural\Bridge\Vehicle"> <point x="-157.0" y="-136.0" />
<point x="0.0" y="-34.0" /> <point x="-37.0" y="25.5" />
<point x="135.0" y="134.0" /> </edge>
<point x="233.75" y="134.0" /> </edges>
<point x="-46.25" y="54.5" /> <settings layout="Hierarchic Group" zoom="1.0" x="147.5" y="130.5" />
</edge> <SelectedNodes />
<edge source="\DesignPatterns\Structural\Bridge\Produce" target="\DesignPatterns\Structural\Bridge\Workshop"> <Categories>
<point x="0.0" y="-23.5" /> <Category>Fields</Category>
<point x="150.5" y="344.0" /> <Category>Constants</Category>
<point x="118.75" y="344.0" /> <Category>Methods</Category>
<point x="21.75" y="23.5" /> </Categories>
</edge> <VISIBILITY>private</VISIBILITY>
</edges> </Diagram>
<settings layout="Hierarchic Group" zoom="0.8853211009174312" x="280.0" y="208.0" />
<SelectedNodes />
<Categories>
<Category>Fields</Category>
<Category>Constants</Category>
<Category>Constructors</Category>
<Category>Methods</Category>
</Categories>
<VISIBILITY>private</VISIBILITY>
</Diagram>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 40 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 61 KiB

After

Width:  |  Height:  |  Size: 105 KiB