Principle and Usage Analysis of Prototype Design Pattern of PHP Design Pattern
- 2021-09-24 21:36:23
- OfStack
This paper describes the principle and usage of prototype design pattern of PHP design pattern with examples. Share it for your reference, as follows:
1. What is the prototype design pattern
The prototype design pattern uses a cloning technique to replicate instantiated objects, and new objects are created by replicating prototype instances. The purpose of prototyping design pattern is to reduce the number of clones
The overhead of instantiating the object.
In the prototype design pattern, Client class is an indispensable part.
PHP has a built-in cloning method
__clone()
It can be used in design mode, but cannot be accessed directly. Use the clone keyword. Cloning does not start the constructor.
2. When to use prototype design patterns
If a project requires you to create multiple instances of a prototype object, you can use the prototype design pattern.
3. Examples of prototype design patterns
Take modern enterprise organization as an example:
<?php
/**
* Prototype design pattern
* Take modern enterprise organization as an example
**/
// Departmental abstract class
abstract class IAcmePrototype
{
protected $id; // Employees ID No.
protected $name; // Employee name
protected $dept; // Employee department
// Cloning method
abstract function __clone();
// Employee Department Setup Method
abstract function setDept($orgCode);
// Employee Department Acquisition Method
public function getDept()
{
return $this->dept;
}
// Employees ID Number setting method
public function setId($id)
{
$this->id = $id;
}
// Employees ID Number acquisition method
public function getId()
{
return $this->id;
}
// Employee name setting method
public function setName($name)
{
$this->name = $name;
}
// Employee name acquisition method
public function getName()
{
return $this->name;
}
}
// Marketing category
class Marketing extends IAcmePrototype
{
const UNIT = "Marketing"; // Identification
// Market sector category
private $sales = "sales";
private $promotion = "promotion";
private $strategic = "strategic planning";
// Clonal function
function __clone()
{
}
// Department setting 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";
}
}
}
// Management department class
class Management extends IAcmePrototype
{
const UNIT = "Management";
private $research = "research";
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 division
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 category
class Client
{
private $market; // Marketing class instance
private $manage; // Management department class instance
private $engineer; // Instance of factory department class
// Constructor
public function __construct()
{
$this->makeConProto();
// Marketing 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);
// Management class instance cloning
$Ricky = clone $this->manage;
$this->setEmployee($Ricky,"Ricky Rodrigues",203,"rr203-5634");
$this->showEmployee($Ricky);
// Cloning of engineering department class instance
$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 a 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);
}
// Display method of employee information
private function showEmployee(IAcmePrototype $employee)
{
echo $employee->getName() . '<br />';
echo $employee->getDept() . '<br />';
echo $employee->getId() . '<br />';
}
}
$client = new Client();
?>
Run results:
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
For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"
I hope this article is helpful to everyone's PHP programming.