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.