本文实例讲述了php设计模式之原型设计模式原理与用法。分享给大家供大家参考,具体如下:
一、什么是原型设计模式
原型设计模式使用一种克隆技术来复制实例化的对象,新对象是通过复制原型实例创建的。原型设计模式的目的是通过使用克隆以减少
实例化对象的开销。
在原型设计模式中,client类是不可缺少的一部分。
php有一个内置的克隆方法__clone()可以在设计模式中使用,但是不能直接访问,使用clone关键字即可。克隆不会启动构造函数。
二、什么时候使用原型设计模式
如果一个项目要求你创建某个原型对象的多个实例,就可以使用原型设计模式。
三、原型设计模式实例
这里以现代企业组织为例:
<?php
/**
* 原型设计模式
* 以现代企业组织为例
**/
//部门抽象类
abstract class iacmeprototype
{
protected $id; //员工id号
protected $name; //员工名字
protected $dept; //员工部门
//克隆方法
abstract function __clone();
//员工部门设置方法
abstract function setdept($orgcode);
//员工部门获取方法
public function getdept()
{
return $this->dept;
}
//员工id号设置方法
public function setid($id)
{
$this->id = $id;
}
//员工id号获取方法
public function getid()
{
return $this->id;
}
//员工名字设置方法
public function setname($name)
{
$this->name = $name;
}
//员工名字获取方法
public function getname()
{
return $this->name;
}
}
//市场部类
class marketing extends iacmeprototype
{
const unit = "marketing"; //标识
//市场部门类别
private $sales = "sales";
private $promotion = "promotion";
private $strategic = "strategic planning";
//克隆函数
function __clone()
{
}
//部门设置函数
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; //工厂部类实例
//构造函数
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();
}
//员工信息设置方法
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();
?>
运行结果:
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
更多关于php相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《php数组(array)操作技巧大全》、《php基本语法入门教程》、《php运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!