2.2. Bridge
2.2.1. Purpose
Decouple an abstraction from its implementation so that the two can varyindependently.
2.2.2. Examples
2.2.3. UML Diagram
2.2.4. Code
You can also find this code on GitHub
FormatterInterface.php
- <?php
- namespace DesignPatterns\Structural\Bridge;
- interface FormatterInterface
- {
- public function format(string $text);
- }
PlainTextFormatter.php
- <?php
- namespace DesignPatterns\Structural\Bridge;
- class PlainTextFormatter implements FormatterInterface
- {
- public function format(string $text)
- {
- return $text;
- }
- }
HtmlFormatter.php
- <?php
- namespace DesignPatterns\Structural\Bridge;
- class HtmlFormatter implements FormatterInterface
- {
- public function format(string $text)
- {
- return sprintf('<p>%s</p>', $text);
- }
- }
Service.php
- <?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();
- }
HelloWorldService.php
- <?php
- namespace DesignPatterns\Structural\Bridge;
- class HelloWorldService extends Service
- {
- public function get()
- {
- return $this->implementation->format('Hello World');
- }
- }
2.2.5. Test
Tests/BridgeTest.php
- <?php
- namespace DesignPatterns\Structural\Bridge\Tests;
- use DesignPatterns\Structural\Bridge\HelloWorldService;
- use DesignPatterns\Structural\Bridge\HtmlFormatter;
- use DesignPatterns\Structural\Bridge\PlainTextFormatter;
- use PHPUnit\Framework\TestCase;
- class BridgeTest extends TestCase
- {
- public function testCanPrintUsingThePlainTextPrinter()
- {
- $service = new HelloWorldService(new PlainTextFormatter());
- $this->assertEquals('Hello World', $service->get());
- // now change the implementation and use the HtmlFormatter instead
- $service->setImplementation(new HtmlFormatter());
- $this->assertEquals('<p>Hello World</p>', $service->get());
- }
- }