diff --git a/Structural/Bridge/Formatter.php b/Structural/Bridge/Formatter.php new file mode 100644 index 0000000..7d161bf --- /dev/null +++ b/Structural/Bridge/Formatter.php @@ -0,0 +1,8 @@ +implementation->format('Hello World'); } diff --git a/Structural/Bridge/HtmlFormatter.php b/Structural/Bridge/HtmlFormatter.php index 79e49b5..043bc15 100644 --- a/Structural/Bridge/HtmlFormatter.php +++ b/Structural/Bridge/HtmlFormatter.php @@ -2,9 +2,9 @@ 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('

%s

', $text); } diff --git a/Structural/Bridge/PingService.php b/Structural/Bridge/PingService.php new file mode 100644 index 0000000..f72cf81 --- /dev/null +++ b/Structural/Bridge/PingService.php @@ -0,0 +1,11 @@ +implementation->format('pong'); + } +} diff --git a/Structural/Bridge/PlainTextFormatter.php b/Structural/Bridge/PlainTextFormatter.php index af46283..1c36cb5 100644 --- a/Structural/Bridge/PlainTextFormatter.php +++ b/Structural/Bridge/PlainTextFormatter.php @@ -2,9 +2,9 @@ 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; } diff --git a/Structural/Bridge/README.rst b/Structural/Bridge/README.rst index e228166..4bb6ef5 100644 --- a/Structural/Bridge/README.rst +++ b/Structural/Bridge/README.rst @@ -25,9 +25,9 @@ Code You can also find this code on `GitHub`_ -FormatterInterface.php +Formatter.php -.. literalinclude:: FormatterInterface.php +.. literalinclude:: Formatter.php :language: php :linenos: @@ -55,6 +55,12 @@ HelloWorldService.php :language: php :linenos: +PingService.php + +.. literalinclude:: PingService.php + :language: php + :linenos: + Test ---- diff --git a/Structural/Bridge/Service.php b/Structural/Bridge/Service.php index 06c6252..479b50f 100644 --- a/Structural/Bridge/Service.php +++ b/Structural/Bridge/Service.php @@ -5,25 +5,25 @@ namespace DesignPatterns\Structural\Bridge; abstract class Service { /** - * @var FormatterInterface + * @var Formatter */ protected $implementation; /** - * @param FormatterInterface $printer + * @param Formatter $printer */ - public function __construct(FormatterInterface $printer) + public function __construct(Formatter $printer) { $this->implementation = $printer; } /** - * @param FormatterInterface $printer + * @param Formatter $printer */ - public function setImplementation(FormatterInterface $printer) + public function setImplementation(Formatter $printer) { $this->implementation = $printer; } - abstract public function get(); + abstract public function get(): string; } diff --git a/Structural/Bridge/Tests/BridgeTest.php b/Structural/Bridge/Tests/BridgeTest.php index 4b89495..10e3e58 100644 --- a/Structural/Bridge/Tests/BridgeTest.php +++ b/Structural/Bridge/Tests/BridgeTest.php @@ -9,13 +9,17 @@ use PHPUnit\Framework\TestCase; class BridgeTest extends TestCase { - public function testCanPrintUsingThePlainTextPrinter() + public function testCanPrintUsingThePlainTextFormatter() { $service = new HelloWorldService(new PlainTextFormatter()); - $this->assertSame('Hello World', $service->get()); - // now change the implementation and use the HtmlFormatter instead - $service->setImplementation(new HtmlFormatter()); + $this->assertSame('Hello World', $service->get()); + } + + public function testCanPrintUsingTheHtmlFormatter() + { + $service = new HelloWorldService(new HtmlFormatter()); + $this->assertSame('

Hello World

', $service->get()); } } diff --git a/Structural/Bridge/uml/Bridge.uml b/Structural/Bridge/uml/Bridge.uml index d63905a..7b0eb0f 100644 --- a/Structural/Bridge/uml/Bridge.uml +++ b/Structural/Bridge/uml/Bridge.uml @@ -1,38 +1,48 @@ PHP - \DesignPatterns\Structural\Bridge\HtmlFormatter + \DesignPatterns\Structural\Bridge\Service - \DesignPatterns\Structural\Bridge\PlainTextFormatter - \DesignPatterns\Structural\Bridge\FormatterInterface - \DesignPatterns\Structural\Bridge\Service - \DesignPatterns\Structural\Bridge\HelloWorldService - \DesignPatterns\Structural\Bridge\HtmlFormatter + \DesignPatterns\Structural\Bridge\PlainTextFormatter + \DesignPatterns\Structural\Bridge\Formatter + \DesignPatterns\Structural\Bridge\Service + \DesignPatterns\Structural\Bridge\PingService + \DesignPatterns\Structural\Bridge\HelloWorldService + \DesignPatterns\Structural\Bridge\HtmlFormatter - + + + - + - - - + + + - + - - - + + + + + + + + + - + Fields Constants + Constructors Methods private diff --git a/Structural/Bridge/uml/uml.png b/Structural/Bridge/uml/uml.png index 6de5b02..c5a88e0 100644 Binary files a/Structural/Bridge/uml/uml.png and b/Structural/Bridge/uml/uml.png differ diff --git a/Structural/Bridge/uml/uml.svg b/Structural/Bridge/uml/uml.svg index 9483eab..8a0b7e4 100644 --- a/Structural/Bridge/uml/uml.svg +++ b/Structural/Bridge/uml/uml.svg @@ -1,661 +1,116 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + - - + + - + - - + + - - + + - - + + - - + + - - + + - - + + - - - format(text) + + - - + + - - - + + - - + + - - PlainTextFormatter + + - - PlainTextFormatter + + - - + + - - - - - - - - - - - - - - - - - - format(text) - - - - - - - - - - - - - PlainTextFormatter - - - PlainTextFormatter - - - - - - - - - - - - - - - - - - - - - - format(text) - - - - - - - - - - - - - FormatterInterface - - - FormatterInterface - - - - - - - - - - - - - - - - - - - - - - format(text) - - - - - - - - - - - - - FormatterInterface - - - FormatterInterface - - - - - - - - - - - - - - - - - - - - - - - - - implementation - - - - - - - - - - - - - - - - setImplementation(printer) - - - - - - - - - - - - - get() - - - - - - - - - - - - - Service - - - Service - - - - - - - - - - - - - - - - - - - - - - implementation - - - - - - - - - - - - - - - - - - - setImplementation(printer) - - - - - - - - - - - - - - - - get() - - - - - - - - - - - - - Service - - - Service - - - - - - - - - - - - - - - - - - - - - - get() - - - - - - - - - - - - - HelloWorldService - - - HelloWorldService - - - - - - - - - - - - - - - - - - - - - - get() - - - - - - - - - - - - - HelloWorldService - - - HelloWorldService - - - - - - - - - - - - - - - - - - - - - - format(text) - - - - - - - - - - - - - HtmlFormatter - - - HtmlFormatter - - - - - - - - - - - - - - - - - - - - - - format(text) - - - - - - - - - - - - - HtmlFormatter - - - HtmlFormatter - - - - - - - - - - + + + + + + + +