mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-30 19:50:12 +02:00
PHP7 Bridge
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Structural\Bridge;
|
||||
|
||||
class Assemble implements Workshop
|
||||
{
|
||||
public function work()
|
||||
{
|
||||
echo 'Assembled';
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
8
Structural/Bridge/FormatterInterface.php
Normal file
8
Structural/Bridge/FormatterInterface.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Structural\Bridge;
|
||||
|
||||
interface FormatterInterface
|
||||
{
|
||||
public function format(string $text);
|
||||
}
|
11
Structural/Bridge/HelloWorldService.php
Normal file
11
Structural/Bridge/HelloWorldService.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Structural\Bridge;
|
||||
|
||||
class HelloWorldService extends Service
|
||||
{
|
||||
public function get()
|
||||
{
|
||||
return $this->implementation->format('Hello World');
|
||||
}
|
||||
}
|
11
Structural/Bridge/HtmlFormatter.php
Normal file
11
Structural/Bridge/HtmlFormatter.php
Normal 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);
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
11
Structural/Bridge/PlainTextFormatter.php
Normal file
11
Structural/Bridge/PlainTextFormatter.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Structural\Bridge;
|
||||
|
||||
class PlainTextFormatter implements FormatterInterface
|
||||
{
|
||||
public function format(string $text)
|
||||
{
|
||||
return $text;
|
||||
}
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Structural\Bridge;
|
||||
|
||||
/**
|
||||
* Concrete Implementation.
|
||||
*/
|
||||
class Produce implements Workshop
|
||||
{
|
||||
public function work()
|
||||
{
|
||||
echo 'Produced ';
|
||||
}
|
||||
}
|
@@ -25,39 +25,33 @@ Code
|
||||
|
||||
You can also find these code on `GitHub`_
|
||||
|
||||
Workshop.php
|
||||
FormatterInterface.php
|
||||
|
||||
.. literalinclude:: Workshop.php
|
||||
.. literalinclude:: FormatterInterface.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Assemble.php
|
||||
PlainTextFormatter.php
|
||||
|
||||
.. literalinclude:: Assemble.php
|
||||
.. literalinclude:: PlainTextFormatter.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Produce.php
|
||||
HtmlFormatter.php
|
||||
|
||||
.. literalinclude:: Produce.php
|
||||
.. literalinclude:: HtmlFormatter.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Vehicle.php
|
||||
Service.php
|
||||
|
||||
.. literalinclude:: Vehicle.php
|
||||
.. literalinclude:: Service.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Motorcycle.php
|
||||
HelloWorldService.php
|
||||
|
||||
.. literalinclude:: Motorcycle.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Car.php
|
||||
|
||||
.. literalinclude:: Car.php
|
||||
.. literalinclude:: HelloWorldService.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
|
29
Structural/Bridge/Service.php
Normal file
29
Structural/Bridge/Service.php
Normal 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();
|
||||
}
|
@@ -2,24 +2,19 @@
|
||||
|
||||
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;
|
||||
use DesignPatterns\Structural\Bridge\HelloWorldService;
|
||||
use DesignPatterns\Structural\Bridge\HtmlFormatter;
|
||||
use DesignPatterns\Structural\Bridge\PlainTextFormatter;
|
||||
|
||||
class BridgeTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testCar()
|
||||
public function testCanPrintUsingThePlainTextPrinter()
|
||||
{
|
||||
$vehicle = new Car(new Produce(), new Assemble());
|
||||
$this->expectOutputString('Car Produced Assembled');
|
||||
$vehicle->manufacture();
|
||||
}
|
||||
$service = new HelloWorldService(new PlainTextFormatter());
|
||||
$this->assertEquals('Hello World', $service->get());
|
||||
|
||||
public function testMotorcycle()
|
||||
{
|
||||
$vehicle = new Motorcycle(new Produce(), new Assemble());
|
||||
$this->expectOutputString('Motorcycle Produced Assembled');
|
||||
$vehicle->manufacture();
|
||||
// now change the implemenation and use the HtmlFormatter instead
|
||||
$service->setImplementation(new HtmlFormatter());
|
||||
$this->assertEquals('<p>Hello World</p>', $service->get());
|
||||
}
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Structural\Bridge;
|
||||
|
||||
/**
|
||||
* Implementer.
|
||||
*/
|
||||
interface Workshop
|
||||
{
|
||||
public function work();
|
||||
}
|
@@ -1,50 +1,40 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Diagram>
|
||||
<ID>PHP</ID>
|
||||
<OriginalElement>\DesignPatterns\Structural\Bridge\Vehicle</OriginalElement>
|
||||
<nodes>
|
||||
<node x="53.5" y="272.0">\DesignPatterns\Structural\Bridge\Workshop</node>
|
||||
<node x="290.0" y="159.0">\DesignPatterns\Structural\Bridge\Car</node>
|
||||
<node x="107.0" y="369.0">\DesignPatterns\Structural\Bridge\Produce</node>
|
||||
<node x="0.0" y="159.0">\DesignPatterns\Structural\Bridge\Motorcycle</node>
|
||||
<node x="0.0" y="369.0">\DesignPatterns\Structural\Bridge\Assemble</node>
|
||||
<node x="187.5" y="0.0">\DesignPatterns\Structural\Bridge\Vehicle</node>
|
||||
</nodes>
|
||||
<notes />
|
||||
<edges>
|
||||
<edge source="\DesignPatterns\Structural\Bridge\Car" target="\DesignPatterns\Structural\Bridge\Vehicle">
|
||||
<point x="0.0" y="-34.0" />
|
||||
<point x="425.0" y="134.0" />
|
||||
<point x="326.25" y="134.0" />
|
||||
<point x="46.25" y="54.5" />
|
||||
</edge>
|
||||
<edge source="\DesignPatterns\Structural\Bridge\Assemble" target="\DesignPatterns\Structural\Bridge\Workshop">
|
||||
<point x="0.0" y="-23.5" />
|
||||
<point x="43.5" y="344.0" />
|
||||
<point x="75.25" y="344.0" />
|
||||
<point x="-21.75" y="23.5" />
|
||||
</edge>
|
||||
<edge source="\DesignPatterns\Structural\Bridge\Motorcycle" target="\DesignPatterns\Structural\Bridge\Vehicle">
|
||||
<point x="0.0" y="-34.0" />
|
||||
<point x="135.0" y="134.0" />
|
||||
<point x="233.75" y="134.0" />
|
||||
<point x="-46.25" y="54.5" />
|
||||
</edge>
|
||||
<edge source="\DesignPatterns\Structural\Bridge\Produce" target="\DesignPatterns\Structural\Bridge\Workshop">
|
||||
<point x="0.0" y="-23.5" />
|
||||
<point x="150.5" y="344.0" />
|
||||
<point x="118.75" y="344.0" />
|
||||
<point x="21.75" y="23.5" />
|
||||
</edge>
|
||||
</edges>
|
||||
<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>
|
||||
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Diagram>
|
||||
<ID>PHP</ID>
|
||||
<OriginalElement>\DesignPatterns\Structural\Bridge\HtmlFormatter</OriginalElement>
|
||||
<nodes>
|
||||
<node x="-111.0" y="-111.0">\DesignPatterns\Structural\Bridge\PlainTextFormatter</node>
|
||||
<node x="-194.0" y="-212.0">\DesignPatterns\Structural\Bridge\FormatterInterface</node>
|
||||
<node x="88.0" y="-239.0">\DesignPatterns\Structural\Bridge\Service</node>
|
||||
<node x="121.0" y="-93.0">\DesignPatterns\Structural\Bridge\HelloWorldService</node>
|
||||
<node x="-278.0" y="-111.0">\DesignPatterns\Structural\Bridge\HtmlFormatter</node>
|
||||
</nodes>
|
||||
<notes />
|
||||
<edges>
|
||||
<edge source="\DesignPatterns\Structural\Bridge\HelloWorldService" target="\DesignPatterns\Structural\Bridge\Service">
|
||||
<point x="0.0" y="-25.5" />
|
||||
<point x="0.0" y="48.0" />
|
||||
</edge>
|
||||
<edge source="\DesignPatterns\Structural\Bridge\PlainTextFormatter" target="\DesignPatterns\Structural\Bridge\FormatterInterface">
|
||||
<point x="0.0" y="-25.5" />
|
||||
<point x="-35.5" y="-136.0" />
|
||||
<point x="-83.0" y="-136.0" />
|
||||
<point x="37.0" y="25.5" />
|
||||
</edge>
|
||||
<edge source="\DesignPatterns\Structural\Bridge\HtmlFormatter" target="\DesignPatterns\Structural\Bridge\FormatterInterface">
|
||||
<point x="0.0" y="-25.5" />
|
||||
<point x="-204.5" y="-136.0" />
|
||||
<point x="-157.0" y="-136.0" />
|
||||
<point x="-37.0" y="25.5" />
|
||||
</edge>
|
||||
</edges>
|
||||
<settings layout="Hierarchic Group" zoom="1.0" x="147.5" y="130.5" />
|
||||
<SelectedNodes />
|
||||
<Categories>
|
||||
<Category>Fields</Category>
|
||||
<Category>Constants</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 |
Reference in New Issue
Block a user