Knowledge Guide
HomeOO & Low-Level DesignCreational

Prototype Pattern

The Prototype Design Pattern is a creational design pattern that involves creating objects based on a template of an existing object through cloning. This pattern is used when creating a new instance of a class is more expensive or complex than copying an existing instance. It's particularly useful in scenarios where objects are dynamically created and the types of objects are not known until runtime.

Real-Life Example

Prototype Pattern - Example
Prototype Pattern - Example

The diagram illustrates the Prototype Design Pattern using the example of robots.

The idea here is that instead of building each of these three robots from scratch, which can be time-consuming and expensive, they are cloned from the prototype robot. This cloning process copies the features and characteristics of the prototype, and potentially, each clone can then be customized further if needed. For example, one might be programmed for talking, another for lifting, and the third for complex calculations.

This is the core concept of the Prototype Pattern: creating new objects by copying an existing object (the prototype) rather than creating new objects from scratch, which can be a more efficient way to produce objects that share similar characteristics or configurations.

Structure of Prototype Pattern

Typically, the prototype pattern includes:

Prototype Pattern - Class Diagram
Prototype Pattern - Class Diagram

How It Works

Implementation of Prototype Pattern

Let's look at a practical application of the prototype pattern: creating customizable cars. Consider yourself at a vehicle dealership, and you come upon a basic car model. The dealer efficiently clones the basic model and tailors it to your specific preferences, eliminating the need for you to begin from scratch.

Let's look at the pseudocode and implementation of this example in C++, Java, Python, and JavaScript.

Pseudocode

The pseudocode for the Vehicle dealership example will be as follows:

ABSTRACT CLASS Car: PROTECTED model, color ABSTRACT METHOD customize(color, accessories) METHOD clone(): TRY RETURN a copy of this object CATCH cloning not supported error PRINT error RETURN null ENDTRY ENDCLASS CLASS BasicCar EXTENDS Car: CONSTRUCTOR: SET model = "Basic" SET color = "White" ENDCONSTRUCTOR OVERRIDE METHOD customize(color, accessories): SET this.color = color PRINT "Car customized with color:", color, "and accessories:", accessories ENDMETHOD ENDCLASS MAIN: DECLARE basicCar = NEW BasicCar DECLARE customerCar = a clone of basicCar CALL customerCar.customize("Red", "Sunroof") ENDMAIN

Simply said, the code exemplifies the Prototype design pattern, with the original BasicCarserving as the prototype and the customerCar serving as the cloned instance that may be modified in accordance with the needs of the user.

java
// Prototype
abstract class Car implements Cloneable {

  protected String model;
  protected String color;

  public abstract void customize(String color, String accessories);

  @Override
  public Object clone() {
    Object clone;
    try {
      clone = super.clone();
    } catch (CloneNotSupportedException e) {
      e.printStackTrace();
      return null;
    }
    return clone;
  }
}

// Concrete Car
class BasicCar extends Car {

  public BasicCar() {
    model = "Basic";
    color = "White";
  }

  @Override
  public void customize(String color, String accessories) {
    this.color = color;
    System.out.println(
      "Car customized with color: " + color + " and accessories: " + accessories
    );
  }
}

// Client Code
public class Solution {

  public static void main(String[] args) {
    Car basicCar = new BasicCar();
    Car customerCar = (Car) basicCar.clone();

    customerCar.customize("Red", "Sunroof");
  }
}

Applications of Prototype Pattern

Pros and Cons

ProsCons
Easy Cloning: Quickly create new objects by copying existing ones.Complex Cloning: Cloning complex objects with interconnected parts can be difficult.
Less Code: Reduces the amount of code needed for object creation.Hidden Issues: Cloning can lead to problems if not all parts of the object are correctly copied.
Flexible Creation: Allows for creating objects dynamically at runtime.Management of Clones: Keeping track of clones and managing their updates can be challenging.
Efficiency: More efficient in cases where object initialization is resource-intensive.Shallow vs Deep Copy: Managing shallow versus deep copies can introduce bugs if not handled carefully.
Prototyping: Facilitates experimentation and prototyping with different object configurations.Resource Handling: Issues can arise if cloned objects hold resources that should not or cannot be duplicated.

Summary

The Prototype Design Pattern is useful for scenarios where object creation is more expensive or complex than duplicating an existing object. It offers a flexible solution to dynamic object creation but requires careful implementation to handle complex cloning scenarios effectively.

🤖 Don't fully get this? Learn it with Claude

Stuck on Prototype Pattern? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Prototype Pattern** (OO & Low-Level Design) and want to truly understand it. Explain Prototype Pattern from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Prototype Pattern** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Prototype Pattern** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Prototype Pattern** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes