In Java, this pattern is primarily related to the Clone () method.
1. Class Diagram
2. Java Implementation CodePackage cn.edu.ynu.sei.prototype;
/**
* Original Model Mode client
*
* @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;
/**
* Specific prototype class, using the {@link #clone ()} Method for shallow cloning
*
* @author 88250
* @version 1.0.0, 2007-8-26
*/
public class Concreteprototype implements Prototype
{
/**
* Cloning method
*
* @return
*/
@Override
Public Object Clone ()
{
Try
{
Return super. Clone ();
}
catch (Clonenotsupportedexception cnse)
{
return null;
}
}
}
Package cn.edu.ynu.sei.prototype;
/**
* Prototype Interface <br>
* Inherits from <code>java.lang.Cloneable</cdoe> interface, identifies this interface can be used
* {@link #clone ()} method
* @author 88250
* @version 1.0.0, 2007-8-26
*/
Public interface Prototype extends cloneable
{
Public Object clone ();
}
3. SummaryIn Java, the prototype pattern is primarily implemented using the Clone () method, but it is important to note that the Clone () method is a shallow clone (shallow clone). The details section can refer to the Javadoc annotation for clone (). It explains the conditions that the clone () method satisfies:
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 was that, for any object x, the expression:
X.clone ()!= x
'll is true, and that the expression:
X.clone (). GetClass () = X.getclass ()
'll is true, but these are not absolute requirements. While it's typically the case:
X.clone (). Equals (x)
Would be true and this isn't an absolute requirement.
By convention, the returned object should is obtained by calling Super.clone. If a class and all of its superclasses (except Object) obey this convention, it'll be the case that X.clone (). GetClass () = = X.getclass ().
By convention, the object returned by this method should to independent of this object (which is being cloned). To achieve this independence, it may is necessary to modify one or more fields of the object returned by Super.clone befor e returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned a nd replacing the references to this objects with references to the copies. If A class contains only primitive fields or references to immutable objects, then it are usually the case that no fields I n the object returned by Super.clone need to be modified.
The method clones for class Object performs a specific cloning operation. The class of this object does not implement the interface cloneable, then a clonenotsupportedexception is thrown . Note this all arrays are considered to implement the interface cloneable. Otherwise, this method creates a new instance of the class of that object and initializes all it 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 is performs a "shallow copy" of this object and 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 I s Object would result in throwing a exception at run time.
Below, let's look at what is the difference between a deep clone and a shallow clone.
Simply put, deep cloning is the cloning of all objects that are cited by the object. For example, an object has a property that is a class type, and if a shallow clone is used at this time, only the instance reference of the property field is copied to the property field of the cloned object, so the source object and the copied object are referenced by the same instance. A deep clone is also a copy of this field, not just a reference change. There is a problem here, if the field also has a property is a class type how to do it.
To solve this problem depends on the needs of your application, because the use of deep copy in Java is the implementation of a class serializable interface, using the Java "cold" and "thaw" methods for deep cloning, so, Programmers can fully design deep cloning level depth according to the needs of the application.
Finally, in a wordy, about cloning and "refrigeration" and "Thaw" methods, here's a sample code I wrote can refer to:-)
4. References "Design Patterns", "Java and schema"