时间:2021-07-01 10:21:17 帮助过:6人阅读
```addApple(); $this->addPineapple(); } abstract protected function addApple(); abstract protected function addPineapple();}//创建一个子类实现父类的模板内的方法class TF extends Fruit{ protected function addApple() { //生成一个工厂 $this->apple = new AppleFactory(); //调用方法 echo $this->apple->doFactory(); } protected function addPineapple() { $this->pineapple = new PineAppleFactory(); echo $this->pineapple->doFactory(); }}class PineAppleFactory extends Creator{ protected function doFacoryMethod() { $product = new PineApple(); return $product->getAttribute(); }}class PineApple implements Product{ private $mfg; public function getAttribute() { $this->mfg = "pineapple"; return $this->mfg; }}abstract class Creator{ abstract protected function doFacoryMethod(); public function doFactory(){ $mfg = $this->doFacoryMethod(); return $mfg; }}//设置一个产品的接口interface Product{ public function getAttribute();}class AppleFactory extends Creator{ protected function doFacoryMethod() { $product = new Apple(); return $product->getAttribute(); }}class Apple implements Product{ private $mfg; public function getAttribute() { $this->mfg = "apple"; return $this->mfg; }}class Client{ public function __construct() { $qq = new TF(); $qq->templateMethod(); }}$data = new Client();```