Analysis of php prototype _ php instances

Source: Internet
Author: User
This article mainly introduces the php prototype. For more information, see the prototype:

Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.
Application Scenario: the class has many resources, performance and security requirements, and is generally used in combination with the factory method.

The Code is as follows:


<? Php
/**
* Prototype
*/
// Declare an interface for cloning itself
Interface Prototype {
Function copy ();
}
// The product needs to clone itself
Class Student implements Prototype {
// For simplicity, the get set is not used here
Public $ school;
Public $ major;
Public $ name;
Public function _ construct ($ school, $ major, $ name ){
$ This-> school = $ school;
$ This-> major = $ major;
$ This-> name = $ name;
}
Public function printInfo (){
Printf ("% s, % s, % sn", $ this-> school, $ this-> major, $ this-> name );
}
Public function copy (){
Return clone $ this;
}
}
$ Stu1 = new Student ('tsinghua ', 'computer', 'zhang san ');
$ Stu1-> printInfo ();
$ Stu2 = $ stu1-> copy ();
$ Stu2-> name = 'Li si ';
$ Stu2-> printInfo ();
?>

Here we can see that if there are many member variables in the class, if multiple new objects are created from the outside and assigned values one by one, less efficient code redundancy is also prone to errors, copying itself through the prototype and then making minor modifications is another new object.

The first part of the design pattern is the summary of the Creation pattern. There are two more structural design patterns and behavioral design patterns to continue later.

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.