2.10. Proxy
2.10.1. Purpose
To interface to anything that is expensive or impossible to duplicate.
2.10.2. Examples
- Doctrine2 uses proxies to implement framework magic (e.g. lazyinitialization) in them, while the user still works with his ownentity classes and will never use nor touch the proxies
2.10.3. UML Diagram
2.10.4. Code
You can also find this code on GitHub
BankAccount.php
- <?php
- namespace DesignPatterns\Structural\Proxy;
- interface BankAccount
- {
- public function deposit(int $amount);
- public function getBalance(): int;
- }
HeavyBankAccount.php
- <?php
- namespace DesignPatterns\Structural\Proxy;
- class HeavyBankAccount implements BankAccount
- {
- /**
- * @var int[]
- */
- private $transactions = [];
- public function deposit(int $amount)
- {
- $this->transactions[] = $amount;
- }
- public function getBalance(): int
- {
- // this is the heavy part, imagine all the transactions even from
- // years and decades ago must be fetched from a database or web service
- // and the balance must be calculated from it
- return array_sum($this->transactions);
- }
- }
BankAccountProxy.php
- <?php
- namespace DesignPatterns\Structural\Proxy;
- class BankAccountProxy extends HeavyBankAccount implements BankAccount
- {
- /**
- * @var int
- */
- private $balance;
- public function getBalance(): int
- {
- // because calculating balance is so expensive,
- // the usage of BankAccount::getBalance() is delayed until it really is needed
- // and will not be calculated again for this instance
- if ($this->balance === null) {
- $this->balance = parent::getBalance();
- }
- return $this->balance;
- }
- }