The ConstructorPrototypePattern prototype is prototypepattern. Php sample details ConstructorPrototypePattern Prototype mode, Prototype role in prototypepattern Prototype mode: declare a specific Prototype for cloning your own interface php sample details Constructor Prototype Pattern prototypepattern
Main role in prototype mode
Prototype role: declares an interface for cloning itself
Concrete Prototype role: implements a clone operation.
When most of a class is the same, only some of them are different. if you need a large number of objects of this class, it is very costly to instantiate the same parts repeatedly each time, if you create the same parts of the object before cloning, you can save the cost.
One implementation method for php is to separate the _ construct () and initialize functions to process the class initialization respectively. prototype is the public part in construct, initialize contains special parts of each object. In this way, we will first create a class without initialize, and then perform initialize every time we clone this class.
This idea is mentioned in the zend framework official manual.
I. INTRODUCTION
In the zf2 model, there is an albumTable class, which is equivalent to a helper class that operates database actions. tablegateway is used in it.
In order to initialize albumtable every time it is the same class, the initialization work is put to the module in the root directory. getServiceConfig () of the PHP file, which uses the factory mode and uses the callback function. when each ServiceManager ($ sm) needs to instantiate an object, it will automatically call to create an alumTable. The following code shows that creating an albumTable also requires creating an AlbumTableGateWay in the same way. this class uses the prototype mode we want to talk about.
II. Code explanation
Public function getServiceConfig () {return array ('factories '=> array ('Album \ Model \ albumtable' => function ($ sm) {$ tableGateway = $ sm-> get ('albumtablegateway'); $ table = new AlbumTable ($ tableGateway); return $ table ;}, 'albumtablegateway' => function ($ sm) {$ dbAdapter = $ sm-> get ('zend \ Db \ Adapter '); $ resultSetPrototype = new ResultSet (); $ resultSetPrototype-> setArrayObjectPrototype (new Album (); // This is a constant prototype. return new TableGateway ('alipay', $ dbAdapter, null, $ resultSetPrototype ); // input to the constructor of TableGateWay },),);}
Note that not TableGateWay uses the prototype mode, but the ResultSet class. Each time tablegateway calls a select () or insert () method, a ResultSet is created to indicate the result. The public part of these ResultSet is cloned, the unique partial classification, such as data, will be initialize.
III. More Code examples
To better understand the prototype, let's look at a complete code example by setting aside the zend Framework. Example from
PHP Constructor Best Practices And The Prototype Pattern
In this article, the first half of prototype pattern is actually a mixture of how to use inheritance in constructors to improve scalability. The two modes may not seem very easy to understand, let's look at the last part of the code about prototype pattern.
<? The adapter class, which is common in php // framework, is used to adapt to various databases and encapsulate some basic database connection operations. // Equivalent to the adapter class DbAdapter {public function fetchAllFromTable ($ table) {return $ arrayOfData ;}/// class that uses prototype pattern in the above code, note that construct and initialize are separated // equivalent to the ResultSet class RowGateway {public function _ construct (DbAdapter $ dbAdapter, $ tableName) in the above zend code) {$ this-> dbAdapter = $ dbAdapter; $ this-> tableName = $ tableName;} public function initialize ($ data) {$ this-> data = $ data ;} /*** Both met Hods require access to the database adapter * to fulfill their duties */public function save () {} public function delete () {} public function refresh () {}} // equivalent to the TableGateway class in the above code. For more information about gateway, see. Class UserRepository {public function _ construct (DbAdapter $ dbAdapter, RowGateway $ rowGatewayPrototype = null) {$ this-> dbAdapter = $ dbAdapter; $ this-> rowGatewayPrototype = ($ rowGatewayPrototype )? New RowGateway ($ this-> dbAdapter, 'User')} public function getUsers () {$ rows = array (); foreach ($ this-> dbAdapter-> fetchAllFromTable ('user') as $ rowData) {$ rows [] = $ row = clone $ this-> rowGatewayPrototype; $ row-> initialize ($ rowData);} return $ rows ;}}
These classes actually correspond to the classes in the above zend code.
Dbadapter -- adpater
RowGateWay -- ResultSet
UserRepository-TableGateWay
For more information, see the comments in the code.
The RowGateWay here clearly shows that a large number of instantiation is required in getusers, so the prototype mode is necessary.
The following code uses this class:
class ReadWriteRowGateway extends RowGateway { public function __construct(DbAdapter $readDbAdapter, DbAdapter $writeDbAdapter, $tableName) { $this->readDbAdapter = $readDbAdapter; parent::__construct($writeDbAdapter, $tableName); } public function refresh() { // utilize $this->readDbAdapter instead of $this->dbAdapter in RowGateway base implementation }}// usage:$userRepository = new UserRepository( $dbAdapter, new ReadWriteRowGateway($readDbAdapter, $writeDbAdapter, 'user'));$users = $userRepository->getUsers();$user = $users[0]; // instance of ReadWriteRowGateway with a specific row of data from the db
The above section describes the Constructor Prototype Pattern Prototype, which I hope you will like.
Prototype Pattern Prototype: specifies the Prototype role in prototypepattern Prototype...