Skip to content

Commit ea85485

Browse files
author
Dominik Liebler
committed
Refactored example of Proxy pattern
1 parent a494c07 commit ea85485

File tree

10 files changed

+197
-413
lines changed

10 files changed

+197
-413
lines changed

Structural/Proxy/BankAccount.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\Proxy;
4+
5+
interface BankAccount
6+
{
7+
public function deposit(int $amount);
8+
9+
public function getBalance(): int;
10+
}

Structural/Proxy/BankAccountProxy.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace DesignPatterns\Structural\Proxy;
4+
5+
class BankAccountProxy extends HeavyBankAccount implements BankAccount
6+
{
7+
/**
8+
* @var int
9+
*/
10+
private $balance;
11+
12+
public function getBalance(): int
13+
{
14+
// because calculating balance is so expensive,
15+
// the usage of BankAccount::getBalance() is delayed until it really is needed
16+
// and will not be calculated again for this instance
17+
18+
if ($this->balance === null) {
19+
$this->balance = parent::getBalance();
20+
}
21+
22+
return $this->balance;
23+
}
24+
}

Structural/Proxy/HeavyBankAccount.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace DesignPatterns\Structural\Proxy;
4+
5+
class HeavyBankAccount implements BankAccount
6+
{
7+
/**
8+
* @var int[]
9+
*/
10+
private $transactions = [];
11+
12+
public function deposit(int $amount)
13+
{
14+
$this->transactions[] = $amount;
15+
}
16+
17+
public function getBalance(): int
18+
{
19+
// this is the heavy part, imagine all the transactions even from
20+
// years and decades ago must be fetched from a database or web service
21+
// and the balance must be calculated from it
22+
23+
return array_sum($this->transactions);
24+
}
25+
}

Structural/Proxy/README.rst

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

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

28-
Record.php
28+
BankAccount.php
2929

30-
.. literalinclude:: Record.php
30+
.. literalinclude:: BankAccount.php
3131
:language: php
3232
:linenos:
3333

34-
RecordProxy.php
34+
HeavyBankAccount.php
3535

36-
.. literalinclude:: RecordProxy.php
36+
.. literalinclude:: HeavyBankAccount.php
37+
:language: php
38+
:linenos:
39+
40+
BankAccountProxy.php
41+
42+
.. literalinclude:: BankAccountProxy.php
3743
:language: php
3844
:linenos:
3945

Structural/Proxy/Record.php

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

Structural/Proxy/RecordProxy.php

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

Structural/Proxy/Tests/ProxyTest.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@
22

33
namespace DesignPatterns\Structural\Proxy\Tests;
44

5-
use DesignPatterns\Structural\Proxy\RecordProxy;
5+
use DesignPatterns\Structural\Proxy\BankAccountProxy;
66
use PHPUnit\Framework\TestCase;
77

88
class ProxyTest extends TestCase
99
{
10-
public function testWillSetDirtyFlagInProxy()
10+
public function testProxyWillOnlyExecuteExpensiveGetBalanceOnce()
1111
{
12-
$recordProxy = new RecordProxy([]);
13-
$recordProxy->username = 'baz';
12+
$bankAccount = new BankAccountProxy();
13+
$bankAccount->deposit(30);
1414

15-
$this->assertTrue($recordProxy->isDirty());
16-
}
15+
// this time balance is being calculated
16+
$this->assertEquals(0, $bankAccount->getBalance());
1717

18-
public function testProxyIsInstanceOfRecord()
19-
{
20-
$recordProxy = new RecordProxy([]);
21-
$recordProxy->username = 'baz';
18+
// inheritance allows for BankAccountProxy to behave to an outsider exactly like ServerBankAccount
19+
$bankAccount->deposit(50);
2220

23-
$this->assertInstanceOf(RecordProxy::class, $recordProxy);
21+
// this time the previously calculated balance is returned again without re-calculating it
22+
$this->assertEquals(0, $bankAccount->getBalance());
2423
}
2524
}

Structural/Proxy/uml/Proxy.uml

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<Diagram>
3-
<ID>PHP</ID>
4-
<OriginalElement>\DesignPatterns\Structural\Proxy\Record</OriginalElement>
5-
<nodes>
6-
<node x="4.0" y="0.0">\DesignPatterns\Structural\Proxy\Record</node>
7-
<node x="0.0" y="159.0">\DesignPatterns\Structural\Proxy\RecordProxy</node>
8-
</nodes>
9-
<notes />
10-
<edges>
11-
<edge source="\DesignPatterns\Structural\Proxy\RecordProxy" target="\DesignPatterns\Structural\Proxy\Record">
12-
<point x="0.0" y="-54.5" />
13-
<point x="0.0" y="54.5" />
14-
</edge>
15-
</edges>
16-
<settings layout="Hierarchic Group" zoom="1.0" x="81.0" y="134.0" />
17-
<SelectedNodes>
18-
<node>\DesignPatterns\Structural\Proxy\Record</node>
19-
</SelectedNodes>
20-
<Categories>
21-
<Category>Fields</Category>
22-
<Category>Constants</Category>
23-
<Category>Constructors</Category>
24-
<Category>Methods</Category>
25-
</Categories>
26-
<VISIBILITY>private</VISIBILITY>
27-
</Diagram>
28-
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Diagram>
3+
<ID>PHP</ID>
4+
<OriginalElement>\DesignPatterns\Structural\Proxy\BankAccount</OriginalElement>
5+
<nodes>
6+
<node x="7.5" y="282.0">\DesignPatterns\Structural\Proxy\BankAccountProxy</node>
7+
<node x="-58.0" y="123.0">\DesignPatterns\Structural\Proxy\HeavyBankAccount</node>
8+
<node x="45.0" y="0.0">\DesignPatterns\Structural\Proxy\BankAccount</node>
9+
</nodes>
10+
<notes />
11+
<edges>
12+
<edge source="\DesignPatterns\Structural\Proxy\HeavyBankAccount" target="\DesignPatterns\Structural\Proxy\BankAccount">
13+
<point x="0.0" y="-48.0" />
14+
<point x="21.5" y="98.0" />
15+
<point x="84.75" y="98.0" />
16+
<point x="-39.75" y="36.5" />
17+
</edge>
18+
<edge source="\DesignPatterns\Structural\Proxy\BankAccountProxy" target="\DesignPatterns\Structural\Proxy\BankAccount">
19+
<point x="37.5" y="-37.0" />
20+
<point x="120.0" y="243.0" />
21+
<point x="164.5" y="243.0" />
22+
<point x="164.5" y="161.0" />
23+
<point x="165.25" y="105.0" />
24+
<point x="39.75" y="36.5" />
25+
</edge>
26+
<edge source="\DesignPatterns\Structural\Proxy\BankAccountProxy" target="\DesignPatterns\Structural\Proxy\HeavyBankAccount">
27+
<point x="-37.5" y="-37.0" />
28+
<point x="45.0" y="247.0" />
29+
<point x="21.5" y="247.0" />
30+
<point x="0.0" y="48.0" />
31+
</edge>
32+
</edges>
33+
<settings layout="Hierarchic Group" zoom="1.0" x="73.0" y="178.0" />
34+
<SelectedNodes />
35+
<Categories>
36+
<Category>Fields</Category>
37+
<Category>Constants</Category>
38+
<Category>Constructors</Category>
39+
<Category>Methods</Category>
40+
</Categories>
41+
<VISIBILITY>private</VISIBILITY>
42+
</Diagram>
43+

Structural/Proxy/uml/uml.png

27.8 KB
Loading

0 commit comments

Comments
 (0)