首頁 > 後端開發 > php教程 > PHP設計模式:工廠模式

PHP設計模式:工廠模式

高洛峰
發布: 2023-03-03 15:10:01
原創
1850 人瀏覽過

工廠模式:

由工廠類別根據參數來決定創建出哪一種產品類別的實例;

工廠類別是指包含了一個專門用來建立其他物件的方法的類別。所謂按需分配,傳入參數進行選擇,傳回具體的類別。工廠模式最主要的功能就是物件創建的封裝、簡化創建物件操作。

簡單的說,就是呼叫工廠類別的一個方法(傳入參數)來得到需要的類別;

程式碼實作:

範例1(最基本的工廠類別):

<?php
 
class MyObject {
 
public function __construct(){}
 
public function test(){
return &#39;测试&#39;;
}
 
}
 
class MyFactory {
 
public static function factory(){
//返回对象的实例
return new MyObject();
}
 
}
 
//调用工厂类MyFactory中的静态方法,获取类MyObject的实例
$myobject=MyFactory::factory();
echo $myobject->test();
登入後複製

 

範例2:

<?php
//简单工厂模式
/1*
 * 定义运算类
 */
abstract class Operation {
 
protected $_NumberA = 0;
protected $_NumberB = 0;
protected $_Result  = 0;
 
public function __construct($A,$B){
$this->_NumberA = $A;
$this->_NumberB = $B;
}
 
public function setNumber($A,$B){
$this->_NumberA = $A;
$this->_NumberB = $B;
}
 
/1*
protected function clearResult(){
$this->_Result  = 0;
}
*/
 
public function clearResult(){
$this->_Result  = 0;
}
 
//抽象方法无方法体
abstract protected function getResult();
 
}
 
//继承一个抽象类的时候,子类必须实现抽象类中的所有抽象方法;
//另外,这些方法的可见性 必须和抽象类中一样(或者更为宽松)
class OperationAdd extends Operation {
 
public function getResult(){
$this->_Result=$this->_NumberA + $this->_NumberB;
return $this->_Result;
}
 
}
 
class OperationSub extends Operation {
 
public function getResult(){
$this->_Result=$this->_NumberA - $this->_NumberB;
return $this->_Result;
}
 
}
 
class OperationMul extends Operation {
 
public function getResult(){
$this->_Result=$this->_NumberA * $this->_NumberB;
return $this->_Result;
}
 
}
 
class OperationDiv extends Operation {
 
public function getResult(){
$this->_Result=$this->_NumberA / $this->_NumberB;
return $this->_Result;
}
 
}
 
class OperationFactory {
 
//创建保存实例的静态成员变量
private static $obj;
 
//创建访问实例的公共的静态方法
public static function CreateOperation($type,$A,$B){
switch($type){
case &#39;+&#39;:
self::$obj = new OperationAdd($A,$B);
break;
case &#39;-&#39;:
self::$obj = new OperationSub($A,$B);
break;
case &#39;*&#39;:
self::$obj = new OperationMul($A,$B);
break;
case &#39;/&#39;:
self::$obj = new OperationDiv($A,$B);
break;
}
return self::$obj;
}
 
}
 
