Java Reference
In-Depth Information
Summary
The P ROTOTYPE pattern lets a client create new objects by copying an example. A major
difference between calling a constructor and copying an object is that a copy typically
includes some of the state of the original object. You can use this to your advantage,
particularly when different "classes" of objects differ only in their attributes, not in their
behaviors. In such a case, you can create new classes at runtime by crafting prototypical
objects for a client to copy.
When you need to create a copy, Object.clone() can help, but you must remember that
this method creates a new object with the same fields. This object may not be a suitable clone,
but any difficulties related to deep copying are your responsibility. If creating a deep copy is
too complex or if a prototypical object has too many fields, you can create a copy by
instantiating a new object and setting its fields to match just the aspects of original object that
you need to copy.
Search WWH ::




Custom Search