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类为该 ...
随机推荐
- AGC007 - C Pushing Ball
Description 题目链接 懒得写详细题意了, 放个链接 \(n\le 2*10^5\) 个球, \(n+1\) 个坑, 排成数轴, 球坑交替. 相邻球-坑距离为等差数列 \(d\). 给定首项 ...
- super真的是调用父类吗?
#!/usr/bin/env python # -*- coding:utf-8 -*- # author:love_cat class A: def __init__(self): print(&q ...
- Java HotSpot(TM) 64-Bit Server VM warning: Insufficient space for shared memory file: /tmp/hsperfdata_hadoop/44512
早上登录hbase shell,出现异常: [hadoop@node002 ~]$ hbase shell Java HotSpot(TM) 64-Bit Server VM warning: Ins ...
- flask的orm框架(SQLAlchemy)-操作数据
# 原创,转载请留言联系 Flask-SQLAlchemy 实现增加数据 用 sqlalchemy 添加数据时,一定要注意,不仅仅要连接到数据表,并且你的创建表的类也必须写进来.而且字段和约束条件要吻 ...
- RobotFramework自动化1-环境搭建【转载】
本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/robotframework/ 前言 Robot Framework是一款python编 ...
- 使用navicat连接linux服务器数据库方法
主机名用localhost 要使用SSH连接
- vs2015 建立项目报错:值不能为空,参数名:path1的错误解决与“未将对象引用到对象的实例”
“值不能为空,参数名:path1” 的错误.原因就是安卓sdk的路径不正确. 最简单的解决办法如下: 找到C:\Program Files (x86)\Android\android-sdk.进入文件 ...
- Ubuntu安装java环境
Ubuntu安装java环境 1.添加ppa sudo add-apt-repository ppa:webupd8team/java sudo apt-get update 2.安装oracle-j ...
- Codeforces 897 B.Chtholly's request-思维题(处理前一半)
B. Chtholly's request time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- HDU 6271 Master of Connected Component(2017 CCPC 杭州 H题,树分块 + 并查集的撤销)
题目链接 2017 CCPC Hangzhou Problem H 思路:对树进行分块.把第一棵树分成$\sqrt{n}$块,第二棵树也分成$\sqrt{n}$块. 分块的时候满足每个块是一个 ...