C4:原型模式 Prototype
用原型实例指定创建对象的种类,并且拷贝这些原型创建新的对象.
应用场景:
A.用new创建对象通常有较为复杂的数据准备或权限准备
B.对象较大,拷贝对象可以节省内存
UML图:
class WorkExperience
{
private $workTime;
private $workAddress; public function __construct($workTime, $workAddress)
{
$this->workTime = $workTime;
$this->workAddress = $workAddress;
} /**
* @return mixed
*/
public function getWorkTime()
{
return $this->workTime;
} /**
* @return mixed
*/
public function getWorkAddress()
{
return $this->workAddress;
} /**
* @param mixed $workTime
*/
public function setWorkTime($workTime)
{
$this->workTime = $workTime;
} /**
* @param mixed $workAddress
*/
public function setWorkAddress($workAddress)
{
$this->workAddress = $workAddress;
} } abstract class Prototype
{
protected $age;
protected $name;
protected $workExperience; public function __clone() {
$this->workExperience = clone $this->workExperience; //将对象深拷贝
}
} class Resume extends Prototype
{
public function __construct($name)
{
$this->name = $name;
} /**
* @return mixed
*/
public function getAge()
{
return $this->age;
} /**
* @param mixed $age
*/
public function setAge($age)
{
$this->age = $age;
} /**
* @return mixed
*/
public function getName()
{
return $this->name;
} /**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
} /**
* @return mixed
*/
public function getWorkExperience()
{
return $this->workExperience;
} /**
* @param mixed $workExperience
*/
public function setWorkExperience(WorkExperience $workExperience)
{
$this->workExperience = $workExperience;
}
} $workExp = new WorkExperience('2013-2015', ' A commpany'); $resumeA = new Resume("jack");
$resumeA->setAge(23);
$resumeA->setWorkExperience($workExp); $resumeB = clone $resumeA;
$workExp->setWorkTime('2016-2017');
$resumeA->setWorkExperience($workExp); echo $resumeA->getName() . PHP_EOL;
echo $resumeA->getAge() . PHP_EOL;
echo $resumeA->getWorkExperience()->getWorkTime() ;
echo $resumeA->getWorkExperience()->getWorkAddress() . PHP_EOL;
echo $resumeB->getWorkExperience()->getWorkTime() ;
echo $resumeB->getWorkExperience()->getWorkAddress() . PHP_EOL;
C4:原型模式 Prototype的更多相关文章
- Net设计模式实例之原型模式( Prototype Pattern)
一.原型模式简介(Brief Introduction) 原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象. Specify the kin ...
- 二十四种设计模式:原型模式(Prototype Pattern)
原型模式(Prototype Pattern) 介绍用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象.示例有一个Message实体类,现在要克隆它. MessageModel usin ...
- 设计模式(四)原型模式Prototype(创建型)
设计模式(四)原型模式Prototype(创建型) 1. 概述 我们都知道,创建型模式一般是用来创建一个新的对象,然后我们使用这个对象完成一些对象的操作,我们通过原型模式可以快速的创建一个对象 ...
- 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)
原文:乐在其中设计模式(C#) - 原型模式(Prototype Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 原型模式(Prototype Pattern) 作者:weba ...
- 原型模式-Prototype(Java实现)
原型模式-Prototype 通过复制(克隆.拷贝)一个指定类型的对象来创建更多同类型的对象. 就像去蛋糕店买蛋糕一样. 柜台里的蛋糕都是非卖品. 只是为顾客提供一种参照. 当顾客看上某一个样式的蛋糕 ...
- 原型模式 prototype 创建型 设计模式(七)
原型模式 prototype 意图 用原型实例指定需要创建的对象的类型,然后使用复制这个原型对象的方法创建出更多同类型的对象 显然,原型模式就是给出一个对象,然后克隆一个或者更多个对象 小时候看 ...
- PHP设计模式 原型模式(Prototype)
定义 和工厂模式类似,用来创建对象.但实现机制不同,原型模式是先创建一个对象,采用clone的方式进行新对象的创建. 场景 大对象的创建. 优点 1.可以在运行时刻增加和删除产品 2.可以改变值或结构 ...
- 设计模式系列之原型模式(Prototype Pattern)——对象的克隆
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 六个创建模式之原型模式(Prototype Pattern)
定义: 使用原型实例指定创建对象的种类,并通过拷贝这个原型的属性创建新的对象. 结构图: Prototype:抽象原型类,声明克隆方法的接口,并是所有原型类的公共父类.在Java中,Object类为该 ...
随机推荐
- 接下来打算写一下visual stuido 2013使用git进行远端管理。
虽然我有了vs的账号,也vs2013开始已经可以进行远端的账户管理了,可是vs的版控毕竟有些依赖vs,想想还是用git吧 今天把这个环境的整套都弄地基本熟了.记录一下,算是一个小结.开始搭建系统框架
- 各版本Sql Server下载地址全
SQL Server 2014简体中文企业版 文件名:cn_sql_server_2014_enterprise_edition 32位下载地址:ed2k://|file|cn_sql_server_ ...
- checkbox选中 解决兼容问题
jquery 1.9 checkbox 是否选中 if($("#chk_selectedall").prop('checked')) checkbox 选中 $("#ch ...
- linux c/c++ 代码使用 doxygen 自动生成文档
www.doxygen.org 的使用非常方便,下面分成2步介绍一下 1. 注释风格,需要在c/c++代码中按照下面的风格添加注释,基本上还是很顺手的 C++的注释风格 主要使用下面这种样式:即在注释 ...
- Linux内核实践之tasklet机制【转】
转自:http://blog.csdn.net/bullbat/article/details/7423321 版权声明:本文为博主原创文章,未经博主允许不得转载. 作者:bullbat 源代码分析与 ...
- Linux c 目录操作函数scandir
头文件#include <dirent.h> 函数定义:int scandir(const char *dir,struct dirent **namelist,int (*filter ...
- input 输入框 光标错位问题 、移动端输入框/input框光标错位问题、微信H5输入框/input框光标错位问题
在IOS系统下的问题: 搜索出的建议如下: 你应该是用fixed定位做的弹出框,弹出框里面有文本框.fixed在ios上兼容不友好,会造成光标乱跳.建议用better-scroll,或者用absolu ...
- hdu 1856(hash+启发式并查集)
More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) ...
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- 代码编辑器[0] -> Vim/gVim[1] -> Vim 的快捷键操作
快捷键 / Shortcut Keys 1 基本操作 / Basic Operation Vim的基本操作主要可以参考以下几张图,参考链接, 命令行模式 i 从光标所在字符前插 ...