PHP prototype design pattern use case study

Source: Internet
Author: User
This time for you to bring PHP prototype design patterns use case analysis, PHP prototype design patterns used in the attention of what, the following is the actual case, together to see.

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";          Clone 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->de          PT = $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 class 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

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

Php+curl obtaining the public number Access_token step analysis

PHP solves the fault location by strace positioning and resolves

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.