某天又突然想到好久沒復習設計模式了。綜合一下,復習一個設計模式之后在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結構調用即可,簡單復習完成??吹筋悎D應該能想起簡單工廠了,因為他本身確實很簡單。復習完簡單工廠,思維稍微跳躍一下,直接來看看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'));
$db->setFetchMode(Zend_Db::FETCH_OBJ); $result = $db->fetchAssoc('SELECT bug_id, bug_description, bug_status FROM bugs');
<?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'));
注意,注意,里面還有個低調的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,這張簡單的類圖就應該是,
一個非常非常純凈的簡單工廠就這么出來了(不像簡單工廠類圖嗎?那只是因為類的位置沒放好)。
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答