(二十三)原型模式详解(clone方法源码的简单剖析)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可。
原型模式算是JAVA中最简单的设计模式了,原因是因为它已经被提供了语言级的支持,但是如果提到它的实现原理,又是最复杂的一个设计模式。
下面我们先来看看这个又简单又复杂的设计模式的定义。
定义:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
定义比较简单,总结一下是通过实例指定种类,通过拷贝创建对象。
在JAVA语言中使用原型模式是非常简单的,这是因为Object类当中提供了一个本地方法clone,而JAVA中的任何类只要实现了Cloneable标识接口,就可以使用clone方法来进行对象的拷贝。
我们写一个简单的实例来测试一下,很简单。
package com.prototype;
public class Prototype implements Cloneable {
private int x;
private int y;
private int z;
public Prototype() {
this.x = 2;
this.y = 3;
this.z = 4;
}
public void change() {
this.x = 9;
this.y = 8;
this.z = 7;
}
public Prototype clone() {
Object object = null;
try {
object = super.clone();
} catch (CloneNotSupportedException exception) {
throw new RuntimeException(exception);
}
return (Prototype) object;
}
public String toString() {
return "[" + x + "," + y + "," + z + "]";
}
public static void main(String[] args) {
Prototype prototype1 = new Prototype();
prototype1.change();
System.out.println(prototype1);
Prototype prototype2 = prototype1.clone();
System.out.println(prototype2);
}
}
输入结果:
[9,8,7]
[9,8,7]
从输出结果可以看出来,clone方法将prototype1复制了一个,然后赋给了prototype2,这就像复制粘贴一样。值得注意的是,在使用Object.clone()方法去拷贝一个对象时,构造方法是不被执行的,否则prototype2实例中x,y,z的值应该为2,3,4才对,如果你觉得不够直观,可以在构造方法里写一个输出语句试试。
从原型模式的使用方式不难推断出,原型模式常使用于以下场景:
1、对象的创建非常复杂,可以使用原型模式快捷的创建对象。
2、在运行过程中不知道对象的具体类型,可使用原型模式创建一个相同类型的对象,或者在运行过程中动态的获取到一个对象的状态。
对于clone方法,它执行的是浅拷贝,也就是说如果是引用类型的属性,则它不会进行拷贝,而是只拷贝引用。
看下面这个简单的测试,就能看出来了。
package com.prototype;
class Field{
private int a;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
public class ShallowPrototype implements Cloneable {
private int x;
private int y;
private int z;
private Field field;
public ShallowPrototype() {
this.x = 2;
this.y = 3;
this.z = 4;
this.field = new Field();
this.field.setA(5);
}
public Field getField() {
return field;
}
public ShallowPrototype clone() {
Object object = null;
try {
object = super.clone();
} catch (CloneNotSupportedException exception) {
throw new RuntimeException(exception);
}
return (ShallowPrototype) object;
}
public String toString() {
return "[" + x + "," + y + "," + z + "," + field.getA() + "]";
}
public static void main(String[] args) {
ShallowPrototype prototype1 = new ShallowPrototype();
System.out.println(prototype1);
System.out.println(prototype1.getField());
ShallowPrototype prototype2 = prototype1.clone();
System.out.println(prototype2);
System.out.println(prototype2.getField());
}
}
输入结果:
[2,3,4,5]
com.prototype.Field@de6ced
[2,3,4,5]
com.prototype.Field@de6ced
可以看到我们对ShallowPrototype拷贝以后,得到一个实例prototype2,不过当我们输出field属性时,发现它们是引用的同一个对象。这当然不是我们期望得到的结果,这种情况下,我们如果修改prototype1中field的属性a的值,则prototype2中的也会跟着改变。
然而如果要实现深度拷贝,则需要将实现了Cloneable接口并重写了clone方法的类中,所有的引用类型也全部实现Cloneable接口并重写clone方法,而且需要将引用类型的属性全部拷贝一遍。
下面是一个简单的深度拷贝的例子,由上面的例子更改得到。
package com.prototype;
class Field implements Cloneable{
private int a;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
protected Field clone() {
Object object = null;
try {
object = super.clone();
} catch (CloneNotSupportedException exception) {
throw new RuntimeException(exception);
}
return (Field) object;
}
}
public class DeepPrototype implements Cloneable {
private int x;
private int y;
private int z;
private Field field;
public DeepPrototype() {
this.x = 2;
this.y = 3;
this.z = 4;
this.field = new Field();
this.field.setA(5);
}
public Field getField() {
return field;
}
protected DeepPrototype clone() {
Object object = null;
try {
object = super.clone();
((DeepPrototype)object).field = this.field.clone();
} catch (CloneNotSupportedException exception) {
throw new RuntimeException(exception);
}
return (DeepPrototype) object;
}
public String toString() {
return "[" + x + "," + y + "," + z + "," + field.getA() + "]";
}
public static void main(String[] args) {
DeepPrototype prototype1 = new DeepPrototype();
System.out.println(prototype1);
System.out.println(prototype1.getField());
DeepPrototype prototype2 = prototype1.clone();
System.out.println(prototype2);
System.out.println(prototype2.getField());
}
}
输出结果:
[2,3,4,5]
com.prototype.Field@a90653
[2,3,4,5]
com.prototype.Field@de6ced
下面我们来看下原型模式的主要优点:
1、由于clone方法是由虚拟机直接复制内存块执行,所以在速度上比使用new的方式创建对象要快。
2、可以基于原型,快速的创建一个对象,而无需知道创建的细节。
3、可以在运行时动态的获取对象的类型以及状态,从而创建一个对象。
然而原型模式的缺点也是相当明显的,主要的缺点就是实现深度拷贝比较困难,需要很多额外的代码量。
不过实际当中我们使用原型模式时,也可以写一个基类实现Cloneable接口重写clone方法,然后让需要具有拷贝功能的子类继承自该类,这是一种节省代码量的常用方式。像上面的例子一样,如果一个类继承自Prototype,则会自动具有拷贝功能。
下面我们来看看虚拟机中本地方法Object.clone()的源代码,如下。
JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle))
JVMWrapper("JVM_Clone");
Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
const KlassHandle klass (THREAD, obj->klass());
JvmtiVMObjectAllocEventCollector oam; #ifdef ASSERT
// Just checking that the cloneable flag is set correct
if (obj->is_javaArray()) {
guarantee(klass->is_cloneable(), "all arrays are cloneable");
} else {
guarantee(obj->is_instance(), "should be instanceOop");
bool cloneable = klass->is_subtype_of(SystemDictionary::Cloneable_klass());
guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
}
#endif // Check if class of obj supports the Cloneable interface.
// All arrays are considered to be cloneable (See JLS 20.1.5)
if (!klass->is_cloneable()) {//这里检查了是否实现了Cloneable接口,如果没实现,会抛出异常CloneNotSupportException。
ResourceMark rm(THREAD);
THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
} // Make shallow object copy
const int size = obj->size();//取对象大小
oop new_obj = NULL;
if (obj->is_javaArray()) {//如果是数组
const int length = ((arrayOop)obj())->length();//取长度
new_obj = CollectedHeap::array_allocate(klass, size, length, CHECK_NULL);//分配内存,写入元数据信息
} else {
new_obj = CollectedHeap::obj_allocate(klass, size, CHECK_NULL);//分配内存,写入元数据信息
}
// 4839641 (4840070): We must do an oop-atomic copy, because if another thread
// is modifying a reference field in the clonee, a non-oop-atomic copy might
// be suspended in the middle of copying the pointer and end up with parts
// of two different pointers in the field. Subsequent dereferences will crash.
// 4846409: an oop-copy of objects with long or double fields or arrays of same
// won't copy the longs/doubles atomically in 32-bit vm's, so we copy jlongs instead
// of oops. We know objects are aligned on a minimum of an jlong boundary.
// The same is true of StubRoutines::object_copy and the various oop_copy
// variants, and of the code generated by the inline_native_clone intrinsic.
assert(MinObjAlignmentInBytes >= BytesPerLong, "objects misaligned");
Copy::conjoint_jlongs_atomic((jlong*)obj(), (jlong*)new_obj,
(size_t)align_object_size(size) / HeapWordsPerLong);//这一步就是真正的COPY内存块了
// Clear the header
new_obj->init_mark();//初始化对象头,里面包含了Hashcode,GC信息,锁信息等,因为拷贝出的对象是一个全新的对象,所以这些信息需要初始化一下。 // Store check (mark entire object and let gc sort it out)
BarrierSet* bs = Universe::heap()->barrier_set();
assert(bs->has_write_region_opt(), "Barrier set does not have write_region");
bs->write_region(MemRegion((HeapWord*)new_obj, size));//write_region最终的实现在一个虚方法里,相当于JAVA的抽象方法,LZ没找到实现。暂不发表意见。 // Caution: this involves a java upcall, so the clone should be
// "gc-robust" by this stage.
if (klass->has_finalizer()) {//如果有finalize方法,则需要注册一下。
assert(obj->is_instance(), "should be instanceOop");
new_obj = instanceKlass::register_finalizer(instanceOop(new_obj), CHECK_NULL);
} return JNIHandles::make_local(env, oop(new_obj));//将内存对象转换成JAVA本地对象返回
JVM_END
虚拟机的源码比较复杂,而且完全没有相关文献和资料,所以LZ也只能简单的添加一些注释,特别是write_region这个方法,LZ没找到实现在哪里,LZ猜测这个方法的功能是设置对象的边界的,好让GC能够正确的回收内存,但由于没找到实现,所以不敢断言。
在上面的过程中调用了Copy对象的conjoint_jlongs_atomic方法,那个就是真正的复制实例数据的方法,LZ找到了这个方法的实现,给各位看一下。
void _Copy_conjoint_jlongs_atomic(jlong* from, jlong* to, size_t count) {
if (from > to) {
jlong *end = from + count;
while (from < end)
os::atomic_copy64(from++, to++);
}
else if (from < to) {
jlong *end = from;
from += count - ;
to += count - ;
while (from >= end)
os::atomic_copy64(from--, to--);
}
}
这是一个操作内存块的方法,其中atomic_copy64这个方法是用汇编语言写的,它确保了在64位的机子下也可以正确的进行内存块的拷贝操作。它的作用很简单,就是把from指针指向的内存的值赋给to指针指向的内存,也就是一个简单的拷贝操作。知道了atomic_copy64方法的作用,上面这个方法的逻辑就非常简单了。
由此可以看出,我们可以将clone方法想象成内存块的复制操作,它的速度比一般的创建对象操作要快。
原型模式的分析就到此结束了,对于虚拟机源码的研究,LZ一直在断断续续的继续着,等设计模式系列写完以后,LZ会写一些虚拟机以及虚拟机源码的相关内容,希望各位能继续支持吧。
感谢各位的收看。
(二十三)原型模式详解(clone方法源码的简单剖析)的更多相关文章
- 设计模式之 原型模式详解(clone方法源码的简单剖析)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 原型模式算是JAVA中最简单 ...
- 【详解】ThreadPoolExecutor源码阅读(二)
系列目录 [详解]ThreadPoolExecutor源码阅读(一) [详解]ThreadPoolExecutor源码阅读(二) [详解]ThreadPoolExecutor源码阅读(三) AQS在W ...
- 《Android NFC 开发实战详解 》简介+源码+样章+勘误ING
<Android NFC 开发实战详解>简介+源码+样章+勘误ING SkySeraph Mar. 14th 2014 Email:skyseraph00@163.com 更多精彩请直接 ...
- 设计模式(五)——原型模式(加Spring框架源码分析)
原型模式 1 克隆羊问题 现在有一只羊 tom,姓名为: tom, 年龄为:1,颜色为:白色,请编写程序创建和 tom 羊 属性完全相同的 10 只羊. 2 传统方式解决克隆羊问题 1) 思路分析(图 ...
- Android中Canvas绘图基础详解(附源码下载) (转)
Android中Canvas绘图基础详解(附源码下载) 原文链接 http://blog.csdn.net/iispring/article/details/49770651 AndroidCa ...
- 【详解】ThreadPoolExecutor源码阅读(三)
系列目录 [详解]ThreadPoolExecutor源码阅读(一) [详解]ThreadPoolExecutor源码阅读(二) [详解]ThreadPoolExecutor源码阅读(三) 线程数量的 ...
- 【详解】ThreadPoolExecutor源码阅读(一)
系列目录 [详解]ThreadPoolExecutor源码阅读(一) [详解]ThreadPoolExecutor源码阅读(二) [详解]ThreadPoolExecutor源码阅读(三) 工作原理简 ...
- Android事件传递机制详解及最新源码分析——ViewGroup篇
版权声明:本文出自汪磊的博客,转载请务必注明出处. 在上一篇<Android事件传递机制详解及最新源码分析--View篇>中,详细讲解了View事件的传递机制,没掌握或者掌握不扎实的小伙伴 ...
- SpringBoot Profile使用详解及配置源码解析
在实践的过程中我们经常会遇到不同的环境需要不同配置文件的情况,如果每换一个环境重新修改配置文件或重新打包一次会比较麻烦,Spring Boot为此提供了Profile配置来解决此问题. Profile ...
随机推荐
- 使用JUnit4测试Spring
测试DAO import static org.junit.Assert.*; import org.junit.Before; import org.junit.Ignore; import org ...
- flume架构初接触
flume优点 1.存储数据到任何中央数据库 2.进入数据速率大于写出速率,可以起到缓存作用,保证流的平稳 3.提供文本式路由 4.支持事务 5.可靠.容错.可伸缩.可定制.可管理 put的缺点 1. ...
- Linux简介及常用命令使用2--linux常用命令:查看 删除 编辑 创建等
cd . // 进入当前目录 cd ~ //进入根目录 pwd //当前路径 echo "my name is makaidong">makai ...
- linux 拨号+squid监控脚本
客户端 #!/bin/bash #get_memory-info a=`free -m|grep Mem|awk '{print$2}'` #total-memory b=`free -m|grep ...
- DDNS
一.DDNS简介 DNS,域名系统,是因特网的一项服务,它作为将域名和IP地址相互映射的一个分布式数据库,能够使人们更方便的访问互联网. DDNS,动态域名系统,是域名系统(DNS)中的一种自动更新名 ...
- day 2 常用快捷键
tab命令或路径补全**,linux里最有用的快捷键,如果tab不到路径或命令,就代表没有这个路径或者命令,还有可能是权限不对. ctrl+c :终止当前任务命令或程序. ctrl+d 退出当前用户环 ...
- 002.ICMP--拼接ICMP包,实现简单Ping程序(原始套接字)
一.大致流程: 将ICMP头和时间数据设置好后,通过创建好的原始套接字socket发出去.目的主机计算效验和后会将数据原样返回,用当前时间和返回的数据结算时间差,计算出rtt. 二.数据结构: ICM ...
- vmware 10 e1000e e1000e_cyclecounter_read 挂机解法
http://ehc.ac/p/e1000/mailman/message/34100875/In the e1000e_cyclecounter_read function, if incvalue ...
- .net 操作sftp服务器
因为项目的需要,整理了一段C#操作sftp的方法. 依赖的第三方类库名称为:SharpSSH 1.1.1.13. 代码如下: 1: using System; 2: using System.Coll ...
- 网络之OSI&&TCP/IP比较
共同点: 1.OSI和TCP/IP都采用了层次结构的概念 2.都能够提供面向链接(TCP)和无链接(UDP)两种通信服务机制 不同点: 1.前者7层,后者两层 2.对可靠性要求不同,TCP/IP要求高 ...