Prototype Pattern

Make new objects by copying a configured one instead of building from scratch, and know exactly what a copy shares with its original.

Overview

Prototype is a creational pattern from the Gang of Four book (Gamma, Helm, Johnson and Vlissides, *Design Patterns*, 1994). Instead of naming a class and calling its constructor, the client keeps a configured object, the prototype, and asks it for a copy. New variants come from copying and adjusting, not from new subclasses.

The pattern is small; what interviewers test is the copy itself. A shallow copy makes a new outer object whose fields point at the same inner objects as the original. A deep copy makes new inner objects too. Pick the wrong one and two objects that should be independent change together. Most Prototype questions are really shallow-versus-deep questions.

Python and Java both ship the machinery: Python's copy module and copy / deepcopy hooks, and Java's Object.clone() with the Cloneable marker. Knowing what each does by default is the difference between a good answer and a wrong one.

Shallow copy versus deep copy

  • Outer object — New — New
  • Inner objects (lists, dicts, other objects) — Shared with the original — New copies, recursively
  • Cost — Cheap — Grows with the object graph
  • Python — copy.copy(x) — copy.deepcopy(x)
  • Java default — Object.clone() copies fields; it does not clone what they point to — You write it: clone, then copy each mutable field
  • Use when — Inner objects are immutable or meant to be shared — The copy must be independent

How a deep copy handles cycles

Python's deepcopy keeps a memo dictionary of objects already copied during the pass. When it meets an object again, it reuses the copy instead of recursing forever, so a graph with cycles copies correctly.

Where you meet it for real

  • Python copy.copy / copy.deepcopy — Shallow and deep copies of most objects; classes customize them with copy() and deepcopy(memo)
  • Python dataclasses.replace(obj, **changes) — A new object of the same type with some fields replaced; it goes through init, so validation runs again
  • Python copy.replace() (3.13) — The same idea for any class that defines replace()
  • Java Object.clone() — A field-by-field copy; throws CloneNotSupportedException unless the class implements Cloneable; by convention x.clone() != x

What you will be able to do

  • Say in one sentence what a shallow copy shares and a deep copy does not, and show it in code.
  • Write a prototype registry and explain why a clone, not the template, is handed out.
  • Implement deepcopy in Python and a correct clone() in Java, and know what each must reset.
  • Choose between Prototype, a copy constructor and a factory for a given problem.

Interview questions

  • Show a bug caused by a shallow copy and how you fix it.
  • How do you implement Prototype correctly in Python and in Java?
  • When is Prototype the right pattern, compared with a factory or a builder?
  • Copy constructor or clone(): which do you recommend?
  • What goes wrong when Prototype is used carelessly?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio