5. Prototype prototype mode

Prototype mode is used on frequently called and extremely similar objects. It clones the object and sets the changed properties, and consumes less resources.

The prototype mode is mainly used for object replication, and its core is the prototype class Prototype in the class diagram. Prototype class needs to meet the following two conditions:

Implement the Cloneable interface. There is a Cloneable interface in the Java language, which has only one function, which is to notify the virtual machine that it can safely use the clone method on the class that implements this interface at runtime. In the Java virtual machine, only classes that implement this interface can be copied, otherwise a CloneNotSupportedException will be thrown at runtime.

  1. Override the clone method in the Object class. In Java, the parent class of all classes is the Object class. There is a clone method in the Object class, which is used to return a copy of the object, but its scope is protected and cannot be called by ordinary classes. Therefore, the Prototype class needs to clone The scope of the method is modified to the public type.
  2.  The prototype mode is a relatively simple mode, and it is also very easy to understand. The prototype mode is completed by implementing an interface and rewriting a method. In practical applications, the prototype mode rarely appears alone. Often mixed with other modes, his prototype class Prototype is often replaced by abstract classes.

Class Diagram

Code example

package designpatterns.prototype;
 
//原型
interface Prototype {
    void setSize(int x);
    void printSize();
 }
 
// 一个具体类
class A implements Prototype, Cloneable {
    private int size;
 
    public A(int x) {
        this.size = x;
    }
 
    @Override
    public void setSize(int x) {
        this.size = x;
    }
 
    @Override
    public void printSize() {
        System.out.println("Size: " + size);
    }
 
    @Override
    public A clone() throws CloneNotSupportedException {
        return (A) super.clone();
    }
}
 
//需要很多类似的对象进行测试
public class PrototypeTest {
    public static void main(String args[]) throws CloneNotSupportedException {
        A a = new A(1);
 
        for (int i = 2; i < 10; i++) {
            Prototype temp = a.clone();
            temp.setSize(i);
            temp.printSize();
        }
    }
}

 

Advantages and applicable scenarios of prototype mode

Using the prototype mode to create an object is much better in performance than directly creating an object , because the clone method of the Object class is a local method, which directly manipulates the binary stream in memory, especially when copying large objects, the performance difference is very obvious.

Another advantage of using the prototype mode is to simplify the creation of objects, making creating objects as easy as copying and pasting when editing documents.

Because of the above advantages, you can consider using prototype mode when you need to repeatedly create similar objects . For example, you need to create an object in a loop body, if the object creation process is more complicated or the number of cycles is many.

Precautions

  1. Copying objects using prototype mode does not call the constructor of the class. Because the copy of the object is done by calling the clone method of the Object class, it directly copies the data in the memory, so it does not call the constructor of the class. Not only the code in the constructor will not be executed, even the access permissions are invalid for the prototype mode. Remember the singleton pattern? In singleton mode, as long as the access permission of the constructor is set to private, singleton can be realized. However, the clone method directly ignores the authority of the construction method. Therefore, the singleton mode and the prototype mode are in conflict. Pay special attention when using it.
  2. Deep copy and shallow copy. The clone method of the Object class only copies the basic data types in the object, and does not copy arrays, container objects, reference objects, etc. This is a shallow copy. If you want to implement deep copy, you must copy the arrays, container objects, reference objects, etc. in the prototype mode separately.
1.	public class Prototype implements Cloneable {  
2.	    private ArrayList list = new ArrayList();  
3.	    public Prototype clone(){  
4.	        Prototype prototype = null;  
5.	        try{  
6.	            prototype = (Prototype)super.clone();  
7.	            prototype.list = (ArrayList) this.list.clone();  
8.	        }catch(CloneNotSupportedException e){  
9.	            e.printStackTrace();  
10.	        }  
11.	        return prototype;   
12.	    }  

Since ArrayList is not a basic type, the member variable list will not be copied. We need to implement deep copy by ourselves. Fortunately, most of the container classes provided by java implement the Cloneable interface. Therefore, deep copy is not particularly difficult.

PS : In the deep copy and shallow copy problems, there are 8 basic types in java and their encapsulation types that will occur in deep copy , and there is also the String type. The rest are shallow copies.

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/sinat_37138973/article/details/88243631