PHP design mode (vi) prototype mode (Prototype for PHP)

Source: Internet
Author: User

Prototype design Pattern: use prototype instances to specify the kind of objects that are created, and create new objects by copying those prototypes.

The prototype design pattern is simply, as the name implies, a design pattern that preserves the prototype without creating new Objects.

Disadvantages:
The main disadvantage of the prototype design pattern is that the cloning method needs to detect the function of the class, which is easier for the new class, but it will not be easy to transform the existing Classes.

Interface Prototype {    publicfunctioncopy();}

Prototype Class:

classPrototypedemoImplementsprototype{Private $_name;  public function__construct ($name)    {        //This could be complex logic.        $this->_name =$name; }     public functionGetmul () {return $this->_name *$this-_name; }     public function Copy()    {        //Post-clone Logic        $this->_name + +; return Clone $this; }}

Customer Class://input 10

//Customer Classclassclient{ public functionmain () {$pro 1=NewPrototypedemo (' 10 '); Echo $pro 1-Getmul (); Echo"<br>"; $pro 2=$pro 1-Copy(); Echo $pro 2-Getmul (); }}

Calling the Client:

$obj New Client (); $obj->main ();

Output result:

 - 121

Show incoming 10 then Getmul method to do a exponentiation to get 10*10 = 100

Then clone the object, when the copy method of the prototype class is executed, $_name 1 (the cloned Logic) and then do the multiplication, get 11*11 = 121

PHP design mode (vi) prototype mode (Prototype for PHP)

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.