当前位置:Gxlcms > PHP教程 > php示例详解ConstructorPrototypePattern原型模式,prototypepattern_PHP教程

php示例详解ConstructorPrototypePattern原型模式,prototypepattern_PHP教程

时间:2021-07-01 10:21:17 帮助过:19人阅读

php示例详解Constructor Prototype Pattern 原型模式,prototypepattern


原型模式中主要角色

抽象原型(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. {
  3. return array(
  4. 'factories' => array(
  5. 'Album\Model\AlbumTable' => function($sm) {
  6. $tableGateway = $sm->get('AlbumTableGateway');
  7. $table = new AlbumTable($tableGateway);
  8. return $table;
  9. },
  10. 'AlbumTableGateway' => function ($sm) {
  11. $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
  12. $resultSetPrototype = new ResultSet();
  13. $resultSetPrototype->setArrayObjectPrototype(new Album());//这个就是一个不变的原型
  14. return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);//传入到TableGateWay的构造函数中去
  15. },
  16. ),
  17. );
  18. }

注意并不是TableGateWay运用了原型模式而是ResultSet这个类运用了。每当tablegateway调用select()或者insert()等方法的时候都会建立一个ResultSet用来表示结果,这些ResultSet中公共部分被clone,而独特的部分类如data就会被initialize。

三、更多代码示例

  为了更清晰得了解这个原型,我们先抛开zend这个大框架,看一个完整的代码示例。示例来自

PHP Constructor 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. }
  8. }
  9. //运用prototype pattern的类,注意construct和initialize是分开的
  10. //相当于上面zend 代码里面的ResultSet类
  11. class RowGateway {
  12. public function __construct(DbAdapter $dbAdapter, $tableName) {
  13. $this->dbAdapter = $dbAdapter;
  14. $this->tableName = $tableName;
  15. }
  16. public function initialize($data) {
  17. $this->data = $data;
  18. }
  19. /**
  20. * Both methods require access to the database adapter
  21. * to fulfill their duties
  22. */
  23. public function save() {}
  24. public function delete() {}
  25. public function refresh() {}
  26. }
  27. //相当于上面代码中的TableGateway类,关于gateway可以具体去了解一下。
  28. class UserRepository {
  29. public function __construct(DbAdapter $dbAdapter, RowGateway $rowGatewayPrototype = null) {
  30. $this->dbAdapter = $dbAdapter;
  31. $this->rowGatewayPrototype = ($rowGatewayPrototype) ? new RowGateway($this->dbAdapter, 'user')
  32. }
  33. public function getUsers() {
  34. $rows = array();
  35. foreach ($this->dbAdapter->fetchAllFromTable('user') as $rowData) {
  36. $rows[] = $row = clone $this->rowGatewayPrototype;
  37. $row->initialize($rowData);
  38. }
  39. return $rows;
  40. }
  41. }

这几个类其实和上面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. }
  6. public function refresh() {
  7. // utilize $this->readDbAdapter instead of $this->dbAdapter in RowGateway base implementation
  8. }
  9. }
  10. // usage:
  11. $userRepository = new UserRepository(
  12. $dbAdapter,
  13. new ReadWriteRowGateway($readDbAdapter, $writeDbAdapter, 'user')
  14. );
  15. $users = $userRepository->getUsers();
  16. $user = $users[0]; // instance of ReadWriteRowGateway with a specific row of data from the db

以上内容是小编给大家介绍的php示例详解Constructor Prototype Pattern 原型模式,希望大家喜欢。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1061520.htmlTechArticlephp示例详解Constructor Prototype Pattern 原型模式,prototypepattern 原型模式中主要角色 抽象原型(Prototype)角色:声明一个克隆自己的接口 具体原型...

人气教程排行