The prototype pattern in a nutshell

When needing to implement the prototype pattern with classes, remember that the NSCopying protocol is part of Foundation.

  1. Identify the need for large classes that are already initialized to be copied.
  2. If possible, use structs and value type semanticsthis saves a lot of boilerplate code.
  3. Using classes, implement NSCopying and NSMutableCopying if necessary.
  4. Leverage code generation tools such as Sourcery to help reduce implementation time.
  5. Where needed, replace the new instances of the objects with calls to copy() on the prototype object.

To sum up, the prototype pattern is widely used in Foundation through NSArray and NSDictionary. In Swift, you can always leverage value types to benefit transparently from copying your objects. Value types benefit from copy-on-write, which ultimately prevents the mutation of prototype objects.