Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand the Constructor Prototype Pattern prototype pattern

Shulou Source: shulou.com Published: 2022-06-03 09:15:32 10月19日 Update

This article focuses on "how to understand the Constructor Prototype Pattern prototype pattern", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to understand the Constructor Prototype Pattern prototype pattern.

The main roles in the prototype pattern

Abstract prototype (Prototype) role: declare an interface that clones itself

Concrete Prototype role: implement an operation to clone yourself

When most of a class is the same and only the parts are different, if you need a large number of objects of this class, it is expensive to instantiate the same parts every time, and if you create the same parts of the object before clone, you can save money.

One way to implement php is to handle the initialization of this class separately by the _ _ construct () and initialize functions. The prototype is the common part in the construct, and the special part of each object is in the initialize. In this way, we first create a class that is not initialize, and then initialize every time we clone this class.

I. introduction

There is an albumTable class in zf2's model, which is equivalent to a helper class that manipulates database actions, which uses tablegateway.

In order to initialize albumtable with the same class each time, initialization is placed in the getServiceConfig () of the module.php file in the root directory, where the factory mode is used, and through the callback function, an alumTable is automatically called every time ServiceManager ($sm) needs to instantiate an object. As we can see in the following code, creating an albumTable also requires the creation of an AlbumTableGateWay in the same way, and this class uses the prototype pattern we are going to talk about.

Second, detailed explanation of the code

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\ Adapter'); $resultSetPrototype = new ResultSet () $resultSetPrototype- > setArrayObjectPrototype (new Album ()); / / this is an immutable prototype return new TableGateway ('album', $dbAdapter, null, $resultSetPrototype); / / passed into the constructor of TableGateWay},);}

Note that it is not TableGateWay that uses the prototype pattern, but the class ResultSet. Whenever tablegateway calls methods such as select () or insert (), a ResultSet is created to represent the result. The common part of the ResultSet is clone, while the unique part, such as data, is initialize.

Third, more code examples

To get a clearer picture of the prototype, let's put aside the big framework of zend and look at a complete code example. Examples come from

PHP Constructor Best Practices And The Prototype Pattern

The first half of this article about prototype pattern is actually a mix of how to use inheritance in constructors to improve extensibility. The two patterns may not seem easy to understand. Let's go straight to the last part of the code about prototype pattern.

Tags: Prototype pattern code part same function object instance that is time example role content overhead way method more learning different practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info vpn Linux Docker