In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.