Design Pattern-prototype Pattern

Source: Internet
Author: User

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 ");}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.