Analyze the php prototype mode and the php prototype. Analyze the php prototype mode and the php prototype mode: Use the prototype instance to specify the object type, and copy the prototype to create a new object. Application scenario: class Resource Analysis: php prototype analysis, php prototype analysis
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.
Original 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: Class resources...