Prototype Pattternspecify the kinds of objects to create using a prototypical Instance,and create new objects by coping this Prototy Pe. (Specify the kind of object created with the prototype instance instance and create a new object by copying the prototype)implement the Clonable interface and implement the Clone method.
ExampleSend e-mail
public class Advtemplate {private String advsubject = "xx Bank lottery"; Ad name Private String Advcontext = "Just swipe to send you 10Q"; Ad content public string Getadvsubject () { return advsubject;} public string Getadvcontext () { return advcontext;}}
public class Mail implements cloneable{private string receiver; private string subject; private string appellation; Priv Ate String context; Private String tail; Public Mail (Advtemplate advtemplate) {this.context=advtemplate.getadvcontext (); This.subject=advtemplate.getadvsubject (); } @Override protected 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;} Publ IC string getappellation () {return appellation;} public void Setappellation (String appellation) {this.appellation = a Ppellation; public string GetContext () {return context,} public void SetContext (String context) {This.context = context;} Publ IC String GetTail () {return tail;} public void Settail (String tail) {this.tail = tail;}}
public class Clienti {private static int max_count=6; public static void Main (string[] args) { int i=0; Mail Mail=new Mail (new advtemplate ()); Mail.settail ("All rights reserved by XX Bank"); while (I<max_count) { Mail clonemail=mail.clone (); Copy clonemail.setappellation here (getrandstring (5) + "Mr. (female)"); Clonemail.setreceiver (getrandstring (5) + "@" +getrandstring (8) + ". com"); SendMail (clonemail); i++; } } private static void SendMail (mail mail) { System.out.println ("title:" +mail.getsubject () + "\ T recipient:" + Mail.getreceiver () + "send success!"); private static String getrandstring (int maxLength) { string source= " ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ "; StringBuffer sb=new StringBuffer (); Random random=new random (); for (int i=0;i<maxlength;i++) { sb.append (Source.charat (Random.nextint (Source.length ()))); } return sb.tostring (); }}
Features 1. Cloning when the constructor does not execute the Clone () method in 2.Object is a shallow copy, only the object itself, its internal array, reference object, etc., or the internal element address of the native object. Two objects share a private variable, which is not secure.
public class Thing implements cloneable{private arraylist<string> list=new arraylist<string> (); Public Thing () { System.out.println ("constructed method executed"),}/* Shallow copy: Two objects share a private variable, point to the inner element address of the native object, unsafe * deep copy: thing.list= ( arraylist<string>) This.list.clone (); * Two conditions are not copied: 1. The member variable of the class 2. Must be a mutable reference object, not an original type or immutable object */@Override protected Thing Clone () { Thing Thing=null; try { thing= (thing) Super.clone (); Thing.list= (arraylist<string>) This.list.clone (); Not the same as the result of this sentence } catch (Clonenotsupportedexception e) { e.printstacktrace (); } return thing; } public void SetValue (String value) { list.add (value);} Public arraylist<string> GetValue () { return this.list;}}
public static void Main (string[] args) { Thing thing=new Thing (); Thing.setvalue ("LOL"); Thing Thingclone=thing.clone (); Thingclone.setvalue ("CF"); System.out.println (Thing.getvalue ()); }}
The result: whether it is a shallow copy or a deep copy, the constructor executes only once, and the shallow copy changes the original set value, while the deep copy does not, two separate.
Excellent performancethe copy of the memory binary stream is much better than the new performance, especially when a large number of objects are required to produceEscape from the constraints of a constructor, constructor = constraint
Usage Scenarioswhen producing an object that requires very tedious data preparation or access, you can use thedisadvantage, when an object requires multiple object access, and each caller can modify its value (it must be said that nothing is too narrow to see, the disadvantage is the advantage)
Understanding : Copying objects from memory, divided into shallow copy and deep copy. Unsafe, to see the use of the occasion.
I'm a rookie, I'm on my way.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Design Pattern _ Prototype mode