WorkspaceLow-Level Design
← Course overview

Course navigation

On this page

Low-Level Design / Creational Patterns

pattern · 12 MIN READ

Prototype

Prototype creates a new object by copying an existing configured object. The copy contract must identify which parts are shared and which mutable parts become independent.

Reading completion is independent of quizzes and challenges.

What you’ll understand

  • Prototype creates a new object by copying an existing configured object. The copy contract must identify which parts are shared and which mutable parts become independent.
  • Clone the nested mutable tag list as well as the outer document record.
  • Evaluate the tradeoff: Deep copying an arbitrary graph is expensive and may mishandle external resources or cycles. Copy only the state the clone contract owns and deliberately share immutable data.

Useful first: Classes and Objects

The idea and when to recognize it

Prototype creates a new object by copying an existing configured object. The copy contract must identify which parts are shared and which mutable parts become independent.

  • Requirement: A document template contains tags. A new document should start with the template tags but own future edits.
  • Change to anticipate: Add a tag to a cloned document without changing the reusable template.
  • Design goal: Clone the nested mutable tag list as well as the outer document record.

Start with the concrete problem

A document template contains tags. A new document should start with the template tags but own future edits.

Add a tag to a cloned document without changing the reusable template.

Direct implementationRevised design
A shallow outer copy leaves the nested tag list shared between template and document.Clone the nested mutable tag list as well as the outer document record.
The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design.Deep copying an arbitrary graph is expensive and may mishandle external resources or cycles. Copy only the state the clone contract owns and deliberately share immutable data.

Responsibilities and contracts

Clone the nested mutable tag list as well as the outer document record.

  • Template: Supplies configured initial state. Public operations: clone()
  • Document: Owns mutable copied tags. Public operations: addTag(value)

Read the design

Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.

Preparing the design walkthrough…

Follow the interaction

Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.

Preparing the design walkthrough…

Implementation and expected output

Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.

Preparing the design walkthrough…

Try the idea in another setting

A game can clone a configured enemy prototype while allocating an independent inventory.

  • Name the new client contract before adapting the example.
  • Identify which state belongs to each object and who controls its lifetime.
  • Write one normal case and one boundary case before extending the design.

Tradeoffs and interview takeaways

Deep copying an arbitrary graph is expensive and may mishandle external resources or cycles. Copy only the state the clone contract owns and deliberately share immutable data.

Common mistake: A shallow outer copy leaves the nested tag list shared between template and document.

  • Explain the choice: Clone the nested mutable tag list as well as the outer document record.
  • Challenge your choice: Deep copying an arbitrary graph is expensive and may mishandle external resources or cycles. Copy only the state the clone contract owns and deliberately share immutable data.
  • Describe a requirement that would make the simpler implementation sufficient.

Design challenge and knowledge check

Preparing the design walkthrough…

Make it your own

Saved on this device · 0/20,000

Reading completion is independent of quizzes and challenges.