mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-11 01:14:01 +02:00
Refactored example of Proxy pattern
This commit is contained in:
24
Structural/Proxy/BankAccountProxy.php
Normal file
24
Structural/Proxy/BankAccountProxy.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user