Designpattern_java:prototype Pattern

Source: Internet
Author: User

Prototype mode Prototype patternspecify the kinds of objects to create using a prototypical Instance,and create new objects by Copyi Ng this prototype. Use the prototype instance to specify the kind of object to create and create a new object by copying the prototypes. Java built-in cloning mechanism: implements the Clone () method that cloneable interface overrides object. Abstract prototype Role (PROTOTYPE): This role is an abstract role, usually with a Java interface or abstract class implementation, giving all the interfaces required for a specific prototype class.
package com.DesignPattern.Creational.Prototype;public interface Prototype extends Cloneable {    //克隆方法    Prototype clone();}
Specific prototype role (concrete Prototype): This role is the object being copied and must implement an abstract prototype interface.
package com.DesignPattern.Creational.Prototype;public class ConcretePrototype implements Prototype {    @Override    public Prototype clone() {        try {            return (Prototype) super.clone();        } catch (CloneNotSupportedException e) {            e.printStackTrace();            return null;        }    }}
Customer role (client): This role presents a request to create an object.
package com.DesignPattern.Creational.Prototype;public class Client {    public void operation(Prototype example) {        // 得到example的副本        Prototype p = example.clone();    }}
Example of prototype mode Mail.java
Package com.    Designpattern.creational.prototype;public class Mail implements cloneable {private String receiver;    Private String subject;    Private String appellation;    Private String Contxt;    Private String tail;        Public Mail (string subject, String contxt) {this.subject = subject;    This.contxt = Contxt;        }//Clone method public Mail clone () {mail mail = null;        try {mail = (mail) super.clone ();        } catch (Clonenotsupportedexception e) {e.printstacktrace ();    } return mail;    } public String Getreceiver () {return receiver;    } public void Setreceiver (String receiver) {this.receiver = receiver;    } public String Getsubject () {return subject;    } public void Setsubject (String subject) {this.subject = subject;    } public String Getappellation () {return appellation; } public void Setappellation (String appellation) {this.appellation = AppEllation;    } public String Getcontxt () {return contxt;    } public void Setcontxt (String contxt) {this.contxt = Contxt;    } public String GetTail () {return tail;    } public void Settail (String tail) {this.tail = tail; }}
Clientdemo.java
Package com.  Designpattern.creational.prototype;import Java.util.random;public class Clientdemo {//The number of bills sent, this value is the private from the database    static int max_count = 6;        public static void Main (string[] args) {int i = 0;        Define a Mail object mail mail = new mail ("Activity", "there is ...");        Mail.setappellation ("copyright");            while (I < max_count) {//clone mail Mail clonemail = Mail.clone ();            Clonemail.setappellation (getrandstring (5) + "G (M)");            Clonemail.setreceiver (getrandstring (5) + "@" + getrandstring (8) + ". com");            Send mail SendMail (clonemail);        i++; }}//Send mail public static void SendMail (mail mail) {System.out.println ("title:" + mail.getsubject () + "\ T recipient    : "+ mail.getreceiver () +" \t.....send success! ");} Gets the random string of the specified length public static string getrandstring (int maxLength) {string Source = "Abcdefghijklmnopqrstuvwxyza BcdEFGHIJKLMNOPQRSTUVWXYZ ";        StringBuffer sb = new StringBuffer ();        Random rand = new Random ();        for (int i = 0; i < maxLength; i++) {Sb.append (Source.charat (Rand.nextint ()));    } return sb.tostring (); }}

Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint |copyright©2011-2015,supernatural, all rights Reserved.

Designpattern_java:prototype Pattern

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.