原型模式Prototype

原型模式

http://www.cnblogs.com/zhili/p/PrototypePattern.html

 

ICloneable接口

https://msdn.microsoft.com/en-us/library/system.icloneable(v=vs.110).aspx

Supports cloning, which creates a new instance of a class with the same value as an existing instance.

Remarks

The ICloneable interface enables you to provide a customized implementation that creates a copy of an existing object.

The ICloneable interface contains one member, the Clone method, which is intended to provide cloning support beyond that supplied by Object.MemberwiseClone.

For more information about cloning, deep versus shallow copies, and examples, see the Object.MemberwiseClone method.

Notes to Implementers

The ICloneable interface simply requires that your implementation of the Clone method return a copy of the current object instance.

It does not specify whether the cloning operation performs a deep copy, a shallow copy, or something in between.

Nor does it require all property values of the original instance to be copied to the new instance.

For example, the NumberFormatInfo.Clone method performs a shallow copy of all properties except the NumberFormatInfo.IsReadOnly property;it always sets this property value to false in the cloned object.

Because callers of Clone cannot depend on the method performing a predictable cloning operation, we recommend that ICloneable not be implemented in public APIs.

 

 

Object.MemberwiseClone

Creates a shallow copy of the current Object.

Remarks

The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.

For example, consider an object called X that references objects A and B. Object B, in turn, references object C. A shallow copy of X creates new object X2 that also references objects A and B. In contrast, a deep copy of X creates a new object X2 that references the new objects A2 and B2, which are copies of A and B. B2, in turn, references the new object C2, which is a copy of C. The example illustrates the difference between a shallow and a deep copy operation.

There are numerous ways to implement a deep copy operation if the shallow copy operation performed by the MemberwiseClone method does not meet your needs. These include the following:

  • Call a class constructor of the object to be copied to create a second object with property values taken from the first object. This assumes that the values of an object are entirely defined by its class constructor.

  • Call the MemberwiseClone method to create a shallow copy of an object, and then assign new objects whose values are the same as the original object to any properties or fields whose values are reference types. The DeepCopy method in the example illustrates this approach.

  • Serialize the object to be deep copied, and then restore the serialized data to a different object variable.

  • Use reflection with recursion to perform the deep copy operation.

 

深拷贝和浅拷贝

public class IdInfo
{public int IdNumber;public IdInfo(int IdNumber){this.IdNumber = IdNumber;}
}public class Person 
{public int Age;public string Name;public IdInfo IdInfo;public Person ShallowCopy(){return (Person) this.MemberwiseClone();}public Person DeepCopy(){Person other = (Person) this.MemberwiseClone();other.IdInfo = new IdInfo(IdInfo.IdNumber);other.Name = String.Copy(Name);return other;}
}

 

ICloneable<T>

http://blog.csdn.net/yapingxin/article/details/12754015

 

转载于:https://www.cnblogs.com/chucklu/p/6279168.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/371677.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

I / O神秘化

由于对高度可扩展的服务器设计的所有炒作以及对nodejs的狂热&#xff0c;我一直想重点研究IO设计模式&#xff0c;直到现在为止都没有足够的时间进行投资。 现在已经做了一些研究&#xff0c;我认为最好记下我遇到的东西&#xff0c;作为对我以及可能遇到这篇文章的任何人的未来…

java三大特性 继承_java基础(二)-----java的三大特性之继承

在《Think in java》中有这样一句话&#xff1a;复用代码是Java众多引人注目的功能之一。但要想成为极具革命性的语言&#xff0c;仅仅能够复制代码并对加以改变是不够的&#xff0c;它还必须能够做更多的事情。在这句话中最引人注目的是“复用代码”,尽可能的复用代码使我们程…

Maven本地仓库配置

本地仓库是远程仓库的一个缓冲和子集&#xff0c;当你构建Maven项目的时候&#xff0c;首先会从本地仓库查找资源&#xff0c;如果没有&#xff0c;那么Maven会从远程仓库下载到你本地仓库。这样在你下次使用的时候就不需要从远程下载了。如果你所需要的jar包版本在本地仓库没有…

配置CDI对话的超时

在开发JSF应用程序时&#xff0c;CDI对话范围是一个很好的功能。 假设您有大型数据表&#xff0c;需要花费很长时间才能加载。 由于高内存消耗&#xff0c;您通常不希望将加载的数据放在会话范围的Bean中。 而且&#xff0c;您不能将加载的数据放入视图范围的Bean中&#xff0c…

记录下log4j的两种配置方式

XML文件配置 <?xml version"1.0" encoding"UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4jhttp://jakarta.apache.org/log4j/><!-- 输出到控制台 --><appender na…

java字符串与数组比较大小_java-如何将存储在数组中的字符串与简单字符串进行比较?...

我想比较数组中字符串形式的学生人数与人数n这是字符串。remarque&#xff1a;班级形成&#xff1a;私有字符串代码&#xff1b;私有字符串名称&#xff1b;private int nbsi 0;私人学生[]标签新学生[200]&#xff1b;班级学生&#xff1a;私有字符串号&#xff1b;私有字符串…

delphi用TAdoStoredProc调用存储过程,兼容sql2005、2008、2014的远程事务问题

