SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Downloaden Sie, um offline zu lesen
Patterns in Zend Framework

                      Jace Ju
Patterns
   Simple Factory
   Singleton
   Adapter
   Strategy
   Composite
   Chain of Responsibility
Simple Factory
   概念
       不讓程序依賴類別名稱。
       利用設定來動態建立物件。
範例: Zend_Db::factory()
   factory() 提供參數以建立不同的 Db Adapter 。
   應用程式可以透過設定來配置 factory 參數。
$db = Zend_Db::factory('pdo_mysql', array(
    'host'             => '127.0.0.1',
    'username'         => 'www',
    'password'         => '123456',
    'dbname'           => 'test',
));

echo get_class($db), "n"; // Zend_Db_Adapter_Pdo_Mysql

$db = Zend_Db::factory('mysqli', array(
    'host'             => '127.0.0.1',
    'username'         => 'www',
    'password'         => '123456',
    'dbname'           => 'test',
));

echo get_class($db), "n"; // Zend_Db_Adapter_Mysqli
Zend_Db::factory()
Singleton
   概念
       整個系統只需要單一個物件實體。
       通常用以取代全域變數。
範例: Zend_Controller_Front
   整個應用程式只需要一個 Front Controller 。
   Application Resource 及 Controller Plugin 可以
    透過 getInstance 方法來取得 Front Controller 的
    唯一實體。
$frontController = Zend_Controller_Front::getInstance();
Adapter
   概念
       將已實作功能但介面不同的類別庫或函式庫整合進來。
       通常用於整合第三方套件。
範例: Zend_Db_Adapter
   轉接微軟寫好的 Sqlsrv 函式庫。
class Zend_Db_Adapter_Sqlsrv extends Zend_Db_Adapter_Abstract
{
    protected function _connect()
    {
        // ...
        $this->_connection = sqlsrv_connect(...);
    }

    public function closeConnection()
    {
        // ...
        sqlsrv_close($this->_connection);
        // ...
    }
}

$dbAdapter = new Zend_Db_Adapter_Sqlsrv(...);
print_r($dbAdapter->listTables());
$dbAdapter->closeConnection();
Zend_Db_Adapter
Strategy
   概念
       採用不同的策略來處理相同的問題。
       依照情境的不同來選擇策略。
範例: Zend_Cache
   Zend_Cache 提供不同的 Backend 儲存方式。
$cache = new Zend_Cache_Core();
$cache->setBackend(new Zend_Cache_Backend_File(array(
     'cache_dir' => dirname(__FILE__),
)));

$data = $cache->load('test');
if (!$data) {
    $data = 'Cached data';
    $cache->save($data, 'test');
}
print_r($data);
Zend_Cache
Composite
   概念
       對群體與個體一視同仁。
       通常用於樹狀結構。
範例: Zend_Filter
   Zend_Filter 可以加入其他 Filter 。
   Zend_Filter 為 Composite ,其他實作
    Zend_Filter_Interface 的子類別為 Leaf 。
$unfiltered = 'ab#12.3$%cde';
$filter = new Zend_Filter();
$value = $filter->filter($unfiltered);
echo $value, "n"; // ab#12.3$%cde

$filter->addFilter(new Zend_Filter_Alnum());
$value = $filter->filter($unfiltered);
echo $value, "n"; // ab123cde

$filter->addFilter(new Zend_Filter_Digits());
$value = $filter->filter($unfiltered);
echo $value, "n"; // 123
Zend_Filter
範例: Zend_Config
   Composite 的變形,Zend_Config 本身就是 Leaf
    及 Composite。
$config = new Zend_Config_Ini(dirname(__FILE__) . '/config.ini');

print_r($config->toArray()); // Composite

foreach ($config as $name => $section) {
    echo $name, " ", get_class($section), "n";
    print_r($section->toArray()); // Composite
}

$dbConfig = $config->production->db;
echo get_class($dbConfig), "n";
print_r($dbConfig->toArray()); // Leaf
Chain of Responsibility
   概念
       定義一連串的處理機制來處理某個需求。
       在找到符合的規則後就離開。
       可以用來取代 if ... elseif 。
範例: Zend_Controller_Router
   只處理符合的 Url 。
$router = new Zend_Controller_Router_Rewrite();
$requests = array(
    new Zend_Controller_Request_Http('http://localhost/login'),
    new Zend_Controller_Request_Http('http://localhost/article/123'),
);

