PHP設(shè)計模式之原型設(shè)計模式原理與用法分析
本文實例講述了PHP設(shè)計模式之原型設(shè)計模式原理與用法。分享給大家供大家參考,具體如下:
一、什么是原型設(shè)計模式
原型設(shè)計模式使用一種克隆技術(shù)來復(fù)制實例化的對象,新對象是通過復(fù)制原型實例創(chuàng)建的。原型設(shè)計模式的目的是通過使用克隆以減少
實例化對象的開銷。
在原型設(shè)計模式中,Client類是不可缺少的一部分。
PHP有一個內(nèi)置的克隆方法__clone()可以在設(shè)計模式中使用,但是不能直接訪問,使用clone關(guān)鍵字即可??寺〔粫訕?gòu)造函數(shù)。
二、什么時候使用原型設(shè)計模式
如果一個項目要求你創(chuàng)建某個原型對象的多個實例,就可以使用原型設(shè)計模式。
三、原型設(shè)計模式實例
這里以現(xiàn)代企業(yè)組織為例:
<?php
/**
* 原型設(shè)計模式
* 以現(xiàn)代企業(yè)組織為例
**/
//部門抽象類
abstract class IAcmePrototype
{
protected $id; //員工ID號
protected $name; //員工名字
protected $dept; //員工部門
//克隆方法
abstract function __clone();
//員工部門設(shè)置方法
abstract function setDept($orgCode);
//員工部門獲取方法
public function getDept()
{
return $this->dept;
}
//員工ID號設(shè)置方法
public function setId($id)
{
$this->id = $id;
}
//員工ID號獲取方法
public function getId()
{
return $this->id;
}
//員工名字設(shè)置方法
public function setName($name)
{
$this->name = $name;
}
//員工名字獲取方法
public function getName()
{
return $this->name;
}
}
//市場部類
class Marketing extends IAcmePrototype
{
const UNIT = "Marketing"; //標(biāo)識
//市場部門類別
private $sales = "sales";
private $promotion = "promotion";
private $strategic = "strategic planning";
//克隆函數(shù)
function __clone()
{
}
//部門設(shè)置函數(shù)
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";
}
}
}
//管理部類
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";
}
}
}
//工廠部類
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";
}
}
}
//客戶類
class Client
{
private $market; //市場部類實例
private $manage; //管理部類實例
private $engineer; //工廠部類實例
//構(gòu)造函數(shù)
public function __construct()
{
$this->makeConProto();
//市場部類實例克隆
$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);
//管理部類實例克隆
$Ricky = clone $this->manage;
$this->setEmployee($Ricky,"Ricky Rodrigues",203,"rr203-5634");
$this->showEmployee($Ricky);
//工程部類實例克隆
$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);
}
//實例化部門對象
private function makeConProto()
{
$this->market = new Marketing();
$this->manage = new Management();
$this->engineer = new Engineering();
}
//員工信息設(shè)置方法
private function setEmployee(IAcmePrototype $employee,$name,$dept,$id)
{
$employee->setName($name);
$employee->setDept($dept);
$employee->setId($id);
}
//員工信息顯示方法
private function showEmployee(IAcmePrototype $employee)
{
echo $employee->getName() . '<br />';
echo $employee->getDept() . '<br />';
echo $employee->getId() . '<br />';
}
}
$client = new Client();
?>
運行結(jié)果:
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
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP使用strrev翻轉(zhuǎn)中文亂碼問題的解決方法
這篇文章主要介紹了PHP使用strrev翻轉(zhuǎn)中文亂碼問題的解決方法,通過自定義函數(shù)遍歷字符串并設(shè)置編碼格式解決亂碼問題,需要的朋友可以參考下2017-01-01
php中將時間差轉(zhuǎn)換為字符串提示的實現(xiàn)代碼
通過傳入數(shù)據(jù)庫中存儲的文章發(fā)表時的UNIX時間戳,來轉(zhuǎn)化為例如 幾分鐘前,幾小時前,幾天前 這樣的提示。2011-08-08
php在服務(wù)器執(zhí)行exec命令失敗的解決方法
出于安全的原因,服務(wù)器是不允許php或者其他語言執(zhí)行exec命令的,當(dāng)你有特殊需要php在服務(wù)器執(zhí)行exec命令時,你需要設(shè)置兩個地方,不然就無法執(zhí)行成功2012-03-03
PHP+MySQL實現(xiàn)輸入頁碼跳轉(zhuǎn)到指定頁面功能示例
這篇文章主要介紹了PHP+MySQL實現(xiàn)輸入頁碼跳轉(zhuǎn)到指定頁面功能,結(jié)合實例形式分析了php連接mysql數(shù)據(jù)庫進行數(shù)據(jù)查詢及分頁顯示、指定頁數(shù)跳轉(zhuǎn)顯示等相關(guān)操作技巧,需要的朋友可以參考下2018-06-06

