Design Pattern-prototype Pattern
Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object. The prototype mode belongs to the object creation mode. Specify the type of all created objects by providing a prototype object, and then create more objects of the same type by copying the prototype object. This is the purpose of the model selection. The prototype mode requires that the object implement an interface that can "clone" itself, so that you can create a new instance by copying an instance object itself. In this way, you no longer need to care about the type of the instance when creating new objects through the prototype instance. As long as you implement the clone method, you can use this method to obtain new objects, instead, you do not need to create it through new. The Code uses a group email as an example to illustrate: copy the code public class Mail implements Cloneable {private String jsoner; private String subject; private String appellation; private String contxt; private String tail; public Mail (String subject, String contxt) {super (); this. subject = subject; this. contxt = contxt;} public String getReceiver () {return receiver;} public void setReceiver (String receiver) {this. extends ER = extends er;} public String getAppellation () {return appellation;} public void setAppellation (String appellation) {this. appellation = appellation;} public String getTail () {return tail;} public void setTail (String tail) {this. tail = tail;} public String getSubject () {return subject;} public String getContxt () {return contxt;} public Mail clone () {Mail mail = null; try {mail = (Mail) super. clone ();} catch (CloneNotSupportedException e) {// catch Block e automatically generated by TODO. printStackTrace ();} return mail;} copy the code copy code public class ClientDemo {private static int MAX_COUNT = 6; public static void main (String [] args) {int I = 0; Mail mail = new Mail ("group title", "group content"); mail. setTail ("End of Group Sending"); Random rand = new Random (); while (I <MAX_COUNT) {Mail cloneMail = mail. clone (); cloneMail. setAppellation ("title" + rand. nextInt (100) + ":"); cloneMail. setcycler (rand. nextInt (1000000000) + "@ qq.com"); sendMail (cloneMail); I ++ ;}} public static void sendMail (Mail mail) {System. out. println ("title:" + mail. getSubject () + "\ t recipient:" + mail. getcycler () + "\ t ..... sent successfully ");}}