Php example description ConstructorPrototypePattern prototype

Source: Internet
Author: User
Tags php example zend framework
The prototype is a creator mode. It is characterized by "copying" an existing instance to return a new instance, rather than creating a new instance.

The prototype is a creator mode. It is characterized by "copying" an existing instance to return a new instance, rather than creating a new instance.

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.

I mentioned this in the official zend framework manual, but I have not explained it in detail. Let's analyze it.

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. the getServiceConfig () of the PHP file uses the factory mode and 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 methods 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.