- If someone asks me what is the example of prototype pattern in Java which is already implemented in Java APIs , can I say clone() method ?
- I feel clone() method in itself doesn't exemplify prototype pattern , it's just a means to achieve prototype pattern. Am I right ?
- Though some answers on stackoverflow says that clone() method is an example of Prototype pattern in Java e.g. Examples of GoF Design Patterns in Java's core libraries Are they right ?

- 1
- 1

- 528
- 7
- 15
-
what do you understand about prototype pattern? – SMA Feb 27 '15 at 09:06
-
http://stackoverflow.com/questions/17203493/prototype-pattern-in-java-the-clone-method – shikjohari Feb 27 '15 at 09:11
2 Answers
Clone is essentially an implementataion of the prototype pattern, but it doesn't really get to the heart of why you use the pattern. Imagine you are buying a new car and you go to the dealer, she asks you:
- What colour do you want it?
- Which wheels would you like?
- Fabric or leather seats?
- Do you want heated seats?
- Do you want air conditioning?
- etc.
Now imagine you are a fleet manager for a large company; do you want to go through this for every car you buy? It's rather easier to just choose the details for the first car and then just tell the dealer "give me another 100 like this".
That's basically the prototype pattern - it gives you an easy way to create objects which are complex to initially set up by simply copying one you already configured.
The clone() method can do this for you, but it's only if you are using it for the above reason that you are really using the prototype pattern.

- 3,759
- 12
- 26
1) Not really.
2) It is meant to be implemented by a class which needs it,and does not really work for every type of object in it's general format.
So, for your situation, the answers are no and no.

- 208
- 1
- 12