php 示例

最后更新于:2022-04-02 04:21:37

[TOC] ## 示例 ### 概念示例
main.go ``` baseOperation1(); $this->requiredOperations1(); $this->baseOperation2(); $this->hook1(); $this->requiredOperation2(); $this->baseOperation3(); $this->hook2(); } protected function baseOperation1(): void { echo "AbstractClass: baseOperation1\n"; } protected function baseOperation2(): void { echo "AbstractClass: baseOperation2\n"; } protected function baseOperation3(): void { echo "AbstractClass: baseOperation3\n"; } abstract protected function requiredOperations1(): void; abstract protected function requiredOperation2(): void; protected function hook1(): void { } protected function hook2(): void { } } class ConcreteClass1 extends AbstractClass { protected function requiredOperations1(): void { echo "ConcreteClass1:requiredOperations1\n"; } protected function requiredOperation2(): void { echo "ConcreteClass1:requiredOperation2\n"; } } class ConcreteClass2 extends AbstractClass { protected function requiredOperations1(): void { echo "ConcreteClass2:requiredOperations1\n"; } protected function requiredOperation2(): void { echo "ConcreteClass2:requiredOperations2\n"; } protected function hook1(): void { echo "ConcreteClass2:hook1\n"; } } function clientCode(AbstractClass $class) { // ... $class->templateMethod(); // ... } clientCode(new ConcreteClass1()); echo "\n"; clientCode(new ConcreteClass2()); ```

输出 ``` AbstractClass: baseOperation1 ConcreteClass1:requiredOperations1 AbstractClass: baseOperation2 ConcreteClass1:requiredOperation2 AbstractClass: baseOperation3 AbstractClass: baseOperation1 ConcreteClass2:requiredOperations1 AbstractClass: baseOperation2 ConcreteClass2:hook1 ConcreteClass2:requiredOperations2 AbstractClass: baseOperation3 ``` ## 社交网络的算法框架 在本例中, 模版方法模式定义了在社交网络上发布消息的算法框架。 每个子类都代表一个不同的社交网络, 它们虽以不同方式实现所有步骤, 但却会复用基本的算法。
main.php ``` username = $username; $this->password = $password; } public function post(string $message): bool { if ($this->logIn($this->username, $this->password)) { // Send the post data. All networks have different APIs. $result = $this->sendData($message); // ... $this->logOut(); return $result; } return false; } abstract public function logIn(string $userName, string $password): bool; abstract public function sendData(string $message): bool; abstract public function logOut(): void; } class Facebook extends SocialNetwork { public function logIn(string $userName, string $password): bool { echo "Name: " . $this->username . "\n"; echo "Password: " . str_repeat("*", strlen($this->password)) . "\n"; simulateNetworkLatency(); echo "\n\nFacebook: '" . $this->username . "' has logged in successfully.\n"; return true; } public function sendData(string $message): bool { echo "Facebook: '" . $this->username . "' has posted '" . $message . "'.\n"; return true; } public function logOut(): void { echo "Facebook: '" . $this->username . "' has been logged out.\n"; } } class Twitter extends SocialNetwork { public function logIn(string $userName, string $password): bool { echo "Name: " . $this->username . "\n"; echo "Password: " . str_repeat("*", strlen($this->password)) . "\n"; simulateNetworkLatency(); echo "\n\nTwitter: '" . $this->username . "' has logged in successfully.\n"; return true; } public function sendData(string $message): bool { echo "Twitter: '" . $this->username . "' has posted '" . $message . "'.\n"; return true; } public function logOut(): void { echo "Twitter: '" . $this->username . "' has been logged out.\n"; } } /** * 一个小助手的功能,使等待时间感到真实。 */ function simulateNetworkLatency() { $i = 0; while ($i < 5) { echo "."; sleep(1); $i++; } } /** * The client code. */ echo "Username: \n"; $username = readline(); echo "Password: \n"; $password = readline(); echo "Message: \n"; $message = readline(); echo "\nChoose the social network to post the message:\n" . "1 - Facebook\n" . "2 - Twitter\n"; $choice = readline(); // Now, let's create a proper social network object and send the message. if ($choice == 1) { $network = new Facebook($username, $password); } elseif ($choice == 2) { $network = new Twitter($username, $password); } else { die("Sorry, I'm not sure what you mean by that.\n"); } $network->post($message); ```

';