Summary

The PROTOTYPE 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 ...

Get Design Patterns Java™ Workbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.