Design pattern of java learning - prototype pattern

Prototype mode


Use an already created instance as a prototype, and create a new object that is the same as the prototype object by copying the prototype object.


The prototype pattern includes the following roles:

  • Abstract prototype class: Specifies the clone() method that the concrete prototype object must implement.
  • Concrete prototype class: implements the clone() method of the Abstract prototype class, which is an object that can be copied.
  • Access class: use the clone() method in the concrete prototype class to copy the new object.

The interface class diagram is as follows:


The clone of prototype pattern can be divided into shallow clone and deep clone.

  • Shallow cloning: create a new object. The properties of the new object are exactly the same as the original object. For non basic type properties, it still points to the memory address of the object pointed to by the original property. (when the basic data type is set for the original object and the cloned object at the same time, different objects can get different attribute values. When the non basic data type is set, because the memory address is pointed to, the original object and the cloned object are equivalent to sharing one attribute.)
  • Deep cloning: when a new object is created, other objects referenced in the attribute will also be cloned and no longer point to the original object address. (the non basic data type of the cloned object is equivalent to the new memory address, so whether it is the basic data type or the reference data type, the original object and the cloned object get different properties.)

The clone() method is provided in the Object class in Java to implement shallow cloning. The clonable interface is the Abstract prototype class in the above class diagram, and the sub implementation class that implements the clonable interface is the concrete prototype class. The code is as follows:
Realizetype (concrete prototype class):

public class Realizetype implements Cloneable {
public Realizetype() {
System.out.println("The specific prototype object is created!");
protected Realizetype clone() throws CloneNotSupportedException {
System.out.println("Specific prototype copied successfully!");
return (Realizetype) super.clone();

PrototypeTest (test access class):

public class PrototypeTest {
public static void main(String[] args) throws CloneNotSupportedException
Realizetype r1 = new Realizetype();
Realizetype r2 = r1.clone();
System.out.println("object r1 and r2 Is it the same object?" + (r1 == r2));


Generating the "three good students" certificate with prototype mode
The "three good students" certificates of the same school are the same except for the names of the winners. You can use the prototype mode to copy multiple "three good students" certificates, and then modify the names on the certificates.

public class Citation implements Cloneable {
private String name;
public void setName(String name) { = name;
public String getName() {
return (;
public void show() {
System.out.println(name + "Classmate: excellent performance in the first semester of the 2020 academic year, rated as "three good studies"
I was born. This is the special case!");
public Citation clone() throws CloneNotSupportedException {
return (Citation) super.clone();
//Test access class
public class CitationTest {
public static void main(String[] args) throws
CloneNotSupportedException {
Citation c1 = new Citation();
c1.setName("Zhang San");
//Copy the certificate
Citation c2 = c1.clone();
//Change the name of the award to Li Si
c2.setName("Li Si");;;

If the stu object and stu1 object are the same object, the name attribute value in stu1 object will be changed to "Li Si", two
Li Si is displayed in the Citation object. This is the effect of shallow cloning
The content of the reference type is copied by reference. This situation requires deep cloning, which requires object flow. Code such as:

public class CitaionTest {
    public static void main(String[] args) throws Exception {
        //1. Create prototype object
        Citation citation = new Citation();
        //Create Zhang San Student object
        Student stu = new Student();
        stu.setName("Zhang San");

        //Create objects: output stream objects
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:/robin/a.txt"));
        //Write object
        //Release resources

        //Create object input stream object
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:/robin/a.txt"));
        //read object
        Citation citation1 = (Citation) ois.readObject();
        //Release resources
        Student stu1 = citation1.getStu();
        stu1.setName("Li Si");;;

Note: the Citation class and the Student class must implement the Serializable interface because the object input / output stream is used for the serialization operation. Otherwise, the NotSerializableException exception will be thrown.

Tags: Java

Posted by rallan on Sat, 20 Aug 2022 17:39:28 +0300