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
                          // private Cloneable cookie;

    public CookieMachine(Cookie cookie){
        this.cookie = cookie;
    }

    public Cookie makeCookie(){
    return (Cookie)cookie.clone();
    }

    public Object clone(){
    }

    public static void main(String args[]){
        Cookie tempCookie =  null;
        Cookie prot = new CoconutCookie();
        CookieMachine cm = new CookieMachine(prot);

        for(int i=0; i<100; i++)
            tempCookie = cm.makeCookie();
    }
}

_________________
| 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.


The CookieMachine is somewhat like a factory and does'nt need to knoe the actual runtime class of the object to create an instance and hence letting you pass objects dynamically. (gtg more explaination next time)

-- Vinay Binny

Related:

Java Books
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.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.