prototype mode
Prototype Pattern (Prototype Pattern) , by copying an existing object to generate a new object, the copied object is called the prototype.
Advantages: Do not use new to create objects, new will consume a lot, and prototype mode only needs to copy memory.
There are two types of copy in prototype mode: deep copy and shallow copy .
Deep copy is to create objects by copying memory (including referenced objects), not only the properties and methods of the basic type of the class, but also the objects of other classes referenced in the class.
Shallow copy will copy the properties and methods of the basic type of the class, and for the referenced object, only the referenced value will be copied.
Deep copy can be achieved by serializing the object into a byte stream and then deserializing it; shallow copy can directly use the clone method (in php).
Roles: 1) Abstract prototype roles; 2) Concrete realization of prototype roles.
Application scenarios:
When you need to create a complex object that takes a lot of time to construct, and there is already a similar object.
When the consumer does not care about the object construction process.
Such as: reconstruction of workflow instances, replication of complex data entities.
The following examples describe in detail how to use the prototype mode for deep copy and shallow copy, and how the final effect is, written in php.
1 <? php 2 /* 3 * prototype pattern example 4 */ 5 error_reporting ( E_ALL ); 6 ini_set ('display_errors','on' ); 7 8 /* 9 * abstract interface class 10 * including deep copy and shallow copy method, so that all classes that implement it can implement 11 */ 12 interface Prototype{ 13 public function shadowCopy(); // shallow copy 14 public function deepCopy(); // deep copy 15 } 16 17 /* 18 * The class Person of the object to be referenced 19 */ 20 class Person{ 21 public $name ; 22 public function __construct( $name ) { 23 $this ->name = $name ; 24 } 25 public function getName(){ 26 return $this -> name; 27 } 28 } 29 30 /* 31 *prototype class 32 */ 33 class Demo implements Prototype{ 34 private $person; 35 public function __construct($person) { 36 $this->person = $person; 37 } 38 public function setPerson($person){ 39 $this->person = $person; 40 } 41 public function getPerson(){ 42 return $this->person; 43 } 44 public function shadowCopy() { 45 return clone $this; 46 } 47 public function deepCopy() { 48 $serializeObj = serialize($this); 49 $cloneObj = unserialize($serializeObj); 50 return $cloneObj; 51 } 52 private function __clone() { 53 echo "come here when clone"; 54 } 55 } 56 57 $person = new Person("Mary" ); 58 $demoObj = new Demo( $person ); 59 $shadowCopyObj = $demoObj -> shadowCopy(); 60 $deepCopyObj = $demoObj -> deepCopy(); 61 62 // After copying, the prototype object, deep copy object, and the reference object in the shallow copy object have the same value 63 var_dump ( $demoObj ->getPerson()->getName()). PHP_EOL ; // string(4) "Mary" 64 var_dump ( $shadowCopyObj- >getPerson()->getName()).PHP_EOL ; // string(4) "Mary" 65 var_dump ( $deepCopyObj ->getPerson()->getName()). PHP_EOL ; // string(4) "Mary" 66 67 //Change the reference object of the prototype object After the value of , the prototype object and the shallow copy object are changed because of the same reference address, and the deep copy object remains unchanged . 68 $person ->name = "Jack" ; 69 var_dump ( $demoObj ->getPerson()->getName()). PHP_EOL ; // string(4) "Jack" 70 var_dump ( $shadowCopyObj ->getPerson()->getName()). PHP_EOL ; // string(4) "Jack" 71 var_dump ( $deepCopyObj ->getPerson()->getName()). PHP_EOL ; // string(4) "Mary" 72 73 //After replacing the reference object of the prototype object, the prototype object changes again, and the shallow copy object is because of the reference address No change, so unchanged, the deep copy object is not affected or keeps the original value 74 $person2 = new Person("Peter" ); 75 $demoObj ->setPerson( $person2 ); 76 var_dump ( $demoObj ->getPerson( )->getName()). PHP_EOL ; // string(4) "Peter" 77 var_dump ( $shadowCopyObj ->getPerson()->getName()). PHP_EOL ; //string(4) "Jack" 78 var_dump($deepCopyObj->getPerson()->getName()).PHP_EOL;//string(4) "Mary" 79 80 ?>
Reference documentation: