Breaking down the Prototype pattern
As part of the creational family of design patterns, the Prototype pattern gives us control over how we make copies of common base objects, making it effective when:
- A system needs to be independent of how its objects are created, composed, and represented.
- The objects you’re creating need to be specified at runtime.
- You want to avoid parallel class hierarchies of factories and objects.
- You want to specify the kind of objects you’re creating by defining a prototypical instance and copying it.
A useful mental model for this scenario is a photocopier (remember those?), shown in Figure 3.1. If we had a memo to pass around the office, it wouldn’t be efficient to create a new memo for each employee; instead, we’d simply duplicate the original memo and hand out copies (or use email, whichever ends up being faster).
Figure 3.1: Photocopy machine example
Rather than spending...