$loginRoute = new Zend_Controller_Router_Route_Static('login', array(
    'controller' => 'user',
    'action' => 'login',
));
$articleRoute = new Zend_Controller_Router_Route_Regex('article/(d+)', array(
    'controller' => 'blog',
    'action' => 'article',
), array(
    1 => 'id',
));

$router->addRoute('login', $loginRoute);
$router->addRoute('article', $articleRoute);

foreach ($requests as $request) {
    $router->route($request);
    print_r($request->getParams());
}
Zend_Controller_Router
其他
   Patterns 和物件導向設計原則讓 Zend
    Framewrork 具有強大的擴充性。
   Zend Framework 還實作了許多企業級的
    Patterns ,例如: Table Data Gateway 、 Front
    Controller 、 Registry ...
總結
   在設計類別時,不一定要先考慮 Patterns 。
   不必拘泥於 Patterns 的形。
   一個類別體系不一定只有一種 Pattern 。
   Patterns 之間可以互相合作。
謝謝

Weitere ähnliche Inhalte

Was ist angesagt?

基于原型的JavaScript面向对象编程
基于原型的JavaScript面向对象编程基于原型的JavaScript面向对象编程
基于原型的JavaScript面向对象编程zhangdaiping
 
[DCTPE2010] Drupal 模組開發入門
[DCTPE2010] Drupal 模組開發入門[DCTPE2010] Drupal 模組開發入門
[DCTPE2010] Drupal 模組開發入門Drupal Taiwan
 
使用 Eloquent ORM
使用 Eloquent ORM使用 Eloquent ORM
使用 Eloquent ORMShengyou Fan
 
LazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHPLazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHPLin Yo-An
 
網頁設計 - 資料庫存取
網頁設計 - 資料庫存取網頁設計 - 資料庫存取
網頁設計 - 資料庫存取Vincent Chi
 
用Jquery实现拖拽层
用Jquery实现拖拽层用Jquery实现拖拽层
用Jquery实现拖拽层yiditushe
 
深入了解Memcache
深入了解Memcache深入了解Memcache
深入了解Memcachezubin Jiang
 
OpenWebSchool - 02 - PHP Part I
OpenWebSchool - 02 - PHP Part IOpenWebSchool - 02 - PHP Part I
OpenWebSchool - 02 - PHP Part IHung-yu Lin
 
PHPUnit + Xdebug 单元测试技术
PHPUnit + Xdebug 单元测试技术PHPUnit + Xdebug 单元测试技术
PHPUnit + Xdebug 单元测试技术hoopchina
 
第十期 阿甘Javascript开发思想(入门篇)
第十期 阿甘Javascript开发思想(入门篇)第十期 阿甘Javascript开发思想(入门篇)
第十期 阿甘Javascript开发思想(入门篇)9scss
 
Migrations 與 Schema操作
Migrations 與 Schema操作Migrations 與 Schema操作
Migrations 與 Schema操作Shengyou Fan
 
Drupal 版型設計 - 瞭解版型程式
Drupal 版型設計 - 瞭解版型程式Drupal 版型設計 - 瞭解版型程式
Drupal 版型設計 - 瞭解版型程式Chris Wu
 
旺铺前端设计和实现
旺铺前端设计和实现旺铺前端设计和实现
旺铺前端设计和实现hua qiu
 
Sina App Quick Guide 1
Sina App Quick Guide 1Sina App Quick Guide 1
Sina App Quick Guide 1guestf4aed35
 
Web Caching Architecture and Design
Web Caching Architecture and DesignWeb Caching Architecture and Design
Web Caching Architecture and DesignHo Kim
 
Mongodb
MongodbMongodb
Mongodbbj
 
深入淺出 Web 容器 - Tomcat 原始碼分析
深入淺出 Web 容器  - Tomcat 原始碼分析深入淺出 Web 容器  - Tomcat 原始碼分析
深入淺出 Web 容器 - Tomcat 原始碼分析Justin Lin
 
Mybatis学习培训
Mybatis学习培训Mybatis学习培训
Mybatis学习培训flynofry
 
Bash编程之变量高级篇
Bash编程之变量高级篇Bash编程之变量高级篇
Bash编程之变量高级篇Zhiyao Pan
 

Was ist angesagt? (20)

Node way
Node wayNode way
Node way
 
基于原型的JavaScript面向对象编程
基于原型的JavaScript面向对象编程基于原型的JavaScript面向对象编程
基于原型的JavaScript面向对象编程
 
[DCTPE2010] Drupal 模組開發入門
[DCTPE2010] Drupal 模組開發入門[DCTPE2010] Drupal 模組開發入門
[DCTPE2010] Drupal 模組開發入門
 
使用 Eloquent ORM
使用 Eloquent ORM使用 Eloquent ORM
使用 Eloquent ORM
 
LazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHPLazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHP
 
網頁設計 - 資料庫存取
網頁設計 - 資料庫存取網頁設計 - 資料庫存取
網頁設計 - 資料庫存取
 
用Jquery实现拖拽层
用Jquery实现拖拽层用Jquery实现拖拽层
用Jquery实现拖拽层
 
深入了解Memcache
深入了解Memcache深入了解Memcache
深入了解Memcache
 
OpenWebSchool - 02 - PHP Part I
OpenWebSchool - 02 - PHP Part IOpenWebSchool - 02 - PHP Part I
OpenWebSchool - 02 - PHP Part I
 
PHPUnit + Xdebug 单元测试技术
PHPUnit + Xdebug 单元测试技术PHPUnit + Xdebug 单元测试技术
PHPUnit + Xdebug 单元测试技术
 
第十期 阿甘Javascript开发思想(入门篇)
第十期 阿甘Javascript开发思想(入门篇)第十期 阿甘Javascript开发思想(入门篇)
第十期 阿甘Javascript开发思想(入门篇)
 
Migrations 與 Schema操作
Migrations 與 Schema操作Migrations 與 Schema操作
Migrations 與 Schema操作
 
Drupal 版型設計 - 瞭解版型程式
Drupal 版型設計 - 瞭解版型程式Drupal 版型設計 - 瞭解版型程式
Drupal 版型設計 - 瞭解版型程式
 
旺铺前端设计和实现
旺铺前端设计和实现旺铺前端设计和实现
旺铺前端设计和实现
 
Sina App Quick Guide 1
Sina App Quick Guide 1Sina App Quick Guide 1
Sina App Quick Guide 1
 
Web Caching Architecture and Design
Web Caching Architecture and DesignWeb Caching Architecture and Design
Web Caching Architecture and Design
 
Mongodb
MongodbMongodb
Mongodb
 
深入淺出 Web 容器 - Tomcat 原始碼分析
深入淺出 Web 容器  - Tomcat 原始碼分析深入淺出 Web 容器  - Tomcat 原始碼分析
深入淺出 Web 容器 - Tomcat 原始碼分析
 
Mybatis学习培训
Mybatis学习培训Mybatis学习培训
Mybatis学习培训
 
Bash编程之变量高级篇
Bash编程之变量高级篇Bash编程之变量高级篇
Bash编程之变量高级篇
 

Ähnlich wie Patterns in Zend Framework

Backbone js and requirejs
Backbone js and requirejsBackbone js and requirejs
Backbone js and requirejsChi-wen Sun
 
Free Marker中文文档
Free Marker中文文档Free Marker中文文档
Free Marker中文文档yiditushe
 
Introduction to CodeIgniter
Introduction to CodeIgniterIntroduction to CodeIgniter
Introduction to CodeIgniterChun-Kai Wang
 
PHP & MySQL 教學
PHP & MySQL 教學PHP & MySQL 教學
PHP & MySQL 教學Bo-Yi Wu
 
Model 設定與 Seeding
Model 設定與 SeedingModel 設定與 Seeding
Model 設定與 SeedingShengyou Fan
 
Coding guideline
Coding guidelineCoding guideline
Coding guideline斯理 衛
 
【 I Love Joomla 】- Joomla!佈景製作教學
【 I Love Joomla 】- Joomla!佈景製作教學【 I Love Joomla 】- Joomla!佈景製作教學
【 I Love Joomla 】- Joomla!佈景製作教學ilovejoomla
 
Moodle 项目帮助手册:程序编写准则
Moodle 项目帮助手册:程序编写准则Moodle 项目帮助手册:程序编写准则
Moodle 项目帮助手册:程序编写准则YUCHENG HU
 
Keep your code clean
Keep your code cleanKeep your code clean
Keep your code cleanmacrochen
 
jQuery底层架构
jQuery底层架构jQuery底层架构
jQuery底层架构fangdeng
 
