欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP模板+工厂设计模式

程序员文章站 2022-03-28 23:51:07
...
```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();```