```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();```