Design Mode 5: Prototype Mode

Catalog

Prototype mode

  • A prototype pattern means that a prototype instance specifies the type of prototype objects and creates new objects by copying them.
  • Scenarios for prototype mode:
    1. Class initialization consumes more resources
    2. new requires a very tedious process to produce an object (access rights, data preparation, etc.)
    3. Constructors are complex
    4. When a large number of objects are generated in a loop

Shallow clone

  • Write a simple example here, by implementing Cloneable, with the following code, first create an entity class as a prototype:
@Getter
@Setter
public class Monkey implements Cloneable{
    private String name;
    private int age;
    private List<String> skills;

    @Override
    public Monkey clone() throws CloneNotSupportedException {
         return (Monkey) super.clone();
    }

}

  • Write another test method test:
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Monkey monkey = new Monkey();
        List<String> list = new ArrayList<>();
        list.add("fly");
        monkey.setAge(500);
        monkey.setName("Qitian Dasheng");
        monkey.setSkills(list);
        Monkey monkey1 = monkey.clone();
        System.out.println("prototype" + monkey);
        System.out.println("copy" + monkey1);
        System.out.println("prototype" + monkey.getSkills());
        System.out.println("copy" + monkey1.getSkills());
        System.out.println(monkey.getSkills() == monkey1.getSkills());
        list.add("run");
        System.out.println("prototype" + monkey.getSkills());
        System.out.println("copy" + monkey1.getSkills());
        System.out.println(monkey.getSkills() == monkey1.getSkills());
    }
}
  • First look at the results:
  • As you can see from the above results, although a new entity is created by the clone() method, the values of the prototype and the copy will change by modifying any one of the attribute values, that is, for the attributes inside the entity, not the value of the copy, but the reference address of the copy, all of which point to the original object, which is a shallow clone.

Deep Cloning

  • The new object created by deep cloning belongs to a complete individual, completely separate from the prototype itself
  • Still take Qitian Dasheng as an example, the code is as follows
@Getter
@Setter
public class MonkeyKing  implements Cloneable, Serializable {
    private String name;
    private int age;
    private List<String> skills;
    @Override
    protected Object clone() {
        //Return Deep Clone Object
        return this.deepClone();
    }
    //Define a deep cloning method
    private Object deepClone() {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(this);

            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);

            MonkeyKing copy = (MonkeyKing) ois.readObject();
            return copy;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
  • test method
public class test {
    public static void main(String[] args) {
        MonkeyKing monkey = new MonkeyKing();
        List<String> list = new ArrayList<>();
        list.add("fly");
        monkey.setAge(500);
        monkey.setName("Qitian Dasheng");
        monkey.setSkills(list);
        MonkeyKing monkey1 = (MonkeyKing)monkey.clone();
        System.out.println("prototype" + monkey);
        System.out.println("copy" + monkey1);
        System.out.println("prototype" + monkey.getSkills());
        System.out.println("copy" + monkey1.getSkills());
        System.out.println(monkey.getSkills() == monkey1.getSkills());
        list.add("run");
        System.out.println("prototype" + monkey.getSkills());
        System.out.println("copy" + monkey1.getSkills());
        System.out.println(monkey.getSkills() == monkey1.getSkills());
    }
}
  • Result
  • You can see that the object generated by deep cloning is no longer a reference to the original object, but a new object is created after the original value is copied.
Twenty-eight original articles were published. They were praised 2. Visits 1619
Private letter follow

Tags: Attribute

Posted on Sat, 11 Jan 2020 21:11:32 -0500 by kenwvs