在Java裡,這個模式主要與clone()方法密切相關。
1. 類圖
2. Java實現代碼 package cn.edu.ynu.sei.prototype;
/**
* 原始模型模式用戶端
*
* @author 88250
* @version 1.0.0, 2007-8-26
*/
public class Client
{
private Prototype prototype;
/**
*
* @param example
*/
public void operation(Prototype example)
{
Prototype p = (Prototype) example.clone();
}
}
package cn.edu.ynu.sei.prototype;
/**
* 具體原型類,使用{ @link #clone()}方法進行淺複製
*
* @author 88250
* @version 1.0.0, 2007-8-26
*/
public class ConcretePrototype implements Prototype
{
/**
* 複製方法
*
* @return
*/
@Override
public Object clone()
{
try
{
return super .clone();
}
catch (CloneNotSupportedException cnse)
{
return null ;
}
}
}
package cn.edu.ynu.sei.prototype;
/**
* 原型介面<br>
* 繼承於<code>java.lang.Cloneable</cdoe>介面,標識本介面可以使用
* { @link #clone()}方法
* @author 88250
* @version 1.0.0, 2007-8-26
*/
public interface Prototype extends Cloneable
{
public Object clone();
}
3. 總結 在Java裡,原型模式主要就是使用clone()方法去實現,但是要注意的是clone()方法是淺複製(shallow clone)的。細節部分可以參考clone()的JavaDoc注釋。裡面說明了clone()方法滿足的條件:
Object java.lang.Object.clone() throws CloneNotSupportedException
Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:
x.clone() != x
will be true, and that the expression:
x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that:
x.clone().equals(x)
will be true, this is not an absolute requirement.
By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().
By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.
The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.
The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time.
下面,我們來看看什麼是深複製和淺複製的不同。
簡單地說,深複製就是對該對象所引由的所有對象都進行複製。比如,一個對象有一個屬性為類類型,這個時候如果使用的是淺複製,那麼就只是將這個屬性欄位的執行個體引用複製給了複製對象的屬性欄位,所以來源物件和拷貝對象的這個欄位都是引用了同一個執行個體。而深複製則是對這個欄位也進行複製,而不是只更改引用。這裡就有一個問題,如果這個欄位也存在一個屬性是類類型怎麼辦呢。
要解決這個問題就看你的應用程式的需要了,因為在Java裡使用深拷貝都是將一個類實現Serializable介面,使用Java的“冷藏”和“解凍”方法去進行深複製,所以,程式員完全可以根據應用的需要而設計深複製層次深度。
最後,在羅嗦一下,關於複製與“冷藏”和“解凍”方法,這裡有個我寫的範例程式碼可以參考 :-)
4. 參考文獻 《Design Patterns》、《Java與模式》