Prototype mode is more figuratively called clone mode. Its main action is to clone objects, but to call the cloned object the original prototype, so the pattern is named. To be honest, the way it’s used really feels like clone mode is more appropriate. Gof class diagram and explanation Gof definition: Specify the types of objects to be created with prototype instances, and create new objects by copying these prototypes

GoF class diagram

Code implementation

abstract class Prototype
{
    public $v = 'clone' . PHP_EOL;

    public function __construct()
    {
        echo 'create' . PHP_EOL;
    }

    abstract public function __clone();
}
Copy the code

First, we define a prototype by simulation, which mainly simulates the __clone() method. In fact, this is a magic method in PHP, there is no need to define it, just need to implement it in the prototype class. When an object is cloned externally using the clone keyword, it is directly involved in this magic method. In this magic method we can handle properties, especially reference properties. In this example, we use only one variable of value type. The problem with reference types is not reflected. We will demonstrate the handling of reference type variables in a later example.

class ConcretePrototype1 extends Prototype
{
    public function __clone()
    {
    }
}

class ConcretePrototype2 extends Prototype
{
    public function __clone()
    {
    }
}
Copy the code

The prototype of the simulated concrete implementation is essentially a concrete implementation of the __clone() method. We’ll talk about that later when we look at specific examples.

class Client
{
    public function operation()
    {
        $p1 = new ConcretePrototype1();
        $p2 = clone $p1;

        echo $p1->v;
        echo $p2->v;
    }
}

$c = new Client();
$c->operation();
Copy the code

The client uses clone to copy P2 with the same $V property.

  • The prototype pattern appears to copy the same object, but note that the __construct() method is not called when you copy, so when you run this code, create prints only once. This brings out one of the biggest features of the prototype pattern — the reduction in object creation overhead.
  • Based on the above features, we can quickly copy a large number of identical objects, such as stuffing a large number of identical objects into an array.
  • If the copied objects are all properties of value type, we can modify them arbitrarily without affecting the prototype. If there are variables of reference type, some processing needs to be done in the __clone() method, otherwise modifying the contents of the reference variable of the copied object will affect the contents of the prototype object.

How does the operating system for our phones (and imagine the operating system for our PCS) get installed on our devices? In fact, they are constantly copying and copying the original system. A good example of this is Microsoft, which became an empire by copying Windows operating system onto CDS and selling it to millions of people (of course, there is nothing China does here). As for the Chinese market, a large number of masters cracked Windows and kept copying copies of this document before installing it into our computers. This is true of operating systems and software for mobile phones, smart devices and other products. Developing unlimited copies at once is what makes the software industry so profitable. After all, our system is also developed on the basis of Android native system by many engineers day and night 996, quickly and continuously copied to the upcoming factory phone!!

Full code: github.com/zhangyue050…

Take mobile phones as an example, this time we are to develop a batch of customized machines according to the needs of different operators, that is, package machines. The phones are all really the same, most of them have the same specs, but the carrier system is different, and the occasional model may have different CPU and memory. At this point, we can use the prototype mode to quickly copy and change only a few of the differences.

Prototype mode produces mobile phone class diagrams

Full source: github.com/zhangyue050…

<? php interface ServiceProvicer { public function getSystem(); } class ChinaMobile implements ServiceProvicer { public $system; Public function getSystem(){return "中国 mobile ". $this->system; } } class ChinaUnicom implements ServiceProvicer { public $system; Public function getSystem(){return $this->system; } } class Phone { public $service_province; public $cpu; public $rom; } class CMPhone extends Phone { function __clone() { // $this->service_province = new ChinaMobile(); } } class CUPhone extends Phone { function __clone() { $this->service_province = new ChinaUnicom(); } } $cmPhone = new CMPhone(); $cmPhone - > CPU = "1.4 G"; $cmPhone->rom = "64G"; $cmPhone->service_province = new ChinaMobile(); $cmPhone->service_province->system = 'TD-CDMA'; $cmPhone1 = clone $cmPhone; $cmPhone1->service_province->system = 'TD-CDMA1'; var_dump($cmPhone); var_dump($cmPhone1); echo $cmPhone->service_province->getSystem(); echo $cmPhone1->service_province->getSystem(); $cuPhone = new CUPhone(); $cuPhone - > CPU = "1.4 G"; $cuPhone->rom = "64G"; $cuPhone->service_province = new ChinaUnicom(); $cuPhone->service_province->system = 'WCDMA'; $cuPhone1 = clone $cuPhone; $cuPhone1->rom = "128G"; $cuPhone1->service_province->system = 'WCDMA1'; var_dump($cuPhone); var_dump($cuPhone1); echo $cuPhone->service_province->getSystem(); echo $cuPhone1->service_province->getSystem();Copy the code

It prints a lot of stuff, but the main thing is to look at the mobile phone, which is the __clone() method in CMPhone. We didn’t re-initialize a new object. In this case, the same object is in the copied cmPhone. Yes, that’s the copying of references. A reference simply copies the address of the reference; they point to the same object. The properties of the service_province object in cmPhone also change. In CUPhone, we re-new a new service_province object. This time the external cuPhone refers to the value of the object. The most important thing in the prototype pattern is to pay attention to the above two points, while the normal value attributes are copied directly and do not cause this problem. Here again involved in the other two concepts: a shallow copy and deep shallow copy, refers to the object to be copied all variables contain the same value with the original object, and all references to other objects still refer to the original object Deep copy of the reference object variables refer to copy of the new object, rather than the original object referenced