php設計模式之簡單原廠模式詳解

來源:互聯網
上載者:User
本文以執行個體形式較為詳細的介紹了PHP設計模式的簡單原廠模式,對於進行PHP程式設計來說有很好的借鑒作用。具體如下:

一、概念

簡單原廠模式 【靜態Factory 方法模式】(Static Factory Method)
是類的建立模式

原廠模式的幾種形態:

1、簡單原廠模式(Simple Factory)又叫做 靜態Factory 方法模式(Static Factory Method)

2、Factory 方法模式(Factory Method)又叫做 多態性原廠模式(Polymorphic Factory)

3、抽象原廠模式(Abstract Factory)又叫做 工具箱模式(ToolKit)

二、配圖分析:

三、代碼執行個體

該執行個體代碼經過測試可以運行,具體如下:

<?php/** * 一個案例 * * 一個農場,要向市場銷售水果 * 農場裡有三種水果 蘋果、葡萄 * 我們設想:1、水果有多種屬性,每個屬性都有不同,但是,他們有共同的地方 | 生長、種植、收貨、吃 *       2、將來有可能會增加新的水果、我們需要定義一個介面來規範他們必須實現的方法 *       3、我們需要擷取某個水果的類,要從農場主那裡去擷取某個水果的執行個體,來知道如何生長、種植、收貨、吃 */  /** * 虛擬產品介面類 * 定義好需要實現的方法 */ interface fruit{   /**   * 生長   */  public function grow();   /**   * 種植   */  public function plant();   /**   * 收穫   */  public function harvest();   /**   * 吃   */  public function eat();   } /** * 定義具體產品類 蘋果 * 首先,我們要實現所繼承的介面所定義的方法 * 然後定義蘋果所特有的屬性,以及方法 */class apple implements fruit{   //蘋果樹有年齡  private $treeAge;   //蘋果有顏色  private $color;   public function grow(){    echo "grape grow";  }   public function plant(){    echo "grape plant";  }   public function harvest(){    echo "grape harvest";  }   public function eat(){    echo "grape eat";  }   //取蘋果樹的年齡  public function getTreeAge(){    return $this->treeAge;  }   //設定蘋果樹的年齡  public function setTreeAge($age){    $this->treeAge = $age;    return trie;  } } /** * 定義具體產品類 葡萄 * 首先,我們要實現所繼承的介面所定義的方法 * 然後定義葡萄所特有的屬性,以及方法 */class grape implements fruit{   //葡萄是否有籽  private $seedLess;   public function grow(){    echo "apple grow";  }   public function plant(){    echo "apple plant";  }   public function harvest(){    echo "apple harvest";  }   public function eat(){    echo "apple eat";  }   //有無籽取值  public function getSeedLess(){    return $this->seedLess;  }   //設定有籽無籽  public function setSeedLess($seed){    $this->seedLess = $seed;    return true;  }} /** *農場主類 用來擷取執行個體化的水果 * */class farmer{   //定義個靜態Factory 方法  public static function factory($fruitName){    switch ($fruitName) {      case 'apple':        return new apple();        break;      case 'grape':        return new grape();        break;      default:        throw new badFruitException("Error no the fruit", 1);        break;    }  }} class badFruitException extends Exception{  public $msg;  public $errType;  public function __construct($msg = '' , $errType = 1){    $this->msg = $msg;    $this->errType = $errType;  }  } /** * 擷取水果執行個體化的方法 */try{  $appleInstance = farmer::factory('apple');  var_dump($appleInstance);}catch(badFruitException $err){  echo $err->msg . "_______" . $err->errType;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.