The prototype pattern
The prototype design pattern performs similar tasks to other creational patterns, such as builders and factories, but it takes a very different approach. Rather than rely heavily on a number of hard-coded sub-classes, the prototype, as its name suggests, makes copies from an original, vastly reducing the number of sub-classes required and any lengthy creation processes.
Setting up a prototype
The prototype is at its most useful when the creation of an instance is expensive in some way. This could be the loading of a large file, a detailed cross-examination of a database, or some other computationally expensive operation. Furthermore, it allows us to decouple cloned objects from their originals, allowing us to make modifications without having to re-instantiate each time. In the following example, we will demonstrate this using functions that take some considerable time to calculate when first created: the nth prime number and the nth Fibonacci number.
Viewed diagrammatically...