ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 示例 ### 概念示例 <details> <summary>main.go</summary> ``` <?php abstract class AbstractClass { final public function templateMethod(): void { $this->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()); ``` </details> <br /> 输出 ``` AbstractClass: baseOperation1 ConcreteClass1:requiredOperations1 AbstractClass: baseOperation2 ConcreteClass1:requiredOperation2 AbstractClass: baseOperation3 AbstractClass: baseOperation1 ConcreteClass2:requiredOperations1 AbstractClass: baseOperation2 ConcreteClass2:hook1 ConcreteClass2:requiredOperations2 AbstractClass: baseOperation3 ``` ## 社交网络的算法框架 在本例中, 模版方法模式定义了在社交网络上发布消息的算法框架。 每个子类都代表一个不同的社交网络, 它们虽以不同方式实现所有步骤, 但却会复用基本的算法。 <details> <summary>main.php</summary> ``` <?php abstract class SocialNetwork { protected $username; protected $password; public function __construct(string $username, string $password) { $this->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); ``` </details> <br />