国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

Zend Framework中的簡單工廠模式

2020-03-22 18:01:15
字體:
來源:轉載
供稿:網友
  • 某天又突然想到好久沒復習設計模式了。綜合一下,復習一個設計模式之后在ZF中找一下使用這模式的源碼吧,不讀所有源碼,讀讀比較”高級”的部分吧,要說模式,暫時不知道是不是所有模式ZF里面都有,但是應該有足夠的模式夠我最近看了,再說可以找找其他開源的軟件來研究下設計模式。
    前段時間用Zend Framework,把他當黑盒感覺不是很好,一直有看其源碼的沖動,但是。。。如果一點一點地看的話,挑戰確實有些大了。要說模式,暫時不知道是不是所有模式ZF里面都有,但是應該有足夠的模式夠我最近看了,在說可以找找其他開源的軟件來找模式。這段時間被各種筆試神馬亂七八糟的把生活搞得稍微有點亂,但是不管怎樣,復習還是必須的吧。再說一下ZF吧,ZF一個好處就是各個component比較獨立,component之間沒有過多的依賴,這樣一來,為使用者提供了方便,當然也為我這樣無聊且懶的想看源碼的人提供了方便。

    今天看看簡單工廠,ZF里面不缺模式,更不缺工廠模式,大名鼎鼎的的 Zend_Db就毫不吝嗇的使用簡單工廠,再ctrl+h(zend studio下)一下會發現factory特別多,如果沒猜錯應該大多應該也是簡單工廠。由于Zend_Db 最常用,我也就自然的會比較想看一下他的實現。在查看源碼之前先復習一下怎么用Zend_Db和簡單工廠(這里是一個stack,先復習簡單工廠)。

    復習簡單工廠模式
    用類圖回憶一下,簡單工廠類圖:


    借用《研磨設計模式》作者的一張圖,可以看到Client通過factory來獲取對象,通過Api結構來調用。用factory把具體的Api的創建隱藏起來。而其他所有使用者在使用時,只需要知道用factory創建,通過Api結構調用即可,簡單復習完成。看到類圖應該能想起簡單工廠了,因為他本身確實很簡單。復習完簡單工廠,思維稍微跳躍一下,直接來看看Zend_Db的使用。
    1.復習Zend_Db的使用
    如果不知道如何使用,準備看XXX的源碼卻不知道怎么用XXX,這有點囧,所以先小小的看一下Zend_Db的使用,下面這段是在ZF官方文檔里面的(個人不是很喜歡ZF文檔,沒Yii易讀)
    /html' target='_blank'>public/index.php
    代碼如下:

    $db = Zend_Db::factory('Pdo_Mysql', array('host' => '127.0.0.1','username' => 'webuser','password' => 'xxxxxxxx','dbname' => 'test'));

    這里是把數據庫配置也放到代碼里面,看起來最簡單(實際上其他也不難,只是數據庫放置的位置不同,便于管理罷了),但這樣在正常情況下不是最好的方式,但是為了突出重點,這里選用這中最簡單的方式。注意里面的Zend_Db::factory(‘Pdo_Mysql'…這段
    上面生成了一個$db(一個Zend_Db對象),使用上面的$db進行查詢如下www.it165.net:
    $db->setFetchMode(Zend_Db::FETCH_OBJ); $result = $db->fetchAssoc('SELECT bug_id, bug_description, bug_status FROM bugs');

    繼續來自網址文檔,這是取記錄的模式為Object,再fetch,一切目前看起來都自然而然,但是至今還是把它Zend_Db當作一個黑盒使用。下面可以進入正題。
    首先,查看一下zend/Db.php的代碼摘要:
    <?phpclass Zend_Db{/**設定一些常量和默認值*//*** Factory for Zend_Db_Adapter_Abstract classes.** First argument may be a string containing the base of the adapter class* name, e.g. 'Mysqli' corresponds to class Zend_Db_Adapter_Mysqli. This* name is currently case-insensitive, but is not ideal to rely on this behavior.* If your class is named 'My_Company_Pdo_Mysql', where 'My_Company' is the namespace* and 'Pdo_Mysql' is the adapter name, it is best to use the name exactly as it* is defined in the class. This will ensure proper use of the factory API.** First argument may alternatively be an object of type Zend_Config.* The adapter class base name is read from the 'adapter' property.* The adapter config parameters are read from the 'params' property.** Second argument is optional and may be an associative array of key-value* pairs. This is used as the argument to the adapter constructor.** If the first argument is of type Zend_Config, it is assumed to contain* all parameters, and the second argument is ignored.** @param mixed $adapter String name of base adapter class, or Zend_Config object.* @param mixed $config OPTIONAL; an array or Zend_Config object with adapter parameters.* @return Zend_Db_Adapter_Abstract* @throws Zend_Db_Exception*/public static function factory ($adapter, $config = array()){//使用Zend_Config對象,上述方式沒有使用,直接使用Arrayif ($config instanceof Zend_Config) {$config = $config->toArray();}/** Convert Zend_Config argument to plain string* adapter name and separate config object.*/if ($adapter instanceof Zend_Config) {if (isset($adapter->params)) {$config = $adapter->params->toArray();}if (isset($adapter->adapter)) {$adapter = (string) $adapter->adapter;} else {$adapter = null;}}/** Verify that adapter parameters are in an array.*/if (! is_array($config)) {/*** @see Zend_Db_Exception*/require_once 'Zend/Db/Exception.php';throw new Zend_Db_Exception('Adapter parameters must be in an array or a Zend_Config object');}/** Verify that an adapter name has been specified.*/if (! is_string($adapter) || empty($adapter)) {/*** @see Zend_Db_Exception*/require_once 'Zend/Db/Exception.php';throw new Zend_Db_Exception('Adapter name must be specified in a string');}/** Form full adapter class name*/$adapterNamespace = 'Zend_Db_Adapter';if (isset($config['adapterNamespace'])) {if ($config['adapterNamespace'] != '') {$adapterNamespace = $config['adapterNamespace'];}unset($config['adapterNamespace']);}// Adapter no longer normalized- see http://framework.zend.com/issues/browse/ZF-5606$adapterName = $adapterNamespace . '_';$adapterName .= str_replace(' ', '_',ucwords(str_replace('_', ' ', strtolower($adapter))));/** Load the adapter class. This throws an exception* if the specified class cannot be loaded.*/if (! class_exists($adapterName)) {require_once 'Zend/Loader.php';Zend_Loader::loadClass($adapterName);}/** Create an instance of the adapter class.* Pass the config to the adapter class constructor.*/$dbAdapter = new $adapterName($config);/** Verify that the object created is a descendent of the abstract adapter type.*/if (! $dbAdapter instanceof Zend_Db_Adapter_Abstract) {/*** @see Zend_Db_Exception*/require_once 'Zend/Db/Exception.php';throw new Zend_Db_Exception("Adapter class '$adapterName' does not extend Zend_Db_Adapter_Abstract");}return $dbAdapter;}}?>

    最上方的注釋非常值得看,它清楚的說明了這個工廠,另外一段比較重要的幾段代碼(忽略其中的異常處理)是:

    //factory有一個參數叫做$adapterpublic static function factory($adapter, $config = array())//確定namespace$adapterNamespace = 'Zend_Db_Adapter';//用namespace和上面傳入的$adapter構造類名$adapterName = $adapterNamespace . '_';$adapterName .= str_replace(' ', '_', ucwords(str_replace('_', ' ', strtolower($adapter))));//用上面生成的類名new出obj,看起來PHP比java方便那么一點點哈(Class.forName(‘XXX').newInstance())$dbAdapter = new $adapterName($config);

    在回想上面使用Zend_Db::factory生成$db的地方:

    $db = Zend_Db::factory('Pdo_Mysql', array('host' => '127.0.0.1','username' => 'webuser','password' => 'xxxxxxxx','dbname' => 'test'));

    factory方法的第一個參數即是$adapter為Pdo_Mysql,記住這里是Pdo_Mysql,再跳躍一下,根據上面的$adapterNamespace = ‘Zend_Db_Adapter';可以看到生成的找到$dbAdapter的值最終必為:Zend_Db_Adapter_Pdo_Mysql,ok,根據此名字找到zend/db/adapter/pdo目錄下,哈,這么多熟悉的面孔,看到了熟悉的MySql、Mssql、Sqlite這些老面孔了。


    注意,注意,里面還有個低調的Abstract.php,里面他們的父類Zend_Db_Adapter_Pdo_Abstract。打開Mysql.php可以看到
    class Zend_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Abstract

    嗯,類名Zend_Db_Adapter_Pdo_Mysql和上面生成的名字一樣滴,在看看其他幾個文件里面的類,他們都繼承自Zend_Db_Adapter_Pdo_Abstract,如果要畫類圖,那就應該會有如下這么一張類圖:


    接著再加入調用著Client和工廠函數所在的位置Zend_Db,這張簡單的類圖就應該是,


    一個非常非常純凈的簡單工廠就這么出來了(不像簡單工廠類圖嗎?那只是因為類的位置沒放好)。

    PHP編程

    鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 黔南| 鸡东县| 泌阳县| 福鼎市| 乡宁县| 肃宁县| 雷州市| 保定市| 会理县| 棋牌| 桑植县| 宁强县| 冀州市| 盐城市| 永新县| 互助| 景东| 华阴市| 揭东县| 黄浦区| 股票| 分宜县| 工布江达县| 平原县| 南皮县| 项城市| 海晏县| 青岛市| 襄城县| 尼勒克县| 绵阳市| 冕宁县| 新丰县| 闸北区| 德阳市| 秦安县| 交口县| 文登市| 甘德县| 旬阳县| 沁源县|