PHP模板+工厂设计模式

WBOY
發布: 2016-06-20 12:35:04
原創
939 人瀏覽過

```<?php/** * Created by PhpStorm. * User: gewenrui * Date: 16/3/11 * Time: 上午8:48 */namespace  practice;abstract  class Fruit{    protected $apple;    protected $pineapple;    public  function  templateMethod(){         $this->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();```
登入後複製

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板