原型模式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.pswp.cn/news/371677.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/371677.shtml
英文地址,請注明出處:http://en.pswp.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;我指…