Prototype mode of GOF23 design mode

Posted by phpprogrammer on Sat, 18 Dec 2021 09:06:24 +0100

What is a prototype pattern

Prototype Pattern is used to create duplicate objects while ensuring performance. This type of design pattern belongs to creation pattern, which provides the best way to create objects.

This pattern implements a prototype interface that is used to create a clone of the current object. This pattern is used when the cost of directly creating objects is relatively high. For example, an object needs to be created after a costly database operation. We can cache the object, return its clone on the next request, and update the database when necessary to reduce database calls

Usage scenario

(1) Class initialization needs to digest a lot of resources, including data and hardware resources. These consumption can be avoided through prototype copy.
(2) Through new, an object requires very cumbersome data preparation or access rights, and the prototype pattern can be used.
(3) An object needs to be accessed by other objects, and each caller may need to modify its value. You can consider using the prototype mode to copy multiple objects for the caller's use, that is, protective copy

How to solve: use an existing prototype object to quickly generate the same instance as the prototype object.

Implementation mode

  1. Implement clonable interface
  2. Override clone method (super.clone)

Implementation code

You can see that the implementation of the prototype pattern is very simple, as follows

public class Test implements Cloneable{

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

Related development

But object The clone () method has some problems in some scenarios. Let's look at the following problems first?

Question 1:

  1. What is shallow copy? What is deep copy? What is the difference between shallow copy and deep copy?

Phenomena caused by shallow replication: a Test object inherits the clonable interface and overrides the clone()f method, and there are two attributes, int id; User user; id is the basic type and user is an object.
If Test t1 = new Test(1,user); Test t2 = t1.clone(); At this time, two objects with the same content, T1 and T2, will be obtained, that is, T1 ID equals T2 id, t1. User equals T2 user; The problem lies in the user. We all know that the basic type in java is value passing, while the object passes reference and memory address. If we modify an attribute of user in T1, the of that attribute in T2 will also change with the modification of T1. Such a copy is called a shallow copy and will not produce such a deep copy.

2. What are the implementations of shallow copy

  • constructor
	public User(User u){
        this.id = u.id;
        this.name = u.name;
        //... Other properties
    }
  • Cloneable
public class User implements Cloneable{
    
    public int age;
    public String name; //String is also an object, but it is not like other objects, because the value of string is a constant, that is, it is a new object of new every time

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

3. What are the implementation methods of deep copy

  • constructor
  public class User implements Cloneable{
    
    public User(int age, String name){
        return new User(age, name);//Directly re create a User object
    }

    public int age;
    public String name;

    public Object clone() {
        Object clone = null;
        try {
            clone = super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return clone;
    }
}
  • Cloneable
    That is, the object properties inside the User object must all implement the clonable interface
  public class User implements Cloneable{
  
    public int age;
    public ArrayList<String> nameList;//There may be multiple aliases

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

Topics: Java Design Pattern