Java华为面试题
Java华为面试题Java华为面试题
Java华为面试题yiditushe
 
Introduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xIntroduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xBo-Yi Wu
 
React vs Flux
React vs FluxReact vs Flux
React vs FluxLC2009
 
I Love Joomla! 佈景製作教學 0212
I Love Joomla! 佈景製作教學 0212I Love Joomla! 佈景製作教學 0212
I Love Joomla! 佈景製作教學 0212Asika Simon
 
PHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming SkillsPHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming SkillsHo Kim
 
lwdba – 開放原始碼的輕量級資料庫存取程式庫
lwdba – 開放原始碼的輕量級資料庫存取程式庫lwdba – 開放原始碼的輕量級資料庫存取程式庫
lwdba – 開放原始碼的輕量級資料庫存取程式庫建興 王
 
Node.js开发体验
Node.js开发体验Node.js开发体验
Node.js开发体验QLeelulu
 

Ähnlich wie Patterns in Zend Framework (20)

CRUD 綜合運用
CRUD 綜合運用CRUD 綜合運用
CRUD 綜合運用
 
Backbone js and requirejs
Backbone js and requirejsBackbone js and requirejs
Backbone js and requirejs
 
Free Marker中文文档
Free Marker中文文档Free Marker中文文档
Free Marker中文文档
 
Introduction to CodeIgniter
Introduction to CodeIgniterIntroduction to CodeIgniter
Introduction to CodeIgniter
 
PHP & MySQL 教學
PHP & MySQL 教學PHP & MySQL 教學
PHP & MySQL 教學
 
CRUD 綜合運用
CRUD 綜合運用CRUD 綜合運用
CRUD 綜合運用
 
Model 設定與 Seeding
Model 設定與 SeedingModel 設定與 Seeding
Model 設定與 Seeding
 
Coding guideline
Coding guidelineCoding guideline
Coding guideline
 
【 I Love Joomla 】- Joomla!佈景製作教學
【 I Love Joomla 】- Joomla!佈景製作教學【 I Love Joomla 】- Joomla!佈景製作教學
【 I Love Joomla 】- Joomla!佈景製作教學
 
Moodle 项目帮助手册:程序编写准则
Moodle 项目帮助手册:程序编写准则Moodle 项目帮助手册:程序编写准则
Moodle 项目帮助手册:程序编写准则
 
Keep your code clean
Keep your code cleanKeep your code clean
Keep your code clean
 
jQuery底层架构
jQuery底层架构jQuery底层架构
jQuery底层架构
 
Java华为面试题
Java华为面试题Java华为面试题
Java华为面试题
 
Introduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xIntroduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.x
 
React vs Flux
React vs FluxReact vs Flux
React vs Flux
 
I Love Joomla! 佈景製作教學 0212
I Love Joomla! 佈景製作教學 0212I Love Joomla! 佈景製作教學 0212
I Love Joomla! 佈景製作教學 0212
 
PHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming SkillsPHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming Skills
 
nodejs开发web站点
nodejs开发web站点nodejs开发web站点
nodejs开发web站点
 
lwdba – 開放原始碼的輕量級資料庫存取程式庫
lwdba – 開放原始碼的輕量級資料庫存取程式庫lwdba – 開放原始碼的輕量級資料庫存取程式庫
lwdba – 開放原始碼的輕量級資料庫存取程式庫
 
Node.js开发体验
Node.js开发体验Node.js开发体验
Node.js开发体验
 

Mehr von Jace Ju

What happens in laravel 4 bootstraping
What happens in laravel 4 bootstrapingWhat happens in laravel 4 bootstraping
What happens in laravel 4 bootstrapingJace Ju
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in actionJace Ju
 
Beginning PHPUnit
Beginning PHPUnitBeginning PHPUnit
Beginning PHPUnitJace Ju
 
深入淺出 MVC
深入淺出 MVC深入淺出 MVC
深入淺出 MVCJace Ju
 
如何打造 WebMVC Framework - 基礎概念篇
如何打造 WebMVC Framework - 基礎概念篇如何打造 WebMVC Framework - 基礎概念篇
如何打造 WebMVC Framework - 基礎概念篇Jace Ju
 
Refactoring with Patterns in PHP
Refactoring with Patterns in PHPRefactoring with Patterns in PHP
Refactoring with Patterns in PHPJace Ju
 
