首頁  >  文章  >  後端開發  >  PHP 中使用工廠模式

PHP 中使用工廠模式

高洛峰
高洛峰原創
2016-12-17 11:21:211106瀏覽

工廠類是指包含一個專門用來創建其他對象的方法的類,工廠類在多態性編程實踐中是至關重要的,它允許動態的替換類,修改配置,通常會使應用程序更加靈活,熟練工廠模式高級PHP開發人員是很重要的。

  工廠模式通常用來返回符合類似介面的不同的類,工廠的一種常見用法就是創建多態的提供者,從而允許我們基於應用程式邏輯或配置設定來決定應實例化哪一個類,例如,可以使用這樣的提供者來擴展一個類,而不需要重構應用程式的其他部分,從而使用新的擴充後的名稱。 

  通常,工廠模式有一個關鍵的構造,根據一般原則命名為Factory的靜態方法,然而這只是一種原則,工廠方法可以任意命名,這個靜態還可以接受任意資料 的參數,必須傳回一個物件。

基本的工廠類

class MyObject{  
  //对象将从工厂返回  
}  
  
class MyFactory{  
public static function factory(){  
return new MyObject():  
   }  
}  
  
$instance=MyFactory::factory();

使用工廠類解析圖像檔案

Php代碼 

<?php  
interface IImage{  
    function getHeight();  
    function getWidth();  
    function getData();  
}  
  
class Image_PNG implements IImage{  
      
    private $_width,$_height,$_data;  
      
    public function __construct($file){  
        $this->_file=$file;  
        $this->_parse();  
    }  
          
    private function _parse(){  
              
            //完成PNG格式的解析工作  
            //并填充$_width,$_height,$_data;  
        }  
          
          
    public function getWidth(){  
        return $this->_width;  
    }  
    public  function getHeight(){  
        return $this->_height;  
    }  
      
    public function getData(){  
       return $this->_data;  
      
    }  
      
}  
  
class Image_JPEG implements IImage{  
      
    private $_width,$_height,$_data;  
      
    public function __construct($file){  
        $this->_file=$file;  
        $this->_parse();  
    }  
          
    private function _parse(){  
              
            //完成JPEG格式的解析工作  
            //并填充$_width,$_height,$_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){  
        $pathParts=pathinfo($file);  
        switch (strtolower($pathParts[&#39;extension&#39;]))  
        {  
            case &#39;jpg&#39;:  
                $ret=new Image_JPEG($file);  
                break;  
            case &#39;png&#39;:  
                $ret=new Image_PNG($file);  
                break;  
                default:  
                    //有问题  
  
                      
        }  
        if($ret instanceof IImage){  
            return $ret;  
        }else {  
            //有问题  
        }  
    }  
}  
  
//当使用图像文件名调用 工厂方法时,根据传入的文件类型不同,取得不同对象。

Php代碼 

//调用ImageFactoyr  
$image=ImageFactory::factory(&#39;/path/to/my.jpg&#39;);  
//$image是Image_JPEG类的一个实例  
echo $image->getWidth();

使用工廠類別資料庫可移值性問題,在資料庫個方面起作用。

1.使軟體更容易支援各種不同的資料庫平台,用於擴展用戶群

2.如果軟體是內部使用,需要修改資料庫時,可以輕鬆將應用程式移值到另一個平台

 

在程式碼中,建立了一個名為User的資料庫表來測試它,這個表定義一個名為email的varchar類型欄位

 

Php程式碼 

<?php  
interface IDatabaseBindings{  
    public function userExists($email);  
}  
  
class PGSQL implements IDatabaseBindings{  
    protected $_connection;  
      
    public function __construct(){  
          
        $this->_connection=pg_connect(&#39;dbname=example_db&#39;);  
    }  
      
    public function userExists($email){  
          
        $emailEscaped=pg_escape_string($email);  
        $query="select 1 from users where email=&#39;".$emailEscaped."&#39;";  
        if($result=pg_query($query,$this->_connection)){  
            return (pg_num_rows($result)>0)?true:false;  
        }else{  
            return false;  
        }  
    }  
      
}  
  
class MYSQL implements IDatabaseBindings{  
      
    protected $_connection;  
      
    public function __construct(){  
          
        $this->_connection=mysql_connect(&#39;localhost&#39;);  
        mysql_select_db(&#39;example_db&#39;,$this->_connection);  
    }  
      
    public function userExists($email){  
          
        $emailEscaped=mysql_real_escape_string($email);  
        $query="select 1 from users where email=&#39;".$emailEscaped."&#39;";  
        if($result=mysql_query($query,$this->_connection)){  
            return (mysql_num_rows($result)>0)?true:false;  
        }else{  
            return false;  
        }  
    }  
      
}  
  
class DatabaseFactory{  
      
    public static function factory(){  
        $type=loadtypefromconfigfile();  
        switch ($type){  
            case &#39;PGSQL&#39;:  
                return new PGSQL();  
                break;  
            case &#39;MYSQL&#39;:  
                return new MYSQL();  
                break;  
        }  
    }  
}

應用程式不必知道它與何種類型的資料庫連接,只會基於IDatabaseBindings介面定義的規則直接與工廠傳回的實例打交道。

 

Php代碼 

//调用DatabaseFactoy  
$db=DatabaseFactory::factory();  
$db->userExists(&#39;person@example.com&#39;);



更多PHP 使用工廠模式相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn