php設計模式 — 簡單工廠模式(靜態工廠方法模式)


概念

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

工廠模式的幾種形態:
  1、簡單工廠模式(Simple Factory) |又叫做  靜態工廠方法模式(Static Factory Method)
  2、工廠方法模式(Factory Method) |又叫做 多態性工廠模式(Polymorphic Factory)
  3、抽象工廠模式(Abstract Factory) |又叫做 工具箱模式(ToolKit)

配圖

代碼實例1

直接將代碼運行即可,都是測試過的

  1 <?php
  2 
  3 /**
  4  * 一個事例
  5  *
  6  * 一個農場,要向市場銷售水果
  7  * 農場里有三種水果 蘋果、葡萄
  8  * 我們設想:1、水果有多種屬性,每個屬性都有不同,但是,他們有共同的地方 |  生長、種植、收貨、吃
  9  *              2、將來有可能會增加新的水果、我們需要定義一個接口來規范他們必須實現的方法
 10  *              3、我們需要獲取某個水果的類,要從農場主那里去獲取某個水果的實例,來知道如何生長、種植、收貨、吃
 11  */
 12 
 13 
 14 /**
 15  * 虛擬產品接口類
 16  * 定義好需要實現的方法
 17  */
 18 
 19 interface fruit{
 20 
 21     /**
 22      * 生長
 23      */
 24     public function grow();
 25 
 26     /**
 27      * 種植
 28      */
 29     public function plant();
 30 
 31     /**
 32      * 收獲
 33      */
 34     public function harvest();
 35 
 36     /**
 37      * 吃
 38      */
 39     public function eat();
 40     
 41 }
 42 
 43 
 44 /**
 45  * 定義具體產品類 蘋果
 46  * 首先,我們要實現所繼承的接口所定義的方法
 47  * 然后定義蘋果所特有的屬性,以及方法
 48  */
 49 class apple implements fruit{
 50 
 51     //蘋果樹有年齡
 52     private $treeAge;
 53 
 54     //蘋果有顏色
 55     private $color;
 56 
 57     public function grow(){
 58         echo "grape grow";
 59     }
 60 
 61     public function plant(){
 62         echo "grape plant";
 63     }
 64 
 65     public function harvest(){
 66         echo "grape harvest";
 67     }
 68 
 69     public function eat(){
 70         echo "grape eat";
 71     }
 72 
 73     //取蘋果樹的年齡
 74     public function getTreeAge(){
 75         return $this->treeAge;
 76     }
 77 
 78     //設置蘋果樹的年齡
 79     public function setTreeAge($age){
 80         $this->treeAge = $age;
 81         return trie;
 82     }
 83 
 84 }
 85 
 86 /**
 87  * 定義具體產品類 葡萄
 88  * 首先,我們要實現所繼承的接口所定義的方法
 89  * 然后定義葡萄所特有的屬性,以及方法
 90  */
 91 class grape implements fruit{
 92 
 93 
 94     //葡萄是否有籽
 95     private $seedLess;
 96 
 97     public function grow(){
 98         echo "apple grow";
 99     }
100 
101     public function plant(){
102         echo "apple plant";
103     }
104 
105     public function harvest(){
106         echo "apple harvest";
107     }
108 
109     public function eat(){
110         echo "apple eat";
111     }
112 
113     //有無籽取值
114     public function getSeedLess(){
115         return $this->seedLess;
116     }
117 
118     //設置有籽無籽
119     public function setSeedLess($seed){
120         $this->seedLess = $seed;
121         return true;
122     }
123 
124 }
125 
126 
127 /**
128  *農場主類 用來獲取實例化的水果
129  *
130  */
131 class farmer{
132 
133     //定義個靜態工廠方法
134     public static function factory($fruitName){
135         switch ($fruitName) {
136             case 'apple':
137                 return new apple();
138                 break;
139             case 'grape':
140                 return new grape();
141                 break;
142             default:
143                 throw new badFruitException("Error no the fruit", 1);
144                 break;
145         }
146     }
147 }
148 
149 class badFruitException extends Exception{
150     public $msg;
151     public $errType;
152     public function __construct($msg = '' , $errType = 1){
153         $this->msg = $msg;
154         $this->errType = $errType;
155     }    
156 }
157 
158 
159 /**
160  * 獲取水果實例化的方法
161  */
162 try{
163     $appleInstance = farmer::factory('apple');
164     var_dump($appleInstance);
165 }catch(badFruitException $err){
166     echo $err->msg . "_______" . $err->errType;
167 }

 

代碼實例2

1、首先大家要明白,簡單工廠模式有三個角色

  1、抽象角色

  2、具體角色

  3、工廠角色 : 負責獲取某個具體角色的實例

2、下面的事例,是使用抽象類直接創建具體產品實例。省去了工廠角色

  這里有幾個需要注意的點:

  1、抽象類AbstractUser 有一個方法getInstance 這個方法是靜態的,不然我們必須要實例化才能使用,但是它是一個抽象類,不能實例化。所以必須要是靜態的方法

  2、大家還發現getInstance 定義了final ,final的意義就是這個方法不需要被具體類繼承

 1 <?
 2 /*
 3 * 定義了 User接口.
 4 * 和子類 NormalUser,VipUser,InnerUser 
 5 */
 6 //User接口,定義了三個抽象方法.
 7 interface User{
 8     public function getName();
 9     public function setName($_name);
10     public function getDiscount();
11 }
12 
13 abstract class AbstractUser implements User{
14     private $name = ""; //名字
15     protected  $discount = 0; //折扣
16     protected  $grade = "";  //級別
17     
18     final public static function getInstance($userType , $name){
19         if(class_exists($userType)){
20             $instance = new $userType($name);
21             if($instance instanceof self){
22                 return $instance;
23             }
24         }
25         throw new Exception("Error no the userType");
26     }
27 
28     public function __construct($_name){
29         $this->setName($_name);
30     }
31     public function getName(){
32         return $this->name;
33     }
34     public function setName($_name){
35         $this->name = $_name;
36     }
37     public function getDiscount(){
38         return $this->discount;
39     }
40     
41     public function getGrade(){
42         return $this->grade;
43     }
44 }
45 
46 class NormalUser extends AbstractUser  {
47     protected  $discount = 1.0;
48     protected  $grade = "NormalUser";
49 }
50 
51 class VipUser extends AbstractUser {
52     protected  $discount = 0.8;
53     protected  $grade = "VipUser";
54 }
55 
56 class InnerUser extends AbstractUser {
57     protected  $discount = 0.7;
58     protected  $grade = "InnerUser";
59 }
60 
61 
62 $user = AbstractUser::getInstance('NormalUser' , 'zhangsan');
63 var_dump($user);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM