CSE Roadmap

Common tier · about 2 hours · 6 parts

Part 5 of 6 · 1 min read

In design rounds

Templates, defaults and undo.

Where prototype shows up in design rounds

A few common prompts suit the pattern. A document or form tool that starts from a template. A game that spawns many enemies of a few types. A scheduling tool that repeats a meeting each week. In each, a model object already holds the right defaults, and the new object differs in a field or two.

The copy also helps when building an object is slow, such as one loaded from a file or a database. Load once, keep as a prototype, and copy in memory. This is a performance reason and should be backed by a measurement, since a copy of a large graph is not free either.

The pattern also supports undo in some designs. Before a change, the program stores a copy of the object. To undo, it swaps the copy back. This is related to the memento idea and is mentioned here only as a use of copying.

State what you copy and what you share. A good answer names the immutable parts that are shared and the mutable parts that are duplicated, and says that the registry hands out copies and never the stored original.

Check yourself: 1. Why can copying be faster than loading an object again? 2. What should a registry never return?

Words you will meet

Word Meaning
Prototype A sample object that is copied to make new ones
Shallow copy A copy that shares the inner objects of the original
Deep copy A copy that duplicates the inner mutable objects as well
Copy constructor A constructor that takes an object of its own class
Registry A map from keys to stored prototypes