Patterns in Library Design (套件設計裡的模式)
Patterns in Library Design (套件設計裡的模式)Patterns in Library Design (套件設計裡的模式)
Patterns in Library Design (套件設計裡的模式)Jace Ju
 
購物車程式架構簡介
購物車程式架構簡介購物車程式架構簡介
購物車程式架構簡介Jace Ju
 
Head First Zend Framework - Part 1 Project & Application
Head First Zend Framework - Part 1 Project & ApplicationHead First Zend Framework - Part 1 Project & Application
Head First Zend Framework - Part 1 Project & ApplicationJace Ju
 
Web Refactoring
Web RefactoringWeb Refactoring
Web RefactoringJace Ju
 
jQuery 實戰經驗講座
jQuery 實戰經驗講座jQuery 實戰經驗講座
jQuery 實戰經驗講座Jace Ju
 
CSS 排版 - 基礎觀念篇
CSS 排版 - 基礎觀念篇CSS 排版 - 基礎觀念篇
CSS 排版 - 基礎觀念篇Jace Ju
 
PHP 防駭 - 基礎觀念篇
PHP 防駭 - 基礎觀念篇PHP 防駭 - 基礎觀念篇
PHP 防駭 - 基礎觀念篇Jace Ju
 
PHP 物件導向 - 基礎觀念篇
PHP 物件導向 - 基礎觀念篇PHP 物件導向 - 基礎觀念篇
PHP 物件導向 - 基礎觀念篇Jace Ju
 

Mehr von Jace Ju (14)

What happens in laravel 4 bootstraping
What happens in laravel 4 bootstrapingWhat happens in laravel 4 bootstraping
What happens in laravel 4 bootstraping
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in action
 
Beginning PHPUnit
Beginning PHPUnitBeginning PHPUnit
Beginning PHPUnit
 
深入淺出 MVC
深入淺出 MVC深入淺出 MVC
深入淺出 MVC
 
如何打造 WebMVC Framework - 基礎概念篇
如何打造 WebMVC Framework - 基礎概念篇如何打造 WebMVC Framework - 基礎概念篇
如何打造 WebMVC Framework - 基礎概念篇
 
Refactoring with Patterns in PHP
Refactoring with Patterns in PHPRefactoring with Patterns in PHP
Refactoring with Patterns in PHP
 
Patterns in Library Design (套件設計裡的模式)
Patterns in Library Design (套件設計裡的模式)Patterns in Library Design (套件設計裡的模式)
Patterns in Library Design (套件設計裡的模式)
 
購物車程式架構簡介
購物車程式架構簡介購物車程式架構簡介
購物車程式架構簡介
 
Head First Zend Framework - Part 1 Project & Application
Head First Zend Framework - Part 1 Project & ApplicationHead First Zend Framework - Part 1 Project & Application
Head First Zend Framework - Part 1 Project & Application
 
Web Refactoring
Web RefactoringWeb Refactoring
Web Refactoring
 
jQuery 實戰經驗講座
jQuery 實戰經驗講座jQuery 實戰經驗講座
jQuery 實戰經驗講座
 
CSS 排版 - 基礎觀念篇
CSS 排版 - 基礎觀念篇CSS 排版 - 基礎觀念篇
CSS 排版 - 基礎觀念篇
 
PHP 防駭 - 基礎觀念篇
PHP 防駭 - 基礎觀念篇PHP 防駭 - 基礎觀念篇
PHP 防駭 - 基礎觀念篇
 
PHP 物件導向 - 基礎觀念篇
PHP 物件導向 - 基礎觀念篇PHP 物件導向 - 基礎觀念篇
PHP 物件導向 - 基礎觀念篇
 

