Skip to content

Commit 4c0fbf4

Browse files
author
Dominik Liebler
committed
refactored Decorator pattern
1 parent 50cb2ea commit 4c0fbf4

12 files changed

+107
-103
lines changed

Structural/Decorator/Booking.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace DesignPatterns\Structural\Decorator;
4+
5+
interface Booking
6+
{
7+
public function calculatePrice(): int;
8+
9+
public function getDescription(): string;
10+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace DesignPatterns\Structural\Decorator;
4+
5+
abstract class BookingDecorator implements Booking
6+
{
7+
/**
8+
* @var Booking
9+
*/
10+
protected $booking;
11+
12+
public function __construct(Booking $booking)
13+
{
14+
$this->booking = $booking;
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace DesignPatterns\Structural\Decorator;
4+
5+
class DoubleRoomBooking implements Booking
6+
{
7+
public function calculatePrice(): int
8+
{
9+
return 40;
10+
}
11+
12+
public function getDescription(): string
13+
{
14+
return 'double room';
15+
}
16+
}

Structural/Decorator/ExtraBed.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace DesignPatterns\Structural\Decorator;
4+
5+
class ExtraBed extends BookingDecorator
6+
{
7+
private const PRICE = 30;
8+
9+
public function calculatePrice(): int
10+
{
11+
return $this->booking->calculatePrice() + self::PRICE;
12+
}
13+
14+
public function getDescription(): string
15+
{
16+
return $this->booking->getDescription() . ' with extra bed';
17+
}
18+
}

Structural/Decorator/JsonRenderer.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

Structural/Decorator/README.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,33 @@ Code
2525

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

28-
RenderableInterface.php
28+
Booking.php
2929

30-
.. literalinclude:: RenderableInterface.php
30+
.. literalinclude:: Booking.php
3131
:language: php
3232
:linenos:
3333

34-
Webservice.php
34+
BookingDecorator.php
3535

36-
.. literalinclude:: Webservice.php
36+
.. literalinclude:: BookingDecorator.php
3737
:language: php
3838
:linenos:
3939

40-
RendererDecorator.php
40+
DoubleRoomBooking.php
4141

42-
.. literalinclude:: RendererDecorator.php
42+
.. literalinclude:: DoubleRoomBooking.php
4343
:language: php
4444
:linenos:
4545

46-
XmlRenderer.php
46+
ExtraBed.php
4747

48-
.. literalinclude:: XmlRenderer.php
48+
.. literalinclude:: ExtraBed.php
4949
:language: php
5050
:linenos:
5151

52-
JsonRenderer.php
52+
WiFi.php
5353

54-
.. literalinclude:: JsonRenderer.php
54+
.. literalinclude:: WiFi.php
5555
:language: php
5656
:linenos:
5757

Structural/Decorator/RenderableInterface.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

Structural/Decorator/RendererDecorator.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

Structural/Decorator/Tests/DecoratorTest.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,37 @@
22

33
namespace DesignPatterns\Structural\Decorator\Tests;
44

5-
use DesignPatterns\Structural\Decorator;
5+
use DesignPatterns\Structural\Decorator\DoubleRoomBooking;
6+
use DesignPatterns\Structural\Decorator\ExtraBed;
7+
use DesignPatterns\Structural\Decorator\WiFi;
68
use PHPUnit\Framework\TestCase;
79

810
class DecoratorTest extends TestCase
911
{
10-
/**
11-
* @var Decorator\Webservice
12-
*/
13-
private $service;
14-
15-
protected function setUp()
12+
public function testCanCalculatePriceForBasicDoubleRoomBooking()
1613
{
17-
$this->service = new Decorator\Webservice('foobar');
14+
$booking = new DoubleRoomBooking();
15+
16+
$this->assertEquals(40, $booking->calculatePrice());
17+
$this->assertEquals('double room', $booking->getDescription());
1818
}
1919

20-
public function testJsonDecorator()
20+
public function testCanCalculatePriceForDoubleRoomBookingWithWiFi()
2121
{
22-
$service = new Decorator\JsonRenderer($this->service);
22+
$booking = new DoubleRoomBooking();
23+
$booking = new WiFi($booking);
2324

24-
$this->assertEquals('"foobar"', $service->renderData());
25+
$this->assertEquals(42, $booking->calculatePrice());
26+
$this->assertEquals('double room with wifi', $booking->getDescription());
2527
}
2628

27-
public function testXmlDecorator()
29+
public function testCanCalculatePriceForDoubleRoomBookingWithWiFiAndExtraBed()
2830
{
29-
$service = new Decorator\XmlRenderer($this->service);
31+
$booking = new DoubleRoomBooking();
32+
$booking = new WiFi($booking);
33+
$booking = new ExtraBed($booking);
3034

31-
$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><content>foobar</content>', $service->renderData());
35+
$this->assertEquals(72, $booking->calculatePrice());
36+
$this->assertEquals('double room with wifi with extra bed', $booking->getDescription());
3237
}
3338
}

Structural/Decorator/Webservice.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)