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类为该 ...
随机推荐
- Synthesis of memory barriers
A framework is provided for automatic inference of memory fences in concurrent programs. A method is ...
- Linux内核实践之序列文件【转】
转自:http://blog.csdn.net/bullbat/article/details/7407194 版权声明:本文为博主原创文章,未经博主允许不得转载. 作者:bullbat seq_fi ...
- MTK_GPIO口的定制
http://blog.csdn.net/zuoyioo7/article/details/77863291如果需要定制GPIO口呢,需要使用mediatek/dct/DrvGen.exe工具,点击O ...
- selenium访问百度 然后获取百度logo的截图
#!/usr/bin/env python # encoding: utf-8 import time from selenium import webdriver from PIL import I ...
- [BZOJ1294][SCOI2009]围豆豆Bean 射线法+状压dp+spfa
1294: [SCOI2009]围豆豆Bean Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 458 Solved: 305[Submit][Sta ...
- (7)oracle数据类型
字符型 char 定长 最大2000字符 例如 char(20) 表示定长20 不够的补空格 定长查询速度快 varchar2 变长 最大4000字符 省空间 clob 字符型大对象 最大 ...
- UVALive 3027 Corporative Network (带权并查集)
题意: 有 n 个节点,初始时每个节点的父节点都不存在,你的任务是执行一次 I 操作 和 E 操作,含义如下: I u v : 把节点 u 的父节点设为 v ,距离为| u - v | ...
- How to not display “Commit point reached - logical record count” counts
You can use the keyword silent, which is available in the options clause. You can set the followin ...
- 线程同步-CountDownLatch
应用场景: 有一个任务想要往下执行,但必须要等到其他的任务执行完毕后才可以继续往下执行. 假如我们这个想要继续往下执行的任务调用一个CountDownLatch对象的await()方法,其他的任务执行 ...
- 如何命令行编译Java工程
在src下的包含Main的包下打开命令行,javac -classpath “路径到src,不到包下” Main.java