Design Pattern _ prototype Pattern

Source: Internet
Author: User

Design Pattern _ prototype Pattern
Prototype Pattternspecify the kinds of objects to create using a prototypical instance, and create new objects by coping this prototype. (use a prototype instance to specify the type of the object to be created and copy the prototype to create a new object) Implement the Clonable interface and implement the clone method.
Example email

Public class AdvTemplate {private String advSubject = XX Bank lottery activity; // advertisement name private String advContext = Send you 10Q coins as long as you swipe your card; // public String getAdvSubject () {return advSubject;} public String getAdvContext () {return advContext ;}}

public class Mail implements Cloneable{  private String receiver; private String subject; private String appellation; private 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; } public String getAppellation() {  return appellation; } public void setAppellation(String appellation) {  this.appellation = appellation; } public String getContext() {  return context; } public void setContext(String context) {  this.context = context; } public 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 (XX Bank copyright); while (I
 
  
Feature 1. the constructor does not execute during cloning. 2. in the Object, the clone () method is a shortest COPY method. It only copies the Object itself, its internal array, reference Object, and so on. It still points to the internal element address of the native Object. The two objects share a private variable, which is insecure.
  
Public class Thing implements Cloneable {private ArrayList
   
    
List = new ArrayList
    
     
(); Public Thing () {System. out. println (constructor executed);}/* shortest copy: The two objects share a private variable pointing to the internal element address of the native object. insecure * Deep copy: thing. list = (ArrayList
     
      
) This. list. clone (); * will not be copied if the two conditions are met: 1. class member variable 2. it 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
      
       
) This. list. clone (); // The result is not the same without this sentence} catch (CloneNotSupportedException e) {e. printStackTrace ();} return thing;} public void setValue (String value) {list. add (value);} public ArrayList
       
        
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()); }}

Result: The constructor only runs the constructor once, and the copy operation changes the original set value, but the copy operation does not. The two are independent.
Excellent performance: the copy of the memory binary stream is much better than the new one. In particular, the constructor constraint is escaped when a large number of objects are generated. constructor = constraint.
When you use a scenario to generate an object that requires complex data preparation or access permissions, you can use the disadvantages. When an object requires multiple objects to access, in addition, each caller can modify its value (I have to say that nothing can be seen too narrowly, and its disadvantage is its advantage)
Note: copying objects from memory can be divided into shallow replication and deep replication. It is not safe. It depends on the use cases.

Related Article

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.