Part 2 of 6 · 1 min read
The idea
Copy a sample instead of building from scratch.
Think of a form letter. An office keeps one filled-in sample, photocopies it, and changes the name and date on each copy. It does not retype the letter each time.
GoF's intent for Prototype is to describe the kinds of objects to make by giving a sample instance, and to create new objects by copying that sample. The client asks a prototype to clone itself, and does not need to know the class of the result.
GoF suggests it when the system should not depend on how its products are built, and at least one of these holds: the classes to create are chosen at run time; you want to avoid a parallel hierarchy of factories next to the hierarchy of products; or instances have only a few combinations of state, so it is easier to keep a prototype for each combination and copy it than to build the object by hand each time.
A booking program has meeting templates: a "standup" with a title, a length and a default attendee list. Staff create their own meetings from a template, then adjust a field or two. Copying the template is shorter than setting every field again.
Prototype
A prototype is a ready-made object that is used as a model, so that new objects are made by copying it.
Example: a "standup" meeting template that is copied for each new meeting.
Check yourself: 1. What does a client ask a prototype to do? 2. In which case does GoF say to keep one prototype per combination of state?