Definition: Use prototype instances to specify the type of objects to be created, and create new objects by copying these prototypes.
Type: Create class pattern
Class diagram:
The prototype pattern is mainly used for object copying, and its core is the prototype class Prototype in the class diagram. The Prototype class needs to meet the following two conditions:
Implement Cloneable interface. There is a Cloneable interface in the Java language, which has only one function, which is to notify the virtual machine at runtime that can safely use the clone method on the classes that implement this interface. In a java virtual machine, only classes that implement this interface can be copied, otherwise a CloneNotSupportedException will be thrown at runtime.
Rewrite the clone method in the Object class. In Java, the parent classes of all classes are Object classes. There is a clone method in the Object class, which returns a copy of the object, but its scope is of protected type, and general classes cannot be called. Therefore, the Prototype class needs to modify the scope of the clone method to public type.
The prototype mode is a relatively simple mode and is very easy to understand. To implement an interface and rewrite a method, the prototype mode is completed. In practical applications, prototype patterns rarely appear alone. It is often mixed with other patterns, and its prototype class Prototype is also often replaced by abstract classes.
Implementation code:
package PrototypePattern; public class PrototypeClass implements Cloneable{ @Override protected PrototypeClass clone(){ PrototypeClass prototypeClass = null; try { prototypeClass = (PrototypeClass)super.clone(); } catch (CloneNotSupportedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return prototypeClass; }}Client:
package PrototypePattern; public class Client { public static void main(String[] args) { PrototypeClass obj1 = new PrototypeClass(); PrototypeClass obj2 = obj1.clone(); System.out.println(obj1); System.out.println(obj2); }} Advantages of prototype mode and applicable scenarios
Creating objects using prototype mode is much better than directly new one, because the clone method of the Object class is a local method that directly manipulates binary streams in memory, especially when copying large objects, the performance difference is very obvious.
Another benefit of using prototype patterns is to simplify the creation of objects, making creating objects as simple as copy-paste when we edit a document.
Because of the above advantages, you can consider using prototype patterns when you need to create similar objects repeatedly. 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, using the prototype mode can not only simplify the creation process, but also improve the overall performance of the system a lot.
public class Prototype implements Cloneable { private ArrayList list = new ArrayList(); public Prototype clone(){ Prototype prototype = null; try{ prototype = (Prototype)super.clone(); prototype.list = (ArrayList) this.list.clone(); }catch(CloneNotSupportedException e){ e.printStackTrace(); } return prototype; } }Notes on prototype mode:
Since ArrayList is not a basic type, the member variable list will not be copied, and we need to implement deep copy by ourselves. Fortunately, most of the container classes provided by Java implement the Cloneable interface. So it is not particularly difficult to achieve deep copying.
PS: In the deep copy and shallow copy problems, deep copy will occur, including the basic types in 8 Java and their encapsulation types, and there are also String types. The rest are shallow copies.