-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,4 @@ How to dev: | |
8. Facade | ||
9. Fluent Interface | ||
10. Flyweight | ||
11. Proxy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Twent\DesignPatterns\Structural\Proxy; | ||
|
||
interface BankAccount | ||
{ | ||
public function deposit(int $amount); | ||
public function getBalance(): int; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Twent\DesignPatterns\Structural\Proxy; | ||
|
||
final class BankAccountProxy extends HeavyBankAccount implements BankAccount | ||
{ | ||
private ?int $balance = null; | ||
|
||
public function getBalance(): int | ||
{ | ||
if (! $this->balance) { | ||
// Call heavy operations | ||
$this->balance = parent::getBalance(); | ||
} | ||
|
||
return $this->balance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Twent\DesignPatterns\Structural\Proxy; | ||
|
||
class HeavyBankAccount implements BankAccount | ||
{ | ||
private array $transactions = []; | ||
|
||
public function deposit(int $amount) | ||
{ | ||
$this->transactions[] = $amount; | ||
} | ||
|
||
public function getBalance(): int | ||
{ | ||
// Здесь ресурсоёмкие расчёты, "дорогое" взаимодействие с БД и т.д. | ||
return array_sum($this->transactions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Structural; | ||
|
||
use Tests\TestCase; | ||
use Twent\DesignPatterns\Structural\Proxy\BankAccountProxy; | ||
|
||
final class ProxyTest extends TestCase | ||
{ | ||
public function testProxyPatternWorksFine(): void | ||
{ | ||
$bankAccount = new BankAccountProxy(); | ||
$bankAccount->deposit(3000); | ||
$this->assertSame(3000, $bankAccount->getBalance()); | ||
|
||
$bankAccount->deposit(5000); | ||
// Heavy operations isn't running again | ||
$this->assertSame(3000, $bankAccount->getBalance()); | ||
} | ||
} |