removed ...Interface suffix and added 2nd service to Bridge example

This commit is contained in:
Dominik Liebler
2018-11-06 20:33:42 +01:00
parent b0ac02f01e
commit 617754573c
12 changed files with 145 additions and 659 deletions

View File

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

View File

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

View File

@@ -4,7 +4,7 @@ namespace DesignPatterns\Structural\Bridge;
class HelloWorldService extends Service class HelloWorldService extends Service
{ {
public function get() public function get(): string
{ {
return $this->implementation->format('Hello World'); return $this->implementation->format('Hello World');
} }

View File

@@ -2,9 +2,9 @@
namespace DesignPatterns\Structural\Bridge; namespace DesignPatterns\Structural\Bridge;
class HtmlFormatter implements FormatterInterface class HtmlFormatter implements Formatter
{ {
public function format(string $text) public function format(string $text): string
{ {
return sprintf('<p>%s</p>', $text); return sprintf('<p>%s</p>', $text);
} }

View File

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

View File

@@ -2,9 +2,9 @@
namespace DesignPatterns\Structural\Bridge; namespace DesignPatterns\Structural\Bridge;
class PlainTextFormatter implements FormatterInterface class PlainTextFormatter implements Formatter
{ {
public function format(string $text) public function format(string $text): string
{ {
return $text; return $text;
} }

View File

@@ -25,9 +25,9 @@ Code
You can also find this code on `GitHub`_ You can also find this code on `GitHub`_
FormatterInterface.php Formatter.php
.. literalinclude:: FormatterInterface.php .. literalinclude:: Formatter.php
:language: php :language: php
:linenos: :linenos:
@@ -55,6 +55,12 @@ HelloWorldService.php
:language: php :language: php
:linenos: :linenos:
PingService.php
.. literalinclude:: PingService.php
:language: php
:linenos:
Test Test
---- ----

View File

@@ -5,25 +5,25 @@ namespace DesignPatterns\Structural\Bridge;
abstract class Service abstract class Service
{ {
/** /**
* @var FormatterInterface * @var Formatter
*/ */
protected $implementation; protected $implementation;
/** /**
* @param FormatterInterface $printer * @param Formatter $printer
*/ */
public function __construct(FormatterInterface $printer) public function __construct(Formatter $printer)
{ {
$this->implementation = $printer; $this->implementation = $printer;
} }
/** /**
* @param FormatterInterface $printer * @param Formatter $printer
*/ */
public function setImplementation(FormatterInterface $printer) public function setImplementation(Formatter $printer)
{ {
$this->implementation = $printer; $this->implementation = $printer;
} }
abstract public function get(); abstract public function get(): string;
} }

View File

@@ -9,13 +9,17 @@ use PHPUnit\Framework\TestCase;
class BridgeTest extends TestCase class BridgeTest extends TestCase
{ {
public function testCanPrintUsingThePlainTextPrinter() public function testCanPrintUsingThePlainTextFormatter()
{ {
$service = new HelloWorldService(new PlainTextFormatter()); $service = new HelloWorldService(new PlainTextFormatter());
$this->assertSame('Hello World', $service->get());
// now change the implementation and use the HtmlFormatter instead $this->assertSame('Hello World', $service->get());
$service->setImplementation(new HtmlFormatter()); }
public function testCanPrintUsingTheHtmlFormatter()
{
$service = new HelloWorldService(new HtmlFormatter());
$this->assertSame('<p>Hello World</p>', $service->get()); $this->assertSame('<p>Hello World</p>', $service->get());
} }
} }

View File

@@ -1,38 +1,48 @@
<?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\HtmlFormatter</OriginalElement> <OriginalElement>\DesignPatterns\Structural\Bridge\Service</OriginalElement>
<nodes> <nodes>
<node x="-111.0" y="-111.0">\DesignPatterns\Structural\Bridge\PlainTextFormatter</node> <node x="151.0" y="366.0">\DesignPatterns\Structural\Bridge\PlainTextFormatter</node>
<node x="-194.0" y="-212.0">\DesignPatterns\Structural\Bridge\FormatterInterface</node> <node x="79.75" y="265.0">\DesignPatterns\Structural\Bridge\Formatter</node>
<node x="88.0" y="-239.0">\DesignPatterns\Structural\Bridge\Service</node> <node x="33.75" y="0.0">\DesignPatterns\Structural\Bridge\Service</node>
<node x="121.0" y="-93.0">\DesignPatterns\Structural\Bridge\HelloWorldService</node> <node x="164.0" y="169.0">\DesignPatterns\Structural\Bridge\PingService</node>
<node x="-278.0" y="-111.0">\DesignPatterns\Structural\Bridge\HtmlFormatter</node> <node x="0.0" y="169.0">\DesignPatterns\Structural\Bridge\HelloWorldService</node>
<node x="0.0" y="366.0">\DesignPatterns\Structural\Bridge\HtmlFormatter</node>
</nodes> </nodes>
<notes /> <notes />
<edges> <edges>
<edge source="\DesignPatterns\Structural\Bridge\HelloWorldService" target="\DesignPatterns\Structural\Bridge\Service"> <edge source="\DesignPatterns\Structural\Bridge\HelloWorldService" target="\DesignPatterns\Structural\Bridge\Service">
<point x="0.0" y="-25.5" /> <point x="0.0" y="-25.5" />
<point x="0.0" y="48.0" /> <point x="72.0" y="144.0" />
<point x="88.75" y="144.0" />
<point x="-55.0" y="59.5" />
</edge> </edge>
<edge source="\DesignPatterns\Structural\Bridge\PlainTextFormatter" target="\DesignPatterns\Structural\Bridge\FormatterInterface"> <edge source="\DesignPatterns\Structural\Bridge\PingService" target="\DesignPatterns\Structural\Bridge\Service">
<point x="0.0" y="-25.5" /> <point x="0.0" y="-25.5" />
<point x="-35.5" y="-136.0" /> <point x="215.5" y="144.0" />
<point x="-83.0" y="-136.0" /> <point x="198.75" y="144.0" />
<point x="37.0" y="25.5" /> <point x="55.0" y="59.5" />
</edge> </edge>
<edge source="\DesignPatterns\Structural\Bridge\HtmlFormatter" target="\DesignPatterns\Structural\Bridge\FormatterInterface"> <edge source="\DesignPatterns\Structural\Bridge\HtmlFormatter" target="\DesignPatterns\Structural\Bridge\Formatter">
<point x="0.0" y="-25.5" /> <point x="0.0" y="-25.5" />
<point x="-204.5" y="-136.0" /> <point x="65.5" y="341.0" />
<point x="-157.0" y="-136.0" /> <point x="112.5" y="341.0" />
<point x="-37.0" y="25.5" /> <point x="-32.75" y="25.5" />
</edge>
<edge source="\DesignPatterns\Structural\Bridge\PlainTextFormatter" target="\DesignPatterns\Structural\Bridge\Formatter">
<point x="0.0" y="-25.5" />
<point x="225.0" y="341.0" />
<point x="178.0" y="341.0" />
<point x="32.75" y="25.5" />
</edge> </edge>
</edges> </edges>
<settings layout="Hierarchic Group" zoom="1.0" x="147.5" y="130.5" /> <settings layout="Hierarchic Group" zoom="1.0" x="144.5" y="130.5" />
<SelectedNodes /> <SelectedNodes />
<Categories> <Categories>
<Category>Fields</Category> <Category>Fields</Category>
<Category>Constants</Category> <Category>Constants</Category>
<Category>Constructors</Category>
<Category>Methods</Category> <Category>Methods</Category>
</Categories> </Categories>
<VISIBILITY>private</VISIBILITY> <VISIBILITY>private</VISIBILITY>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 40 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 105 KiB

After

Width:  |  Height:  |  Size: 67 KiB