PHP prototype prototype Model learning notes

Source: Internet
Author: User

The question to be resolved: Can we reduce the use of new, while avoiding the need for new objects, to understand the object's class name.


Train of thought: PHP5 provides a cloning method, we can add an object, and then each need to add and her similar objects, cloning he can be.


Prototype (Prototype) Pattern Example:

The code is as follows Copy Code

<?php

Machine Soldier Class

Class Marine

{

The user ID to which it belongs

Public $playerID

constructor, parameter is the user's ID

Public function __construct ($id)

{

$this->playerid = $id;

}

}

The management of the class

Class Troopmanager

{

arrays, for storing prototypes of multiple classes

Public $troopPrototype = Array ();

Add the prototype, the first parameter is the prototype name, the second parameter is the prototype object

Public Function Addprototype ($name, $prototype)

{

$this->troopprototype[$name] = $prototype;

}

Get the clone of the prototype, which is the alternative to the new method, the parameter is the prototype name

Public Function Getprototype ($name)

{

return clone $this->troopprototype[$name];

}

}

Initialize the class management

$manager = new Troopmanager ();

Initialize two of different player's prototypes

$m 1 = new Marine (1);

$m 2 = new Marine (2);

Adding prototypes and naming prototypes with more easily memorized names

$manager->addprototype (' Marine of 1 ', $m 1);

$manager->addprototype (' Marine of 2 ', $m 2);

When new objects are needed, we don't have to know the object's class name and initialization work

$m 3 = $manager->getprototype (' Marine of 1 ');

?>

Usage Summary: Prototype mode can encapsulate the work details of the new object.


Implementation Summary: A prototype management class is needed to achieve the method of adding and acquiring a cloned prototype. Note here because for simplicity, there are a few things that we can actually do to make changes to the cloning method, or we can use interfaces to standardize each prototype class.

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.