Skip to content

Commit 6177545

Browse files
author
Dominik Liebler
committed
removed ...Interface suffix and added 2nd service to Bridge example
1 parent b0ac02f commit 6177545

12 files changed

+127
-641
lines changed

Structural/Bridge/Formatter.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace DesignPatterns\Structural\Bridge;
4+
5+
interface Formatter
6+
{
7+
public function format(string $text): string;
8+
}

Structural/Bridge/FormatterInterface.php

-8
This file was deleted.

Structural/Bridge/HelloWorldService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class HelloWorldService extends Service
66
{
7-
public function get()
7+
public function get(): string
88
{
99
return $this->implementation->format('Hello World');
1010
}

Structural/Bridge/HtmlFormatter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace DesignPatterns\Structural\Bridge;
44

5-
class HtmlFormatter implements FormatterInterface
5+
class HtmlFormatter implements Formatter
66
{
7-
public function format(string $text)
7+
public function format(string $text): string
88
{
99
return sprintf('<p>%s</p>', $text);
1010
}

Structural/Bridge/PingService.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace DesignPatterns\Structural\Bridge;
4+
5+
class PingService extends Service
6+
{
7+
public function get(): string
8+
{
9+
return $this->implementation->format('pong');
10+
}
11+
}

Structural/Bridge/PlainTextFormatter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace DesignPatterns\Structural\Bridge;
44

5-
class PlainTextFormatter implements FormatterInterface
5+
class PlainTextFormatter implements Formatter
66
{
7-
public function format(string $text)
7+
public function format(string $text): string
88
{
99
return $text;
1010
}

Structural/Bridge/README.rst

+8-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ Code
2525

2626
You can also find this code on `GitHub`_
2727

28-
FormatterInterface.php
28+
Formatter.php
2929

30-
.. literalinclude:: FormatterInterface.php
30+
.. literalinclude:: Formatter.php
3131
:language: php
3232
:linenos:
3333

@@ -55,6 +55,12 @@ HelloWorldService.php
5555
:language: php
5656
:linenos:
5757

58+
PingService.php
59+
60+
.. literalinclude:: PingService.php
61+
:language: php
62+
:linenos:
63+
5864
Test
5965
----
6066

Structural/Bridge/Service.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55
abstract class Service
66
{
77
/**
8-
* @var FormatterInterface
8+
* @var Formatter
99
*/
1010
protected $implementation;
1111

1212
/**
13-
* @param FormatterInterface $printer
13+
* @param Formatter $printer
1414
*/
15-
public function __construct(FormatterInterface $printer)
15+
public function __construct(Formatter $printer)
1616
{
1717
$this->implementation = $printer;
1818
}
1919

2020
/**
21-
* @param FormatterInterface $printer
21+
* @param Formatter $printer
2222
*/
23-
public function setImplementation(FormatterInterface $printer)
23+
public function setImplementation(Formatter $printer)
2424
{
2525
$this->implementation = $printer;
2626
}
2727

28-
abstract public function get();
28+
abstract public function get(): string;
2929
}

Structural/Bridge/Tests/BridgeTest.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99

1010
class BridgeTest extends TestCase
1111
{
12-
public function testCanPrintUsingThePlainTextPrinter()
12+
public function testCanPrintUsingThePlainTextFormatter()
1313
{
1414
$service = new HelloWorldService(new PlainTextFormatter());
15+
1516
$this->assertSame('Hello World', $service->get());
17+
}
18+
19+
public function testCanPrintUsingTheHtmlFormatter()
20+
{
21+
$service = new HelloWorldService(new HtmlFormatter());
1622

17-
// now change the implementation and use the HtmlFormatter instead
18-
$service->setImplementation(new HtmlFormatter());
1923
$this->assertSame('<p>Hello World</p>', $service->get());
2024
}
2125
}

Structural/Bridge/uml/Bridge.uml

+26-16
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Diagram>
33
<ID>PHP</ID>
4-
<OriginalElement>\DesignPatterns\Structural\Bridge\HtmlFormatter</OriginalElement>
4+
<OriginalElement>\DesignPatterns\Structural\Bridge\Service</OriginalElement>
55
<nodes>
6-
<node x="-111.0" y="-111.0">\DesignPatterns\Structural\Bridge\PlainTextFormatter</node>
7-
<node x="-194.0" y="-212.0">\DesignPatterns\Structural\Bridge\FormatterInterface</node>
8-
<node x="88.0" y="-239.0">\DesignPatterns\Structural\Bridge\Service</node>
9-
<node x="121.0" y="-93.0">\DesignPatterns\Structural\Bridge\HelloWorldService</node>
10-
<node x="-278.0" y="-111.0">\DesignPatterns\Structural\Bridge\HtmlFormatter</node>
6+
<node x="151.0" y="366.0">\DesignPatterns\Structural\Bridge\PlainTextFormatter</node>
7+
<node x="79.75" y="265.0">\DesignPatterns\Structural\Bridge\Formatter</node>
8+
<node x="33.75" y="0.0">\DesignPatterns\Structural\Bridge\Service</node>
9+
<node x="164.0" y="169.0">\DesignPatterns\Structural\Bridge\PingService</node>
10+
<node x="0.0" y="169.0">\DesignPatterns\Structural\Bridge\HelloWorldService</node>
11+
<node x="0.0" y="366.0">\DesignPatterns\Structural\Bridge\HtmlFormatter</node>
1112
</nodes>
1213
<notes />
1314
<edges>
1415
<edge source="\DesignPatterns\Structural\Bridge\HelloWorldService" target="\DesignPatterns\Structural\Bridge\Service">
1516
<point x="0.0" y="-25.5" />
16-
<point x="0.0" y="48.0" />
17+
<point x="72.0" y="144.0" />
18+
<point x="88.75" y="144.0" />
19+
<point x="-55.0" y="59.5" />
1720
</edge>
18-
<edge source="\DesignPatterns\Structural\Bridge\PlainTextFormatter" target="\DesignPatterns\Structural\Bridge\FormatterInterface">
21+
<edge source="\DesignPatterns\Structural\Bridge\PingService" target="\DesignPatterns\Structural\Bridge\Service">
1922
<point x="0.0" y="-25.5" />
20-
<point x="-35.5" y="-136.0" />
21-
<point x="-83.0" y="-136.0" />
22-
<point x="37.0" y="25.5" />
23+
<point x="215.5" y="144.0" />
24+
<point x="198.75" y="144.0" />
25+
<point x="55.0" y="59.5" />
2326
</edge>
24-
<edge source="\DesignPatterns\Structural\Bridge\HtmlFormatter" target="\DesignPatterns\Structural\Bridge\FormatterInterface">
27+
<edge source="\DesignPatterns\Structural\Bridge\HtmlFormatter" target="\DesignPatterns\Structural\Bridge\Formatter">
2528
<point x="0.0" y="-25.5" />
26-
<point x="-204.5" y="-136.0" />
27-
<point x="-157.0" y="-136.0" />
28-
<point x="-37.0" y="25.5" />
29+
<point x="65.5" y="341.0" />
30+
<point x="112.5" y="341.0" />
31+
<point x="-32.75" y="25.5" />
32+
</edge>
33+
<edge source="\DesignPatterns\Structural\Bridge\PlainTextFormatter" target="\DesignPatterns\Structural\Bridge\Formatter">
34+
<point x="0.0" y="-25.5" />
35+
<point x="225.0" y="341.0" />
36+
<point x="178.0" y="341.0" />
37+
<point x="32.75" y="25.5" />
2938
</edge>
3039
</edges>
31-
<settings layout="Hierarchic Group" zoom="1.0" x="147.5" y="130.5" />
40+
<settings layout="Hierarchic Group" zoom="1.0" x="144.5" y="130.5" />
3241
<SelectedNodes />
3342
<Categories>
3443
<Category>Fields</Category>
3544
<Category>Constants</Category>
45+
<Category>Constructors</Category>
3646
<Category>Methods</Category>
3747
</Categories>
3848
<VISIBILITY>private</VISIBILITY>

Structural/Bridge/uml/uml.png

-116 Bytes
Loading

0 commit comments

Comments
 (0)