CreationalverifiedVerified

Prototype

Creates new objects by cloning an existing instance, avoiding the cost of building from scratch.

Coming Soon

errorWhat Problem Does the Prototype Pattern Solve?

Creating objects from scratch can be expensive when initialization involves database queries, network calls, or complex computation. Additionally, client code may need copies of objects whose concrete class it doesn’t know, making direct instantiation via `new` impossible.

check_circleHow the Prototype Pattern Works

Declare a clone method on a prototype interface. Each concrete class implements clone to produce a copy of itself, including all internal state. Clients create new objects by asking an existing instance to clone itself, bypassing constructors and decoupling the client from concrete classes.

Prototype Pattern Architecture

hourglass_empty

Rendering diagram...

Implementation by Language

lightbulb

Prototype Pattern in the Real World

Think of a cell dividing through mitosis. Instead of building a new cell from raw amino acids, the existing cell duplicates itself — copying its DNA, organelles, and membrane. The result is a fully functional copy produced far faster than assembling one molecule at a time.

Frequently Asked Questions

helpWhat is the difference between Prototype and using a copy constructor?

A copy constructor requires the client to know the concrete class being copied. Prototype uses a clone() method on the object itself, so the client can copy objects through an interface without knowing their concrete type. This supports polymorphic cloning.

helpDoes Prototype create deep copies or shallow copies?

The pattern itself does not prescribe either — it depends on your implementation. Shallow copies share references to nested objects, which can cause bugs. Most production uses require deep copying. In TypeScript, structuredClone() handles deep copies for plain objects.