PHP设计模式之:工厂模式
工厂模式:
由工厂类根据参数来决定创建出哪一种产品类的实例;
工厂类是指包含了一个专门用来创建其他对象的方法的类。所谓按需分配,传入参数进行选择,返回具体的类。工厂模式的最主要作用就是对象创建的封装、简化创建对象操作。
简单的说,就是调用工厂类的一个方法(传入参数)来得到需要的类;
代码实现:
示例1(最基本的工厂类):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class MyObject { 2 3 4 5 public function __construct(){} 6 7 8 9 public function test(){ 10 11 return '测试'; 12 13 } 14 15 16 17 } 18 19 20 21 class MyFactory { 22 23 24 25 public static function factory(){ 26 27 //返回对象的实例 28 29 return new MyObject(); 30 31 } 32 33 34 35 } 36 37 38 39 //调用工厂类MyFactory中的静态方法,获取类MyObject的实例 40 41 $myobject=MyFactory::factory(); 42 43 echo $myobject->test();
示例2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //简单工厂模式 2 3 /1* 4 5 * 定义运算类 6 7 */ 8 9 abstract class Operation { 10 11 12 13 protected $_NumberA = 0; 14 15 protected $_NumberB = 0; 16 17 protected $_Result = 0; 18 19 20 21 public function __construct($A,$B){ 22 23 $this->_NumberA = $A; 24 25 $this->_NumberB = $B; 26 27 } 28 29 30 31 public function setNumber($A,$B){ 32 33 $this->_NumberA = $A; 34 35 $this->_NumberB = $B; 36 37 } 38 39 40 41 /1* 42 43 protected function clearResult(){ 44 45 $this->_Result = 0; 46 47 } 48 49 */ 50 51 52 53 public function clearResult(){ 54 55 $this->_Result = 0; 56 57 } 58 59 60 61 //抽象方法无方法体 62 63 abstract protected function getResult(); 64 65 66 67 } 68 69 70 71 //继承一个抽象类的时候,子类必须实现抽象类中的所有抽象方法; 72 73 //另外,这些方法的可见性 必须和抽象类中一样(或者更为宽松) 74 75 class OperationAdd extends Operation { 76 77 78 79 public function getResult(){ 80 81 $this->_Result=$this->_NumberA + $this->_NumberB; 82 83 return $this->_Result; 84 85 } 86 87 88 89 } 90 91 92 93 class OperationSub extends Operation { 94 95 96 97 public function getResult(){ 98 99 $this->_Result=$this->_NumberA - $this->_NumberB; 100 101 return $this->_Result; 102 103 } 104 105 106 107 } 108 109 110 111 class OperationMul extends Operation { 112 113 114 115 public function getResult(){ 116 117 $this->_Result=$this->_NumberA * $this->_NumberB; 118 119 return $this->_Result; 120 121 } 122 123 124 125 } 126 127 128 129 class OperationDiv extends Operation { 130 131 132 133 public function getResult(){ 134 135 $this->_Result=$this->_NumberA / $this->_NumberB; 136 137 return $this->_Result; 138 139 } 140 141 142 143 } 144 145 146 147 class OperationFactory { 148 149 150 151 //创建保存实例的静态成员变量 152 153 private static $obj; 154 155 156 157 //创建访问实例的公共的静态方法 158 159 public static function CreateOperation($type,$A,$B){ 160 161 switch($type){ 162 163 case '+': 164 165 self::$obj = new OperationAdd($A,$B); 166 167 break; 168 169 case '-': 170 171 self::$obj = new OperationSub($A,$B); 172 173 break; 174 175 case '*': 176 177 self::$obj = new OperationMul($A,$B); 178 179 break; 180 181 case '/': 182 183 self::$obj = new OperationDiv($A,$B); 184 185 break; 186 187 } 188 189 return self::$obj; 190 191 } 192 193 194 195 } 196 197 198 199 //$obj = OperationFactory::CreateOperation('+'); 200 201 //$obj->setNumber(4,4); 202 203 $obj = OperationFactory::CreateOperation('*',5,6); 204 205 echo $obj->getResult(); 206 207 /1* 208 209 echo '<br>'; 210 211 $obj->clearResult(); 212 213 echo '<br>'; 214 215 echo $obj->_Result; 216 217 */
示例3:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //抽象工厂 2 3 4 5 //青铜会员的打折商品 6 7 class BronzeRebateCommodity { 8 9 //描述 10 11 public $desc = '青铜会员的打折商品'; 12 13 } 14 15 16 17 //白银会员的打折商品 18 19 class SilverRebateCommodity { 20 21 public $desc = '白银会员的打折商品'; 22 23 } 24 25 26 27 //青铜会员的推荐商品 28 29 class BronzeCommendatoryCommodity { 30 31 public $desc = '青铜会员的推荐商品'; 32 33 } 34 35 36 37 //白银会员的推荐商品 38 39 class SilverCommendatoryCommodity { 40 41 public $desc = '白银会员的推荐商品'; 42 43 } 44 45 46 47 //各个工厂的接口 48 49 interface ConcreteFactory { 50 51 //生产对象的方法 52 53 public function create($what); 54 55 } 56 57 58 59 //青铜工厂 60 61 class BronzeFactory implements ConcreteFactory { 62 63 64 65 //生产对象的方法 66 67 public function create($what){ 68 69 $productName = 'Bronze'.$what.'Commodity'; 70 71 return new $productName; 72 73 } 74 75 76 77 } 78 79 80 81 //白银工厂 82 83 class SilverFactory implements ConcreteFactory { 84 85 86 87 //生产对象的方法 88 89 public function create($what){ 90 91 $productName = 'Silver'.$what.'Commodity'; 92 93 return new $productName; 94 95 } 96 97 98 99 } 100 101 102 103 //调度中心 104 105 class CenterFactory { 106 107 108 109 //获取工厂的方法 110 111 public function getFactory($what){ 112 113 $factoryName = $what.'Factory'; 114 115 return new $factoryName; 116 117 } 118 119 120 121 //获取工厂的静态方法 122 123 public static function getFactory2($what){ 124 125 $factoryName = $what.'Factory'; 126 127 return new $factoryName; 128 129 } 130 131 132 133 } 134 135 136 137 //实例化调度中心 138 139 $center = new CenterFactory(); 140 141 //获得一个白银工厂 142 143 $factory = $center->getFactory('Silver'); 144 145 //让白银工厂制造一个推荐商品 146 147 $product = $factory->create('Commendatory'); 148 149 //得到白银会员的推荐商品 150 151 echo $product->desc.'<br>'; 152 153 154 155 //获得一个青铜工厂 156 157 $factory2 = CenterFactory::getFactory2('Bronze'); 158 159 //让青铜工厂制造一个打折商品 160 161 $product2 = $factory2->create('Rebate'); 162 163 //得到青铜会员的推荐商品 164 165 echo $product2->desc;
示例4
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //使用工厂类解析图像文件 2 3 interface IImage { 4 5 6 7 function getWidth(); 8 9 function getHeight(); 10 11 function getData(); 12 13 14 15 } 16 17 18 19 class Image_PNG implements IImage { 20 21 22 23 protected $_width,$_height,$_data; 24 25 26 27 public function __construct($file){ 28 29 $this->_file = $file; 30 31 $this->_parse(); 32 33 } 34 35 36 37 private function _parse(){ 38 39 //完成PNG格式的解析工作 40 41 //并填充$_width,$_height和$_data 42 43 $this->_data = getimagesize($this->_file); 44 45 list($this->_width,$this->_height)=$this->_data; 46 47 } 48 49 50 51 public function getWidth(){ 52 53 return $this->_width; 54 55 } 56 57 58 59 public function getHeight(){ 60 61 return $this->_height; 62 63 } 64 65 66 67 public function getData(){ 68 69 return $this->_data; 70 71 } 72 73 74 75 } 76 77 78 79 class Image_JPEG implements IImage { 80 81 82 83 protected $_width,$_height,$_data; 84 85 86 87 public function __construct($file){ 88 89 $this->_file = $file; 90 91 $this->_parse(); 92 93 } 94 95 96 97 private function _parse(){ 98 99 //完成JPEG格式的解析工作 100 101 //并填充$_width,$_height和$_data 102 103 //$this->_width = imagesx($this->_file); 104 105 //$this->_height = imagesy($this->_file); 106 107 $this->_data = getimagesize($this->_file); 108 109 list($this->_width,$this->_height)=$this->_data; 110 111 } 112 113 114 115 public function getWidth(){ 116 117 return $this->_width; 118 119 } 120 121 122 123 public function getHeight(){ 124 125 return $this->_height; 126 127 } 128 129 130 131 public function getData(){ 132 133 return $this->_data; 134 135 } 136 137 138 139 } 140 141 142 143 //工厂类 144 145 class ImageFactory { 146 147 148 149 public static function factory($file){ 150 151 152 153 $filename = pathinfo($file); 154 155 switch(strtolower($filename['extension'])){ 156 157 case 'jpg': 158 159 $return = new Image_JPEG($file); 160 161 break; 162 163 case 'png': 164 165 $return = new Image_PNG($file); 166 167 break; 168 169 default: 170 171 echo '图片类型不正确'; 172 173 break; 174 175 } 176 177 if($return instanceof IImage){ 178 179 return $return; 180 181 }else{ 182 183 echo '出错了'; 184 185 exit(); 186 187 } 188 189 190 191 } 192 193 194 195 } 196 197 198 199 $image = ImageFactory::factory('images/11.jpg'); 200 201 var_dump($image->getWidth()); 202 203 echo '<br>'; 204 205 print_r($image->getheight()); 206 207 echo '<br>'; 208 209 print_r($image->getData());