This article mainly introduced the PHP design pattern prototype design pattern, simply described the prototype design pattern the concept, the principle and unifies the example form to analyze the PHP prototype design pattern definition and the use method, needs the friend can refer to the next
In this paper, the prototype design pattern of PHP design mode and its usage are described. Share to everyone for your reference, as follows:
First, what is the prototype design mode
The prototype design pattern uses a cloning technique to replicate the instantiated objects, which are created by copying the prototype instance. The purpose of the prototype design pattern is to use cloning to reduce
The cost of instantiating an object.
In prototype design mode, the client class is an indispensable part.
PHP has a built-in cloning method __clone()
that can be used in design mode, but not directly accessible, using the Clone keyword. Cloning does not start the constructor.
Second, when to use the prototype design mode
If you have a project that requires you to create multiple instances of a prototype object, you can use the prototype design pattern.
Iii. Examples of prototype design patterns
Here is an example of a modern enterprise organization:
<?php/*** prototype Design model * Taking modern enterprise organization as an example **///department abstract class iacmeprototype{protected $id; Employee ID number protected $name; Employee name protected $dept; Employee department//Cloning method abstract function __clone (); Employee Department Setup method abstract function setdept ($orgCode); Employee department Get method Public Function getdept () {return $this->dept; }//Employee ID Number Setting method public function SetId ($id) {$this->id = $id; }//Employee ID number Get method public function GetId () {return $this->id; }//Employee Name Setting method public Function SetName ($name) {$this->name = $name; }//Employee name Get method public Function GetName () {return $this->name; }}//Market Classes Class Marketing extends iacmeprototype{const UNIT = "Marketing"; Identity//Marketing Department category Private $sales = "Sales"; Private $promotion = "promotion"; Private $strategic = "strategic planning"; Cloning functions function __clone () {}//Department Set function Public function setdept ($orgCode) {switch ($orgCode) {Case 101: $this->dept = $this->sales; Break Case 102: $this->dept = $this->promotion; Break Case 103: $this->dept = $this->strategic; Break Default: $this->dept = "Unrecognized Marketing"; }}}//manages class Management extends iacmeprototype{const UNIT = "Management"; Private $research = "the"; Private $plan = "Planning"; Private $operations = "operations"; function __clone () {} public Function setdept ($orgCode) {switch ($orgCode) {case 201: $this- Dept = $this->research; Break Case 202: $this->dept = $this->plan; Break Case 203: $this->dept = $this->operations; Break Default: $this->dept = "Unrecognized Marketing"; }}}//Factory Classes class Engineering extends iacmeprototype{const UNIT = "Engineering"; Private $development = "Programming"; Private $design = "digital artwork"; Private $sysAd = "System Administration"; function __clone () {} PUBlic function Setdept ($orgCode) {switch ($orgCode) {case 301: $this->dept = $this->development ; Break Case 302: $this->dept = $this->design; Break Case 303: $this->dept = $this->sysad; Break Default: $this->dept = "Unrecognized Marketing"; }}}//Customer Class class client{private $market; Market class instance Private $manage; Manage class instances Private $engineer; Factory class Instance//constructor public function __construct () {$this->makeconproto (); Market class instance Cloning $Tess = Clone $this->market; $this->setemployee ($Tess, "Tess Smith", 101, "ts101-1234"); $this->showemployee ($Tess); $Jacob = Clone $this->market; $this->setemployee ($Jacob, "Jacob Jones", 102, "jj101-2234"); $this->showemployee ($Jacob); Managing a class instance clone $Ricky = Clone $this->manage; $this->setemployee ($Ricky, "Ricky Rodrigues", 203, "rr203-5634"); $this->showemployee ($Ricky); Project Partial instance cloning $Olivia = Clone $this->engineer; $this->setemployee ($Olivia, "Olivia Perez", 302, "op302-1278"); $this->showemployee ($Olivia); $John = Clone $this->engineer; $this->setemployee ($John, "John Jackson", 301, "jj301-1454"); $this->showemployee ($John); }//Instantiate Department object Private Function Makeconproto () {$this->market = new Marketing (); $this->manage = new Management (); $this->engineer = new Engineering (); }//Employee Information setting method Private Function Setemployee (Iacmeprototype $employee, $name, $dept, $id) {$employee->setname ($name); $employee->setdept ($dept); $employee->setid ($id); }//Employee information Display method Private function Showemployee (Iacmeprototype $employee) {echo $employee->getname (). ' <br/> '; echo $employee->getdept (). ' <br/> '; echo $employee->getid (). ' <br/> '; }} $client = new client ();? >
Operation Result:
Tess Smith
Sales
ts101-1234
Jacob Jones
Promotion
jj101-2234
Ricky Rodrigues
Operations
rr203-5634
Olivia Perez
Digital artwork
op302-1278
John Jackson
Programming
jj301-1454
Articles you may be interested in:
The implementation of SMS sending verification code in Laravel PHP instance
PHP receivemail to implement the mail-receiving function PHP instance
PHP tips for how to create a shared image