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

首頁 > 開發 > PHP > 正文

php示例詳解Constructor Prototype Pattern 原型模式

2024-05-04 23:39:36
字體:
來源:轉載
供稿:網友

原型模式是一種創建者模式,其特點在于通過“復制”一個已經存在的實例來返回新的實例,而不是新建實例。

原型模式中主要角色

抽象原型(Prototype)角色:聲明一個克隆自己的接口

具體原型(Concrete Prototype)角色:實現一個克隆自己的操作

當一個類大部分都是相同的只有部分是不同的時候,如果需要大量這個類的對象,每次都重復實例化那些相同的部分是開銷很大的,而如果clone之前建立對象的那些相同的部分,就可以節約開銷。

針對php的一種實現方式就是__construct()和initialize函數分開分別處理這個類的初始化,construct里面放prototype也就是公共的部分,initialize里面是每個對象特殊的部分。這樣我們先建立一個類不initialize,以后每次clone這個類再進行initialize就可以了。

在zend framework官方手冊里面提到了這個http://framework.zend.com/manual/2.0/en/user-guide/database-and-models.html,但是沒有細講,下面我來分析一下

一、引入

在zf2的model里面有一個albumTable類,相當于一個操作數據庫動作的助手類,里面用到了tablegateway。

為了每次初始化albumtable都是相同的一個類,將初始化工作放到了根目錄的module.php文件的getServiceConfig(),其中用到工廠模式,并且通過回調函數,當每次ServiceManager($sm)需要實例化一個對象的時候會自動調用創建一個alumTable。下面代碼我們可以看出,創建一個albumTable還需要用相同的方式創建一個AlbumTableGateWay,這個類就用到了我們所要講的原型模式。

二、代碼詳解

 

 
  1. public function getServiceConfig() 
  2. return array( 
  3. 'factories' => array( 
  4. 'Album/Model/AlbumTable' => function($sm) { 
  5. $tableGateway = $sm->get('AlbumTableGateway'); 
  6. $table = new AlbumTable($tableGateway); 
  7. return $table; 
  8. }, 
  9. 'AlbumTableGateway' => function ($sm) { 
  10. $dbAdapter = $sm->get('Zend/Db/Adapter/Adapter'); 
  11. $resultSetPrototype = new ResultSet(); 
  12. $resultSetPrototype->setArrayObjectPrototype(new Album());//這個就是一個不變的原型 
  13. return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);//傳入到TableGateWay的構造函數中去 
  14. }, 
  15. ), 
  16. ); 

注意并不是TableGateWay運用了原型模式而是ResultSet這個類運用了。每當tablegateway調用select()或者insert()等方法的時候都會建立一個ResultSet用來表示結果,這些ResultSet中公共部分被clone,而獨特的部分類如data就會被initialize。

三、更多代碼示例

為了更清晰得了解這個原型,我們先拋開zend這個大框架,看一個完整的代碼示例。示例來自

PHPConstructor Best Practices And The Prototype Pattern

這篇文章關于prototype pattern的部分前半部分其實是混雜怎樣在構造函數中運用繼承來提高擴展性,兩個模式看起來可能不太好理解,我們直接看最后的代碼關于prototype pattern的部分。

 

 
  1. <?php 
  2. //框架中很常見的adapter類,用來適配各種數據庫,封裝一些基本數據庫連接操作。 
  3. //相當于上面代碼中的adapter類 
  4. class DbAdapter { 
  5. public function fetchAllFromTable($table) { 
  6. return $arrayOfData; 
  7. //運用prototype pattern的類,注意construct和initialize是分開的 
  8. //相當于上面zend 代碼里面的ResultSet類 
  9. class RowGateway { 
  10. public function __construct(DbAdapter $dbAdapter, $tableName) { 
  11. $this->dbAdapter = $dbAdapter; 
  12. $this->tableName = $tableName; 
  13. public function initialize($data) { 
  14. $this->data = $data; 
  15. /** 
  16. * Both methods require access to the database adapter 
  17. * to fulfill their duties 
  18. */ 
  19. public function save() {} 
  20. public function delete() {} 
  21. public function refresh() {} 
  22. //相當于上面代碼中的TableGateway類,關于gateway可以具體去了解一下。 
  23. class UserRepository { 
  24. public function __construct(DbAdapter $dbAdapter, RowGateway $rowGatewayPrototype = null) { 
  25. $this->dbAdapter = $dbAdapter; 
  26. $this->rowGatewayPrototype = ($rowGatewayPrototype) ? new RowGateway($this->dbAdapter, 'user'
  27. public function getUsers() { 
  28. $rows = array(); 
  29. foreach ($this->dbAdapter->fetchAllFromTable('user') as $rowData) { 
  30. $rows[] = $row = clone $this->rowGatewayPrototype; 
  31. $row->initialize($rowData); 
  32. return $rows; 

這幾個類其實和上面zend代碼中的類是對應的

Dbadapter -- adpater

RowGateWay -- ResultSet

UserRepository - TableGateWay

具體看代碼中的注釋。

這里的RowGateWay可以很明顯的看出在getusers中需要大量的實例化,那么原型模式就是很必要的了。

下面是運用這個類的代碼

 

 
  1. class ReadWriteRowGateway extends RowGateway { 
  2. public function __construct(DbAdapter $readDbAdapter, DbAdapter $writeDbAdapter, $tableName) { 
  3. $this->readDbAdapter = $readDbAdapter; 
  4. parent::__construct($writeDbAdapter, $tableName); 
  5. public function refresh() { 
  6. // utilize $this->readDbAdapter instead of $this->dbAdapter in RowGateway base implementation 
  7. // usage: 
  8. $userRepository = new UserRepository( 
  9. $dbAdapter, 
  10. new ReadWriteRowGateway($readDbAdapter, $writeDbAdapter, 'user'
  11. ); 
  12. $users = $userRepository->getUsers(); 
  13. $user = $users[0]; // instance of ReadWriteRowGateway with a specific row of data from the db 

以上內容是小編給大家介紹的php示例詳解Constructor Prototype Pattern 原型模式,希望大家喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 托里县| 开封市| 蓝山县| 蒙阴县| 逊克县| 南华县| 延安市| 台江县| 久治县| 莆田市| 平武县| 淅川县| 竹溪县| 安图县| 克东县| 汝州市| 秦皇岛市| 铜山县| 通河县| 那曲县| 枣强县| 汕头市| 永川市| 长海县| 博湖县| 青田县| 岐山县| 东港市| 嘉定区| 萝北县| 桓台县| 鄂尔多斯市| 濮阳市| 邵阳县| 罗源县| 柞水县| 射阳县| 郴州市| 且末县| 金门县| 锦屏县|