设计模式五: 原型模式(Prototype)
简介
原型模式是属于创建型模式的一种,是通过拷贝原型对象来创建新的对象.
万能的Java超类Object提供了clone()方法来实现对象的拷贝.
可以在以下场景中使用原型模式:
- 构造函数创建对象成本太大(性能或安全成本)
- 要保存对象的状态, 且状态变化较小, 不会过多占用内存时(状态变化较大的使用状态模式会更合适)
意图
使用原型实例指定要创建的对象类型,并通过拷贝这个原型来创建新对象。
类图

实现
一. 浅拷贝和深拷贝的概念
Object.clone()方法实现的是对象的浅拷贝, 所谓浅拷贝就是当对象中有复杂引用类型的域变量时, 只拷贝该域变量的引用而不是内容, 当有任一方法修改域变量的状态时会同时影响原型对象及拷贝对象, 实际上他们共用了同一个堆内存. 深拷贝创建的对象即是对原对象的完全拷贝,对任一对象的操作不会影响其他对象的状态.
java中提供了Cloneable接口, 约定实现接口Cloneable且重写Object.clone()方法的类可以用来拷贝自身. Cloneable是一个标记接口, 其中没有定义任何方法.
二. 下面的代码演示了使用clone()方法实现的深拷贝,这种方式更适合用于比较简单的对象,否则clone()方法的实现可能会变得异常复杂.
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class CarProperty implements Cloneable {
private String power;
private double maxSpeed;
private double oilPerKm;
public Object clone(){
Object obj = null;
try {
obj = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return obj;
}
}
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Car implements Cloneable {
private String brand;
private double price;
private CarProperty carProperty;
/**
* 深拷贝在此实现,对于复杂的应用类型, 这里的代码可能会相当复杂,如果类有修改(新增成员变量等),这里也需要相应修改
* @return
*/
public Object clone(){
Object car = null;
try {
car = super.clone();
CarProperty carPropertyClone = (CarProperty)this.getCarProperty().clone();
((Car)car).setCarProperty(carPropertyClone);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return car;
}
public static void main(String[] args) {
CarProperty carProperty = new CarProperty("8匹",250,30);
Car car= new Car("BMW",200,carProperty);
Car copy = (Car) car.clone();
System.out.println("copy最大速度为: "+copy.getCarProperty().getMaxSpeed());
System.out.println("原型最大速度为: "+car.getCarProperty().getMaxSpeed());
car.getCarProperty().setMaxSpeed(360);
System.out.println("copy最大速度为: "+copy.getCarProperty().getMaxSpeed());
System.out.println("原型最大速度为: "+car.getCarProperty().getMaxSpeed());
}
}
三. 深拷贝的其他实现方式: 除了上面的方法,还可以使用反射机制创建对象的深拷贝, 另外一种更简单的方式是使用序列化;
下面的代码使用序列化方式实现对象的深拷贝,需实现Serializable接口.
import java.io.*;
public class DeepCloneBase implements Serializable {
public Object deepClone() {
ByteArrayOutputStream byteArrayOutputStream = null;
ObjectOutputStream objectOutputStream = null;
ByteArrayInputStream byteArrayInputStream = null;
ObjectInputStream objectInputStream = null;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(this);
byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
objectInputStream = new ObjectInputStream(byteArrayInputStream);
return objectInputStream.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
byteArrayOutputStream.close();
objectOutputStream.close();
byteArrayInputStream.close();
objectInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serializable;
@Data
@AllArgsConstructor
public class MyCar extends DeepCloneBase {
private String brand;
private double price;
private CarProperty carProperty;
public static void main(String[] args) throws Exception{
// 注意CarProperty也需要实现Serializable接口,代码不再单独列出
CarProperty carProperty = new CarProperty("8匹",250,30);
MyCar car= new MyCar("BMW",200,carProperty);
MyCar copy = (MyCar)car.deepClone();
if (copy!=null){
System.out.println("copy最大速度为: "+copy.getCarProperty().getMaxSpeed());
System.out.println("原型最大速度为: "+car.getCarProperty().getMaxSpeed());
car.getCarProperty().setMaxSpeed(360);
System.out.println("copy最大速度为: "+copy.getCarProperty().getMaxSpeed());
System.out.println("原型最大速度为: "+car.getCarProperty().getMaxSpeed());
}else{
System.out.println("对象没拷贝成功....");
}
}
}
总结
优点: 1. 如果对象创建比较复杂, 可以简化创建过程, 提高效率;2. 可以保留对象状态;
缺点: 对于clone()方式,如果类有修改则需要修改clone()的实现,不符合开闭原则; 复杂对象的clone逻辑可能较复杂;
JDK
设计模式五: 原型模式(Prototype)的更多相关文章
- 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)
原文:乐在其中设计模式(C#) - 原型模式(Prototype Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 原型模式(Prototype Pattern) 作者:weba ...
- 二十四种设计模式:原型模式(Prototype Pattern)
原型模式(Prototype Pattern) 介绍用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象.示例有一个Message实体类,现在要克隆它. MessageModel usin ...
- [设计模式] 4 原型模式 prototype
设计模式:可复用面向对象软件的基础>(DP)本文介绍原型模式和模板方法模式的实现.首先介绍原型模式,然后引出模板方法模式. DP书上的定义为:用原型实例指定创建对象的种类,并且通过拷贝这些原型创 ...
- 设计模式 笔记 原型模式 prototype
//---------------------------15/04/07---------------------------- //prototype 原型模式--对象创建型模式 /* 1:意图: ...
- python 设计模式之原型模式 Prototype Pattern
#引入 例子1: 孙悟空拔下一嘬猴毛,轻轻一吹就会变出好多的孙悟空来. 例子2:寄个快递下面是一个邮寄快递的场景:“给我寄个快递.”顾客说.“寄往什么地方?寄给……?”你问.“和上次差不多一样,只是邮 ...
- 【UE4 设计模式】原型模式 Prototype Pattern
概述 描述 使用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.如孙悟空猴毛分身.鸣人影之分身.剑光分化.无限剑制 原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象, ...
- 【设计模式】—— 原型模式Prototype
前言:[模式总览]——————————by xingoo 模式意图 由于有些时候,需要在运行时指定对象时哪个类的实例,此时用工厂模式就有些力不从心了.通过原型模式就可以通过拷贝函数clone一个原有的 ...
- 创建型设计模式之原型模式(Prototype)
结构 意图 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 适用性 当要实例化的类是在运行时刻指定时,例如,通过动态装载:或者 为了避免创建一个与产品类层次平行的工厂类层次时:或 ...
- 设计模式之原型模式(prototype)
原理:拷贝自身对象实际上就是调用的拷贝构造函数,注意事项是这里的拷贝是深拷贝,即需要拷贝指针所指的内容 #include <stdio.h> #include <memory> ...
随机推荐
- 利用BeEF REST API自动化控制僵尸主机
本文首发Freebuf,属于原创奖励计划,未经许可禁止转载. http://www.freebuf.com/articles/network/137662.html 一. 前言 关于BeEF,不再多介 ...
- 使用PowerDesigner 15对现有数据库进行生成图表结构
PowerDesigner的安装和基本使用,我就不阐述了.大家可以到这里看看:http://www.blogjava.net/wangdetian168/archive/2011/04/07/Powe ...
- Python Revisited Day 06 (面向对象程序设计)
目录 6.1 面向对象方法 duck typing 访问限制 __ 6.2 自定义类 6.2.1 属性与方法 预定义的特殊方法 __...__ 一般的方法名起始和结尾不应该使用俩个下划线,除非是预定义 ...
- VS2010安装MVC4记录
想学MVC4,但自己电脑上是VS2010,所以在网上查询怎样安装MVC4,总结下给大家. VS2010默认安装的是MVC2,安装MVC4必须先装 SP1,然后安装MVC4. VS2010 SP1 (1 ...
- Kuro and Walking Route CodeForces - 979C (树上DFS)
Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11to nn, and n−1n ...
- windows服务器nginx+php启动开源ecshop
1,下载php,nginx,ECShop源码 2,解压php到指定目录(如:C:\php-7.2.6) 2.1,找到指定目录下文件php.ini-development复制重命名为php.ini 2. ...
- golang介绍
一.golang介绍 golang是Google开发的一种 静态强类型.编译型,并发型,并具有垃圾回收功能的编程语言. 二.语言特性 1..自动垃圾回收 2.支持函数多返回值 3.并发强 三.gol ...
- 4.ansible的delegate_to
完成发布流程如下 first 修改nginx 配置文件下线 web1-2 使用 delegate_to 将默认hosts指定为 nginx主机 使用remote_user 将用户 锁定为 root s ...
- Java执行jar总结
1.命令集 1)nohup 用途:不挂断地运行命令. 语法:nohup Command [ Arg … ] [ & ] 无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 ...
- 【XSY3156】简单计数II 容斥 DP
题目大意 定义一个序列的权值为:把所有相邻的相同的数合并为一个集合后,所有集合的大小的乘积. 特别的,第一个数和最后一个数是相邻的. 现在你有 \(n\) 种数,第 \(i\) 种有 \(c_i\) ...