In Java, this mode is closely related to the clone () method.
1. Class Diagram
2. Java implementation code package cn.edu. ynu. Sei. Prototype;
/**
* Original Model mode Client
*
* @ Author 88250
* @ Version 1.0.0, 2007-8-26
*/
Public class client
{
Private prototype;
/**
*
* @ Param example
*/
Public void operation (prototype example)
{
Prototype P = (prototype) example. Clone ();
}
}
Package cn.edu. ynu. Sei. Prototype;
/**
* For a specific prototype, use the {@ link # clone ()} Method for shortest cloning.
*
* @ Author 88250
* @ Version 1.0.0, 2007-8-26
*/
Public class concreteprototype implements prototype
{
/**
* Clone 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 the <code> JAVA. Lang. cloneable </cdoe> interface and identifies the interface that can be used
* {@ Link # clone ()} Method
* @ Author 88250
* @ Version 1.0.0, 2007-8-26
*/
Public interface prototype extends cloneable
{
Public object clone ();
}
3. Summary
In Java, the prototype mode is implemented by using the clone () method, but note that the clone () method is shallow clone. For details, refer to the javadoc comment of clone. It describes the conditions that the clone () method meets:
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 objectX, The expression:
x.clone() != x
Will be true, and that the expression:
x.clone().getClass() == x.getClass()
Will beTrue, But these are not absolute requirements. While it is typically the case that:
x.clone().equals(x)
Will beTrue, This is not an absolute requirement.
By convention, the returned object shoshould be obtained by callingSuper. Clone. If a class and all of its superclasses (Classes tObject) Obey this convention, it will be the case thatX. Clone (). getclass () = x. getclass ().
By convention, the object returned by this method shoshould 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 returnedSuper. CloneBefore 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 returnedSuper. CloneNeed to be modified.
The methodCloneFor ClassObjectPerforms a specific cloning operation. First, if the class of this object does not implement the interfaceCloneable, ThenClonenotsupportedexceptionIs thrown. Note that all arrays are considered to implement the interfaceCloneable. 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 classObjectDoes not itself implement the interfaceCloneable, So callingCloneMethod on an object whose class isObjectWill result in throwing an exception at run time.
Next, let's take a look at what is the difference between deep cloning and light cloning.
In short, deep cloning is to clone all objects cited by this object. For example, an object has a property of the class type. If you use a shortest clone, you just copy the instance reference of this property field to the property field of the cloned object, therefore, this field of the source object and the copy object references the same instance. Deep cloning also copies this field, instead of modifying the reference only. Here is a problem. What if this field also has an attribute of the class type?
To solve this problem, it depends on the needs of your application, because using deep copy in Java implements the serializable interface of a class, java's "Refrigerating" and "unfreezing" methods are used for deep cloning. Therefore, programmers can design deep clone hierarchies based on application needs.
Finally, let's look at the clone and "freeze" and "unfreeze" methods. Here is a sample code I wrote for reference.
4. References
Design Patterns and Java and Pattern