Design pattern note 5 - prototype pattern

Keywords: Java Design Pattern

5. Prototype mode

         Prototype pattern refers to specifying the type of objects to be created with prototype instances, and creating new objects by copying these prototypes

         Prototype pattern is a creative design pattern that allows an object to create another customizable pair without knowing the details of how to create it

         Working principle: by passing a prototype object to the object to be created, the object to be created is created by requesting the prototype objects to copy themselves, that is, the object. clone()

         Image understanding: Monkey King pulls out monkey hair and turns into other monkeys

Let's take an example of cloned sheep         

         Now there is a sheep, Dolly , name : tom, Age: 2 , color: white, please write a program to create and dolly Sheep have exactly the same attributes 10 A sheep.

5.1 traditional methods to solve cloned sheep

thinking

No idea, write directly

UML class diagram

code

//Sheep.java
//It's basically generated automatically. There's nothing to look at
public class Sheep {
    private String name;

    private int age;

    private String color;

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }

    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 String getColor() {
        return color;
    }

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

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


//Client.java
public class Client {
    public static void main(String[] args) {
        Sheep sheep = new Sheep("Dolly", 2, "white");
        Sheep sheep1 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
        Sheep sheep2 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
        Sheep sheep3 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
        //....

        System.out.println(sheep);
        System.out.println(sheep1);
        System.out.println(sheep2);
        System.out.println(sheep3);
        //...
    }
}

/*Operation results
Sheep{name='Dolly ', age=2, color =' white '}
Sheep{name='Dolly ', age=2, color =' white '}
Sheep{name='Dolly ', age=2, color =' white '}
Sheep{name='Dolly ', age=2, color =' white '}
*/

analysis

         This method is quite simple, easy to think of, and the problem is obvious. When I modify or add a new attribute to the sheet (such as adding a weight attribute to the sheet), the part of the constructor needs to be changed. Similarly, every code that uses the constructor new needs to be changed. (I've also thought about whether it's better to use circulation. Don't make a fuss first, go on to learn.)

5.2 prototype mode to solve the problem of cloned sheep

thinking

        Create an interface of the clone class, and let the object to be cloned directly implement the cloning method of the interface. There happens to be such an interface in java, which can be used directly.

UML class diagram

  code

//Sheep.java
//Implement clonable interface
public class Sheep implements Cloneable {
    private String name;

    private int age;

    private String color;

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }

    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 String getColor() {
        return color;
    }

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

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

//Implement clone method
    @Override
    public Object clone() {

        Sheep sheep = null;
        try {
            sheep = (Sheep) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        return sheep;
    }
}


//Client.java
public class Client {
    public static void main(String[] args) {
        Sheep sheep = new Sheep("Dolly", 2, "white");
        Object clone = sheep.clone();
        Object clone1 = sheep.clone();
        Object clone2 = sheep.clone();
        Object clone3 = sheep.clone();
        Object clone4 = sheep.clone();
        //...
        System.out.println(sheep);
        System.out.println(clone);
        System.out.println(clone1);
        System.out.println(clone2);
        System.out.println(clone3);
        System.out.println(clone4);
        System.out.println("................");
        //..
    }
}

/* Operation results
Sheep{name='Dolly ', age=2, color =' white '}
Sheep{name='Dolly ', age=2, color =' white '}
Sheep{name='Dolly ', age=2, color =' white '}
Sheep{name='Dolly ', age=2, color =' white '}
Sheep{name='Dolly ', age=2, color =' white '}
Sheep{name='Dolly ', age=2, color =' white '}
................
*/

analysis

        It can be seen that the clone method has been replaced where the constructor clone needs to be called. Even if the property of sheet is changed, there is no need to modify the code of the clone part. Better maintainability than traditional methods.

Posted by mzshah on Thu, 07 Oct 2021 00:37:59 -0700