1. Scene description
In the creation mode, from the factory method mode, abstract factory mode, to the builder mode, and then to the prototype mode, my understanding is that the way of creating objects is gradually shifted from coding implementation to memory object processing.
For example, during the creation of the subclass/objects of "PDF File Data Collector" and "Excel File Data Collector",
Each subclass is defined in the factory mode and is created by the (abstract) factory class Factory, so each subclass can define its respective properties in the class definition;
In the builder mode, different sub-objects are created through different creator classes Builders, and the sub-classes are no longer defined at this time;
In the prototype mode, the caller creates child objects entirely based on the parent object cloning, and does not create classes for the child object or its related factory or builder classes.
The three modes correspond to different scenarios. When actually operating, the mode is selected reasonably according to the scenario.
In prototype mode, a new object is created based on the prototype object, so the attribute values assigned to the prototype object can be used directly in the new object, eliminating repeated assignments;
For example, the common initialization work of the instrument data collector can be completed in the prototype object, which can then be cloned out the PDF file data collector object and the Excel file data collector object, and subsequent expansion of the two object properties is eliminated, which eliminates the initialization work of public attributes;
The cloning operation is completed in memory. Since the attribute value of the object type is stored as a reference, the cloning is divided into shallow cloning and deep cloning, and deep cloning is realized through the Serializable interface.
2. Sample code
Prototype:
package lims.designpatterndemo.prototypedemo;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;public class EquipmentDataCapture implements Cloneable, Serializable { private String filePath = "file path"; private String equipmentData = "file content"; // public String getFilePath() { return this.filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public String getEquipmentData() { return this.equipmentData; } public void setEquipmentData(String equipmentData) { this.equipmentData = equipmentData; } // private static final long serialVersionUID = 1L; private SerializableObject obj; // public SerializableObject getObj() { return obj; } public void setObj(SerializableObject obj) { this.obj = obj; } // public EquipmentDataCapture getEquipmentDataCapture() throws CloneNotSupportedException { EquipmentDataCapture capture = (EquipmentDataCapture) super.clone(); return capture; } // public EquipmentDataCapture getPdfFileCapture() throws CloneNotSupportedException { // EquipmentDataCapture capture = (EquipmentDataCapture) super.clone(); // capture.setEquipmentData("pdf file content");// return capture; // } // public EquipmentDataCapture getExcelFileCapture() throws CloneNotSupportedException { // EquipmentDataCapture capture = (EquipmentDataCapture) super.clone(); // capture.setEquipmentData("excel file content");// return capture; // } /* Deep copy*/ public EquipmentDataCapture newEquipmentDataCapture() throws IOException, ClassNotFoundException { /* Write binary stream to the current object*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(this); /* Read out the new object generated by the binary stream*/ ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); return (EquipmentDataCapture)ois.readObject(); } }class SerializableObject implements Serializable { private static final long serialVersionUID = 1L;}Calling side:
package lims.designpatterndemo.prototypedemo;public class PrototypeDemo { public static void main(String[] args) throws CloneNotSupportedException { EquipmentDataCapture edc = new EquipmentDataCapture(); EquipmentDataCapture capture = null;// capture = edc.getPdfFileCapture();// capture = edc.getExcelFileCapture(); capture = edc.getEquipmentDataCapture(); capture.setEquipmentData("equipment data file content"); String fileContent = capture.getEquipmentData(); System.out.println(fileContent); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.