Prototype
Pattern of GOF Design Patterns
I would like to understand in detail about the Prototype Pattern of GOF design Patterns. Can anyone help me get the details to understand it. I have gone through google and have the Same UML diagram everywhere, but want to understand what that UML diagram actually explains. Prototype pattern is usually used to create objects when the process of object creation is time consuming when done from scratch. In this case an existing object is used as an prototype and similar objects are constructed using this prototype object(yeah, no constructors). We already have a facility to implement the prototype pattern in the JAVA API. That's the Cloneable interface. A small example: Lets say there is a need for you to create several Cookie Objects and its time/resource consuming to create a Cookie Object from scratch. SO we'll use a prototype to create the several Objects instead of goin thru the constructor. public class Cookie implements Cloneable{
public class CoconutCookie extends Cookie{
public class CookieMachine{ private Cookie cookie;//could have
well
public CookieMachine(Cookie cookie){
public Cookie makeCookie(){
public Object clone(){
public static void main(String args[]){
for(int i=0;
i<100; i++)
_________________ | CookieMachine | ----------------- | | |------------| |Cloneable c----|-------->| Cloneable | | | |------------| ----------------- ^ | Cookie ^ | CoconutCookie Compare this UML diagram with a UML diagram for a prototype Design Pattern, I hope you'll get some idea.
-- Vinay Binny
Related:
Java Certification, Programming, JavaBean and Object Oriented Reference Books Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|