代码改变世界

php设计模式 — 简单工厂模式(静态工厂方法模式)

2014-09-04 09:11  爱情香烟  阅读(4443)  评论(2编辑  收藏  举报

概念

简单工厂模式 【静态工厂方法模式】(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);