Patterns in Zend Framework

  • 1. Patterns in Zend Framework Jace Ju
  • 2. Patterns  Simple Factory  Singleton  Adapter  Strategy  Composite  Chain of Responsibility
  • 3. Simple Factory  概念  不讓程序依賴類別名稱。  利用設定來動態建立物件。
  • 4. 範例: Zend_Db::factory()  factory() 提供參數以建立不同的 Db Adapter 。  應用程式可以透過設定來配置 factory 參數。 $db = Zend_Db::factory('pdo_mysql', array( 'host' => '127.0.0.1', 'username' => 'www', 'password' => '123456', 'dbname' => 'test', )); echo get_class($db), "n"; // Zend_Db_Adapter_Pdo_Mysql $db = Zend_Db::factory('mysqli', array( 'host' => '127.0.0.1', 'username' => 'www', 'password' => '123456', 'dbname' => 'test', )); echo get_class($db), "n"; // Zend_Db_Adapter_Mysqli
  • 6. Singleton  概念  整個系統只需要單一個物件實體。  通常用以取代全域變數。
  • 7. 範例: Zend_Controller_Front  整個應用程式只需要一個 Front Controller 。  Application Resource 及 Controller Plugin 可以 透過 getInstance 方法來取得 Front Controller 的 唯一實體。 $frontController = Zend_Controller_Front::getInstance();
  • 8. Adapter  概念  將已實作功能但介面不同的類別庫或函式庫整合進來。  通常用於整合第三方套件。
  • 9. 範例: Zend_Db_Adapter  轉接微軟寫好的 Sqlsrv 函式庫。 class Zend_Db_Adapter_Sqlsrv extends Zend_Db_Adapter_Abstract { protected function _connect() { // ... $this->_connection = sqlsrv_connect(...); } public function closeConnection() { // ... sqlsrv_close($this->_connection); // ... } } $dbAdapter = new Zend_Db_Adapter_Sqlsrv(...); print_r($dbAdapter->listTables()); $dbAdapter->closeConnection();
  • 11. Strategy  概念  採用不同的策略來處理相同的問題。  依照情境的不同來選擇策略。
  • 12. 範例: Zend_Cache  Zend_Cache 提供不同的 Backend 儲存方式。 $cache = new Zend_Cache_Core(); $cache->setBackend(new Zend_Cache_Backend_File(array( 'cache_dir' => dirname(__FILE__), ))); $data = $cache->load('test'); if (!$data) { $data = 'Cached data'; $cache->save($data, 'test'); } print_r($data);
  • 14. Composite  概念  對群體與個體一視同仁。  通常用於樹狀結構。
  • 15. 範例: Zend_Filter  Zend_Filter 可以加入其他 Filter 。  Zend_Filter 為 Composite ,其他實作 Zend_Filter_Interface 的子類別為 Leaf 。 $unfiltered = 'ab#12.3$%cde'; $filter = new Zend_Filter(); $value = $filter->filter($unfiltered); echo $value, "n"; // ab#12.3$%cde $filter->addFilter(new Zend_Filter_Alnum()); $value = $filter->filter($unfiltered); echo $value, "n"; // ab123cde $filter->addFilter(new Zend_Filter_Digits()); $value = $filter->filter($unfiltered); echo $value, "n"; // 123
  • 17. 範例: Zend_Config  Composite 的變形,Zend_Config 本身就是 Leaf 及 Composite。 $config = new Zend_Config_Ini(dirname(__FILE__) . '/config.ini'); print_r($config->toArray()); // Composite foreach ($config as $name => $section) { echo $name, " ", get_class($section), "n"; print_r($section->toArray()); // Composite } $dbConfig = $config->production->db; echo get_class($dbConfig), "n"; print_r($dbConfig->toArray()); // Leaf
  • 18. Chain of Responsibility  概念  定義一連串的處理機制來處理某個需求。  在找到符合的規則後就離開。  可以用來取代 if ... elseif 。
  • 19. 範例: Zend_Controller_Router  只處理符合的 Url 。 $router = new Zend_Controller_Router_Rewrite(); $requests = array( new Zend_Controller_Request_Http('http://localhost/login'), new Zend_Controller_Request_Http('http://localhost/article/123'), ); $loginRoute = new Zend_Controller_Router_Route_Static('login', array( 'controller' => 'user', 'action' => 'login', )); $articleRoute = new Zend_Controller_Router_Route_Regex('article/(d+)', array( 'controller' => 'blog', 'action' => 'article', ), array( 1 => 'id', )); $router->addRoute('login', $loginRoute); $router->addRoute('article', $articleRoute); foreach ($requests as $request) { $router->route($request); print_r($request->getParams()); }
  • 21. 其他  Patterns 和物件導向設計原則讓 Zend Framewrork 具有強大的擴充性。  Zend Framework 還實作了許多企業級的 Patterns ,例如: Table Data Gateway 、 Front Controller 、 Registry ...
  • 22. 總結  在設計類別時,不一定要先考慮 Patterns 。  不必拘泥於 Patterns 的形。  一個類別體系不一定只有一種 Pattern 。  Patterns 之間可以互相合作。