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)