PHP设计模式之原型设计模式原理与用法分析
本文实例讲述了PHP设计模式之原型设计模式原理与用法。分享给大家供大家参考,具体如下:
一、什么是原型设计模式
原型设计模式使用一种克隆技术来复制实例化的对象,新对象是通过复制原型实例创建的。原型设计模式的目的是通过使用克隆以减少
实例化对象的开销。
在原型设计模式中,Client类是不可缺少的一部分。
PHP有一个内置的克隆方法__clone()可以在设计模式中使用,但是不能直接访问,使用clone关键字即可。克隆不会启动构造函数。
二、什么时候使用原型设计模式
如果一个项目要求你创建某个原型对象的多个实例,就可以使用原型设计模式。
三、原型设计模式实例
这里以现代企业组织为例:
dept;
}
//员工ID号设置方法
publicfunctionsetId($id)
{
$this->id=$id;
}
//员工ID号获取方法
publicfunctiongetId()
{
return$this->id;
}
//员工名字设置方法
publicfunctionsetName($name)
{
$this->name=$name;
}
//员工名字获取方法
publicfunctiongetName()
{
return$this->name;
}
}
//市场部类
classMarketingextendsIAcmePrototype
{
constUNIT="Marketing";//标识
//市场部门类别
private$sales="sales";
private$promotion="promotion";
private$strategic="strategicplanning";
//克隆函数
function__clone()
{
}
//部门设置函数
publicfunctionsetDept($orgCode)
{
switch($orgCode)
{
case101:
$this->dept=$this->sales;
break;
case102:
$this->dept=$this->promotion;
break;
case103:
$this->dept=$this->strategic;
break;
default:
$this->dept="UnrecognizedMarketing";
}
}
}
//管理部类
classManagementextendsIAcmePrototype
{
constUNIT="Management";
private$research="research";
private$plan="planning";
private$operations="operations";
function__clone()
{
}
publicfunctionsetDept($orgCode)
{
switch($orgCode)
{
case201:
$this->dept=$this->research;
break;
case202:
$this->dept=$this->plan;
break;
case203:
$this->dept=$this->operations;
break;
default:
$this->dept="UnrecognizedMarketing";
}
}
}
//工厂部类
classEngineeringextendsIAcmePrototype
{
constUNIT="Engineering";
private$development="programming";
private$design="digitalartwork";
private$sysAd="systemadministration";
function__clone()
{
}
publicfunctionsetDept($orgCode)
{
switch($orgCode)
{
case301:
$this->dept=$this->development;
break;
case302:
$this->dept=$this->design;
break;
case303:
$this->dept=$this->sysAd;
break;
default:
$this->dept="UnrecognizedMarketing";
}
}
}
//客户类
classClient
{
private$market;//市场部类实例
private$manage;//管理部类实例
private$engineer;//工厂部类实例
//构造函数
publicfunction__construct()
{
$this->makeConProto();
//市场部类实例克隆
$Tess=clone$this->market;
$this->setEmployee($Tess,"TessSmith",101,"ts101-1234");
$this->showEmployee($Tess);
$Jacob=clone$this->market;
$this->setEmployee($Jacob,"JacobJones",102,"jj101-2234");
$this->showEmployee($Jacob);
//管理部类实例克隆
$Ricky=clone$this->manage;
$this->setEmployee($Ricky,"RickyRodrigues",203,"rr203-5634");
$this->showEmployee($Ricky);
//工程部类实例克隆
$Olivia=clone$this->engineer;
$this->setEmployee($Olivia,"Oliviaperez",302,"op302-1278");
$this->showEmployee($Olivia);
$John=clone$this->engineer;
$this->setEmployee($John,"JohnJackson",301,"jj301-1454");
$this->showEmployee($John);
}
//实例化部门对象
privatefunctionmakeConProto()
{
$this->market=newMarketing();
$this->manage=newManagement();
$this->engineer=newEngineering();
}
//员工信息设置方法
privatefunctionsetEmployee(IAcmePrototype$employee,$name,$dept,$id)
{
$employee->setName($name);
$employee->setDept($dept);
$employee->setId($id);
}
//员工信息显示方法
privatefunctionshowEmployee(IAcmePrototype$employee)
{
echo$employee->getName().'
';
echo$employee->getDept().'
';
echo$employee->getId().'
';
}
}
$client=newClient();
?>
运行结果:
TessSmith
sales
ts101-1234
JacobJones
promotion
jj101-2234
RickyRodrigues
operations
rr203-5634
Oliviaperez
digitalartwork
op302-1278
JohnJackson
programming
jj301-1454
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。