0% found this document useful (0 votes)
44 views8 pages

Prototype Pattern: Bryan Hansen

The prototype pattern avoids subclassing and costly object creation by allowing objects to copy or clone themselves. It involves implementing a clone or copy method that performs a shallow or deep copy of an object. This allows new objects to be created without using the new operator. The prototype pattern is often used with a registry to manage and provide access to prototype object instances. It can help improve performance over subclassing by copying existing objects rather than creating new ones from scratch.

Uploaded by

Kuby Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views8 pages

Prototype Pattern: Bryan Hansen

The prototype pattern avoids subclassing and costly object creation by allowing objects to copy or clone themselves. It involves implementing a clone or copy method that performs a shallow or deep copy of an object. This allows new objects to be created without using the new operator. The prototype pattern is often used with a registry to manage and provide access to prototype object instances. It can help improve performance over subclassing by copying existing objects rather than creating new ones from scratch.

Uploaded by

Kuby Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Prototype Pattern

Bryan Hansen
twitter: bh5k | http://www.linkedin.com/in/hansenbryan
Concepts
▪ Avoids costly creation
▪ Avoids subclassing
▪ Typically doesn’t use “new”
Drag picture to
▪ Often utilizes an Interface placeholder or click
icon to add a graphic
▪ Usually implemented with a Registry
▪ Example:
▪ java.lang.Object#clone()
Design

Clone / Cloneable
Avoids keyword “new”
Although a copy, each instance unique
Costly construction not handled by client
Can still utilize parameters for construction
Shallow VS Deep Copy
Everyday Example - Object Clone
public  class  Statement  implements  Cloneable  {  

  public  Statement(String  sql,  List<String>  parameters,  Record  record)  {  


    this.sql  =  sql;  
    this.parameters  =  parameters;  
    this.record  =  record;  
  }  

  public  Statement  clone()  {  


    try  {  
      return  (Statement)  super.clone();  
    }  catch  (CloneNotSupportedException  e)  {}  
    return  null;  
  }      
}
Exercise Prototype

Create Prototype
Demonstrate shallow copy
Create with a Registry
Pitfalls
▪ Sometimes not clear when to use
▪ Used with other patterns
▪ Registry
▪ Shallow VS Deep Copy
Contrast

Prototype Factory
▪ Lighter weight construction ▪ Flexible Objects
▪ Copy Constructor or Clone ▪ Multiple constructors
▪ Shallow or Deep ▪ Concrete Instance
▪ Copy of itself ▪ Fresh Instance
Prototype Summary

• Guarantee unique instance


• Often refactored in
• Can help with performance issues
• Don’t always jump to a Factory

You might also like