为昂贵或者无法复制的资源提供接口。
BankAccount.php
<?php declare(strict_types=1); namespace DesignPatternsStructuralProxy; interface BankAccount { public function deposit(int $amount); public function getBalance(): int; }
HeavyBankAccount.php
<?php declare(strict_types=1); namespace DesignPatternsStructuralProxy; class HeavyBankAccount implements BankAccount { private array $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 declare(strict_types=1); namespace DesignPatternsStructuralProxy; class BankAccountProxy extends HeavyBankAccount implements BankAccount { private ?int $balance = null; 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; } }
ProxyTest.php
<?php declare(strict_types=1); namespace DesignPatternsStructuralProxyTests; use DesignPatternsStructuralProxyBankAccountProxy; use PHPUnitFrameworkTestCase; class ProxyTest extends TestCase { public function testProxyWillOnlyExecuteExpensiveGetBalanceOnce() { $bankAccount = new BankAccountProxy(); $bankAccount->deposit(30); // this time balance is being calculated $this->assertSame(30, $bankAccount->getBalance()); // inheritance allows for BankAccountProxy to behave to an outsider exactly like ServerBankAccount $bankAccount->deposit(50); // this time the previously calculated balance is returned again without re-calculating it $this->assertSame(30, $bankAccount->getBalance()); } }
所有方法都扩展自Moralis.SolanaAPI.accountbalance返回地址的SOL余额。选项:network:要从中获取数据的网络集群。有效...
检查仪表板中的错误日志对于任何Moralis问题,首先要检查的是仪表板中的日志部分。未捕获错误:未启用以太坊的浏览器确保您的浏...
在安装ZooKeeper之前,请确保你的系统是在以下任一操作系统上运行:任意Linux OS- 支持开发和部署。适合演示应用程序。 Windows ...
Kafka在“org.apache.kafka.tools。"下打包的工具。 工具分为系统工具和复制工具。系统工具可以使用运行类脚本从命令行运行系统...
首先让我们开始实现单节点单代理配置,然后我们将我们的设置迁移到单节点多代理配置。希望你现在可以在你的机器上安装 Java,Zoo...