Detailed explanation of prototype mode

Prototype mode

Definition: used to create duplicate objects while ensuring performance. Specify the type of objects to be created with prototype instances, and create new objects by copying these prototypes.

Usage scenario:

  • resource optimization
  • Scenarios with performance and safety requirements
  • If generating an object through new requires very cumbersome data preparation or access rights, you can use the prototype pattern
  • When an object needs to be accessed by other objects, and each caller may need to modify its value, you can consider using the prototype pattern to copy multiple objects for the caller to use
  • Prototype patterns rarely appear alone. They are generally used in conjunction with factory method patterns. Prototype patterns provide calls to clone objects and factory methods

Service conditions:

  • The clonable interface must be implemented. In the java virtual machine, only classes that implement this interface can be copied. Otherwise, CloneNotSupportedException will be thrown at runtime.
  • Override the clone() method in the Object class

matters needing attention:

  • Prototype mode is to generate new objects by copying an existing Object, which belongs to shallow copy. You only need to implement the clonable interface and override the clone() method of Object. Deep copy reads binary streams by implementing Serializable. The clone() method of the Object class only copies the basic data types in the Object. For arrays, objects, etc., the address of the Object is copied (therefore, each clone Object points to the same Object, and data isolation cannot be achieved).
  • The prototype pattern copy Object does not call the constructor of the class. Because the Object is copied through the clone() method of the Object. Therefore, the access permission of the constructor will also be invalid and conflict with the singleton mode.

realization

Prototype class

/**
 * Prototype class
 */
@Data
public abstract class Prototype implements Cloneable{
    protected  String name;

    public Prototype clone(){
        Prototype prototype = null;
        try{
            prototype = (Prototype)super.clone();
        }catch(CloneNotSupportedException e){
            e.printStackTrace();
        }
        return prototype;
    }

    public abstract void say(int i);
}

/**
 * Prototype subclass 1
 */
public class PrototypeSubclass1 extends Prototype{
    public PrototypeSubclass1() {
        name = "Subclass 1 parameterless construction";
    }

    public PrototypeSubclass1(int i) {
        name = "Subclass 1 parametric construction";
    }

    @Override
    public void say(int i) {
        System.out.println("Created page "+ i +" Prototype class subclass 1");
    }
}


/**
 * Prototype subclass 2
 */
public class PrototypeSubclass2 extends Prototype{
    public PrototypeSubclass2() {
        name = "Subclass 2 nonparametric construction";
    }

    public PrototypeSubclass2(int i) {
        name = "Subclass 2 parametric construction";
    }

    @Override
    public void say(int i) {
        System.out.println("Created page "+ i +" Prototype class subclass 2");
    }
}

Caller (clone object)

public class CallPrototype {
    public static void main(String[] args){
        Prototype pt1 = new PrototypeSubclass1(1);
        for(int i=0; i< 2; i++){
            // clone does not call the constructor
            Prototype prototype = (Prototype)pt1.clone();
            prototype.say(i);
            System.out.println(prototype.getName());
        }
        Prototype pt2 = new PrototypeSubclass2();
        for(int i=0; i< 2; i++){
            Prototype prototype = (Prototype)pt2.clone();
            prototype.say(i);
            System.out.println(prototype.getName());
        }
    }
}
------------------------Print results-----------------------
Created 0 prototype class subclass 1
 Subclass 1 parametric construction
 Created the first prototype class subclass 1
 Subclass 1 parametric construction
 Created 0 prototype class subclass 2
 Subclass 2 nonparametric construction
 Created the first prototype class subclass 2
 Subclass 2 nonparametric construction

Tags: Design Pattern

Posted on Sat, 06 Nov 2021 06:47:28 -0400 by phencesgirl