The Prototype_Pattern is the application of "template".
package prototype_pattern;
public abstract class Prototype implements Cloneable {
protected String name;
protected int age;
protected Body body;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Body getBody() {
return body;
}
public void setBody(Body body) {
this.body = body;
}
@Override
protected Object clone() throws CloneNotSupportedException {
// The method of clone just copy the basic data type.
Prototype person = (Prototype) super.clone();
return person;
}
}
package prototype_pattern;
public class ConcretePrototype extends Prototype {
@Override
protected Object clone() {//This method process the possible exception.
Prototype person = null;
try {
// The method of clone just copy the basic data type.
person = (ConcretePrototype) super.clone();
person.name = new String(super.name);// The current is deep copy,without this sentence is shallow copy.
person.body = (Body) body.clone();// The reference type need deep copy.
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return person;
}
}
package prototype_pattern;
public class Body implements Cloneable{
private double weight;
private Physique physique;
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public Physique getPhysique() {
return physique;
}
public void setPhysique(Physique pyysique) {
this.physique = pyysique;
}
@Override
protected Object clone() throws CloneNotSupportedException{
Body body =(Body) super.clone();
body.physique = (Physique)physique.clone();//deep copy
return body;
}
}
package prototype_pattern;
public class Physique implements Cloneable{
private boolean health;
public boolean isHealth() {
return health;
}
public void setHealth(boolean health) {
this.health = health;
}
@Override
protected Object clone() throws CloneNotSupportedException{
return super.clone();
}
}
package prototype_pattern;
public class Main {
public static void main(String args[]){
Prototype person = new ConcretePrototype();
person.setAge(11);
person.setName("sam");
Physique physique = new Physique();
physique.setHealth(true);
Body body = new Body();
body.setPhysique(physique);
body.setWeight(65.5);
person.setBody(body);
Prototype newPerson = null;
try {
newPerson = (ConcretePrototype)person.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(person.age == newPerson.age);
System.out.println(person.name == newPerson.name);
System.out.println(person.body == newPerson.body);
System.out.println(person.body.getPhysique() == newPerson.body.getPhysique());
}
}
The illustration of the "clone" method:
package prototype_pattern;
public class Prototype {
private int age;
public static void main(String args[]) {
Prototype p = new Prototype();
p.age = 1;
try {
Prototype q = (Prototype) p.clone();
System.out.println(q.age);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
If we don't implement the interface of clonable,the running results will be listed as follows:
java.lang.CloneNotSupportedException: prototype_pattern.Prototype
at java.lang.Object.clone(Native Method)
at prototype_pattern.Prototype.main(Prototype.java:36)
The official website explains that:
Object.class
protected native Object clone() throws CloneNotSupportedException;
/**
* Returns a string representation of the object. In general, the
* {@code toString} method returns a string that
* "textually represents" this object. The result should
* be a concise but informative representation that is easy for a
* person to read.
* It is recommended that all subclasses override this method.
* <p>
* The {@code toString} method for class {@code Object}
* returns a string consisting of the name of the class of which the
* object is an instance, the at-sign character `{@code @}', and
* the unsigned hexadecimal representation of the hash code of the
* object. In other words, this method returns a string equal to the
* value of:
* <blockquote>
* <pre>
* getClass().getName() + '@' + Integer.toHexString(hashCode())
* </pre></blockquote>
*
* @return a string representation of the object.
*/
Cloneable.class
/*
* Copyright (c) 1995, 2004, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang;
/**
* A class implements the <code>Cloneable</code> interface to
* indicate to the {@link java.lang.Object#clone()} method that it
* is legal for that method to make a
* field-for-field copy of instances of that class.
* <p>
* Invoking Object's clone method on an instance that does not implement the
* <code>Cloneable</code> interface results in the exception
* <code>CloneNotSupportedException</code> being thrown.
* <p>
* By convention, classes that implement this interface should override
* <tt>Object.clone</tt> (which is protected) with a public method.
* See {@link java.lang.Object#clone()} for details on overriding this
* method.
* <p>
* Note that this interface does <i>not</i> contain the <tt>clone</tt> method.
* Therefore, it is not possible to clone an object merely by virtue of the
* fact that it implements this interface. Even if the clone method is invoked
* reflectively, there is no guarantee that it will succeed.
*
* @author unascribed
* @see java.lang.CloneNotSupportedException
* @see java.lang.Object#clone()
* @since JDK1.0
*/
public interface Cloneable {
}
This is a general introduction to the 23 design patterns:
https://blog.csdn.net/GZHarryAnonymous/article/details/81567214