Design Mode Notes - Prototype Mode

Keywords: Java Attribute

First, the problem of cloning sheep

Now there is a sheep tom, named tom, age 1, color white, please write a program to create 10 sheep with the same Tom properties

2. Traditional Solutions

1.sheep class

package Design Mode.Prototype mode.Traditional way;

public class Sheeep {
    private String name;
    private int age;
    private String color;
    public Sheeep(String name,int age,String color){
        super();
        this.name=name;
        this.age=age;
        this.color=color;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

2.Client class complete replication

package Design Mode.Prototype mode.Traditional way;

public class Client {
    public static void main(String []args){
        Sheeep sheeep=new Sheeep("tom",1,"white");
        Sheeep[]sheeeps=new Sheeep[10];
        for(int i=0;i<10;i++){
            sheeeps[i]=new Sheeep(sheeep.getName(),sheeep.getAge(),sheeep.getColor());
        }
    }
}

3. Advantages and disadvantages

Advantages: Better understanding, easy operation

Disadvantages: When creating a new object, it is always necessary to get the attributes of the original object. If the object is complex and inefficient to create

There is always a need to reinitialize objects rather than dynamically get their runtime state, which is not flexible enough

4. Improvement ideas

Object class in Java is the base class for all classes. Object class provides a clone() method which can copy a Java object, but Java classes that need to implement clone must implement Cloneble interface--> prototype mode

3. Prototype Model

1. Prototype mode: Create a type of specified object with a prototype instance, and create new objects by copying these prototypes

2. Prototype mode is a creative design mode that allows one object to create another customizable object without knowing the details of the creation

3. How it works: a = prototype object. clone()

4. Code

Sheep

package Design Mode.Prototype mode.improve;

import Design Mode.Prototype mode.Traditional way.Sheeep;

public class Sheep implements Cloneable {
    private String name;
    private int age;
    private String color;
    Sheep(String name,int age,String color){
        this.name=name;
        this.age=age;
        this.color=color;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    protected Object clone(){
        Sheep sheep=null;
        try {
            sheep=(Sheep) super.clone();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
       
        return sheep;
    }
}

Client

package Design Mode.Prototype mode.improve;

public class Client {
    public static void main(String []args){
        Sheep sheep=new Sheep("tom",1,"white");
        Sheep []sheeps=new Sheep[10];
        for(int i=0;i<10;i++){
            sheeps[i]=(Sheep)sheep.clone();
        }
    }
}

Four, deep and shallow copies

shallow copy

1. For a data class type that is a member variable of a basic data type, a shallow copy will directly transfer the value, that is, copy the attribute value to a new object

2. Shallow copies of member variables that refer to data types, such as an array, an object of a class, etc., are passed directly by reference.

3. Cloning sheep is a shallow copy

deep copy

1. Values of member variables of all basic data types of the assignment object

2. Request storage for all member variables that reference data types, and copy the objects referenced by each reference data type member variable

3. Implementation: clone implementation used

By object serialization

 

39 original articles published, 17 praised, 799 visits
Private letter follow

Posted by _SAi_ on Mon, 24 Feb 2020 19:24:57 -0800