//$obj = OperationFactory::CreateOperation(&#39;+&#39;);
//$obj->setNumber(4,4);
$obj = OperationFactory::CreateOperation(&#39;*&#39;,5,6);
echo $obj->getResult();
/1*
echo &#39;<br>&#39;;
$obj->clearResult();
echo &#39;<br>&#39;;
echo $obj->_Result;
*/
登入後複製

 

範例3:

<?php
//抽象工厂
 
//青铜会员的打折商品
class BronzeRebateCommodity {
//描述
public $desc = &#39;青铜会员的打折商品&#39;;
}
 
//白银会员的打折商品
class SilverRebateCommodity {
public $desc = &#39;白银会员的打折商品&#39;;
}
 
//青铜会员的推荐商品
class BronzeCommendatoryCommodity {
public $desc = &#39;青铜会员的推荐商品&#39;;
}
 
//白银会员的推荐商品
class SilverCommendatoryCommodity {
public $desc = &#39;白银会员的推荐商品&#39;;
}
 
//各个工厂的接口
interface ConcreteFactory {
//生产对象的方法
public function create($what);
}
 
//青铜工厂
class BronzeFactory implements ConcreteFactory {
 
//生产对象的方法
public function create($what){
$productName = &#39;Bronze&#39;.$what.&#39;Commodity&#39;;
return new $productName;
}
 
}
 
//白银工厂
class SilverFactory implements ConcreteFactory {
 
//生产对象的方法
public function create($what){
$productName = &#39;Silver&#39;.$what.&#39;Commodity&#39;;
return new $productName;
}
 
}
 
//调度中心
class CenterFactory {
 
//获取工厂的方法
public function getFactory($what){
$factoryName = $what.&#39;Factory&#39;;
return new $factoryName;
}
 
//获取工厂的静态方法
public static function getFactory2($what){
$factoryName = $what.&#39;Factory&#39;;
return new $factoryName;
}
 
}
 
//实例化调度中心
$center  = new CenterFactory();
//获得一个白银工厂
$factory = $center->getFactory(&#39;Silver&#39;);
//让白银工厂制造一个推荐商品
$product = $factory->create(&#39;Commendatory&#39;);
//得到白银会员的推荐商品
echo $product->desc.&#39;<br>&#39;;
 
//获得一个青铜工厂
$factory2 = CenterFactory::getFactory2(&#39;Bronze&#39;);
//让青铜工厂制造一个打折商品
$product2 = $factory2->create(&#39;Rebate&#39;);
//得到青铜会员的推荐商品
echo $product2->desc;
登入後複製

 

範例4:

<?php
//使用工厂类解析图像文件
interface IImage {
 
function getWidth();
function getHeight();
function getData();
 
}
 
class Image_PNG implements IImage {
 
protected $_width,$_height,$_data;
 
public function __construct($file){
$this->_file = $file;
$this->_parse();
}
 
private function _parse(){
//完成PNG格式的解析工作
//并填充$_width,$_height和$_data
$this->_data   = getimagesize($this->_file);
list($this->_width,$this->_height)=$this->_data;
}
 
public function getWidth(){
return $this->_width;
}
 
public function getHeight(){
return $this->_height;
}
 
public function getData(){
return $this->_data;
}
 
}
 
class Image_JPEG implements IImage {
 
protected $_width,$_height,$_data;
 
public function __construct($file){
$this->_file = $file;
$this->_parse();
}
 
private function _parse(){
//完成JPEG格式的解析工作
//并填充$_width,$_height和$_data
//$this->_width  = imagesx($this->_file);
//$this->_height = imagesy($this->_file);
$this->_data   = getimagesize($this->_file);
list($this->_width,$this->_height)=$this->_data;
}
 
public function getWidth(){
return $this->_width;
}
 
public function getHeight(){
return $this->_height;
}
 
public function getData(){
return $this->_data;
}
 
}
 
//工厂类
class ImageFactory {
 
public static function factory($file){
 
$filename = pathinfo($file);
switch(strtolower($filename[&#39;extension&#39;])){
case &#39;jpg&#39;:
$return = new Image_JPEG($file);
break;
case &#39;png&#39;:
$return = new Image_PNG($file);
break;
default:
echo &#39;图片类型不正确&#39;;
break;
}
if($return instanceof IImage){
return $return;
}else{
echo &#39;出错了&#39;;
exit();
}
 
}
 
}
 
$image = ImageFactory::factory(&#39;images/11.jpg&#39;);
var_dump($image->getWidth());
echo &#39;<br>&#39;;
print_r($image->getheight());
echo &#39;<br>&#39;;
print_r($image->getData());
登入後複製



PHP

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