delphi7写的程序&#xff0c;在sql2000里没问题&#xff0c;调用sql2008、2014里的存储过程时&#xff0c;如果存储过程里操作了大量数据&#xff0c;很容易会莫名其妙的自己撤销掉&#xff0c;但是程序还识别不到&#xff0c;认为还在正常执行。今天尝试换了个控件&#xff1a…

使用NoSQL实现实体服务–第3部分:CouchDB

在本系列的第2部分中 &#xff0c;我使用SOA的“合同优先”技术创建和部署了产品实体服务&#xff0c;现在&#xff0c;我将致力于服务实现的NoSQL数据库方面。 正如我在第1部分中已经提到的那样&#xff0c;我已经选择CouchDB作为我的NoSQL数据库&#xff0c;选择Ektorp库作为…

8、SpringCloud高频面试题-版本1

1、SpringCloud组件有哪些 SpringCloud 是一系列框架的有序集合。它利用 SpringBoot 的开发便利性巧妙地简化了分布式系统基础设施的开发&#xff0c;如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等&#xff0c;都可以用 SpringBoot 的开发风格做到一键启…

java对象不会被改变_Java 并发编程(二)对象的不变性和安全的公布对象

二、安全公布到眼下为止&#xff0c;我们重点讨论的是怎样确保对象不被公布&#xff0c;比如让对象封闭在线程或还有一个对象的内部。当然&#xff0c;在某些情况下我们希望在多个线程间共享对象&#xff0c;此时必须确保安全地进行共享。然而&#xff0c;假设仅仅是像以下程序…

nginx 上php不可写解决方法

在php.ini中设置的session.save_path会被php-fpm.conf中覆盖 打开php-fpm.conf文件找到php_value[session.save_apth] 这里的/var/lib/php/session 为实际的session保存目录&#xff0c;设置为777,必须让其他用户有rw权限,因为php在Linux里面以其他用户身份运行&#xff08;匿名…

JavaOne 2012:Java策略主题演讲和IBM主题演讲

与 JavaOne 2010 相似&#xff0c;我对JavaOne 2012的开始也很艰难。由于“计算机和打印机技术上的困难”&#xff0c;办理登机手续的人花了70分钟为我提供JavaOne徽章。 尽管我不是世界上最有耐心的人&#xff0c;但比等待更令人失望的是&#xff0c;我错过了参加“社区会议&a…

java citymap_Java实现Map集合二级联动

Map集合可以保存键值映射关系&#xff0c;这非常适合本实例所需要的数据结构&#xff0c;所有省份信息可以保存为Map集合的键&#xff0c;而每个键可以保存对应的城市信息&#xff0c;本实例就是利用Map集合实现了省市级联选择框&#xff0c;当选择省份信息时&#xff0c;将改变…

【NIO】之IO和NIO的区别

在Java1.4之前的版本&#xff0c;Java对I/O的支持并不完善&#xff0c;开发人员在开发高性能I/O程序的时候&#xff0c;会面临以下几个问题&#xff1a; 1、没有数据缓存区&#xff0c;I/O性能存在问题 2、没有C/C通道的概念&#xff0c;输入和输出流是相互独立的不能复用 3、同…

Mono环境下访问SSL

由于MONO没有CA证书&#xff0c;所以访问SSL链接&#xff08;HTTPS&#xff09;就会出错&#xff0c;这时候只要强制访问就可以。 using System.Net.Security;using System.Security.Authentication;using System.Security.Cryptography.X509Certificates; private static bool…

JavaOne 2012:使用HTML5和Java构建移动应用程序

我返回了Parc 55 &#xff08;任务会议室&#xff09;&#xff0c;观看Max Katz的&#xff08; Exadel开发人员关系&#xff09;“用HTML5和Java构建移动应用程序” Bird-of-Feather&#xff08;BoF&#xff09;演示。 具体来说&#xff0c;Katz在Tiggzi &#xff08;基于云的应…

HDU 2602.Bone Collector-动态规划0-1背包

Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 85530 Accepted Submission(s): 35381 Problem DescriptionMany years ago , in Teddy’s hometown there was a man who was called “Bone Col…

java线程实现排序_【多线程实现快速排序】

快速排序算法实现文件QuickSort.javapackage quick.sort;import java.util.concurrent.Callable;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class QuickSort implements Callable{private int[] array;private final in…

使用Gitolite搭建Gitserver

Gitolite是一款Perl语言开发的Git服务管理工具。通过公钥对用户进行认证。并可以通过配置文件对些操作进行基于分支和路径的精细控制。Gitolite採用的是SSH协议而且使用SSH公钥认证。因此不管是管理员还是普通用户。都须要对SSH有所了解。Gitolite的官网是&#xff1a;https://…

java任务分支和合并_合并/分支战略

我会给出与Adarsh Shah相同的建议&#xff0c;因为在大多数情况下&#xff0c;2个分支(MAIN&#xff0c;RELEASE)就足够了&#xff0c;并且使用feature branches用于你不想立即提交到MAIN的东西&#xff0c;因为它需要一段时间才能完全准备好测试 . 通过RELEASE&#xff0c;我指…