VOOZH about

URL: https://www.geeksforgeeks.org/system-design/prototype-design-pattern/

⇱ Prototype Design Pattern - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Prototype Design Pattern

Last Updated : 17 Apr, 2026

The prototype pattern is a creational design pattern which is required when object creation is a time-consuming, and costly operation, so we create objects with the existing object itself to by copying the existing ones.

  • The cloned object can modify only the required properties, avoiding unnecessary changes to the original object.
  • This approach saves time and system resources, especially when object creation is expensive or complex.
  • The clone() method is a simple way to implement this pattern, but the cloning logic depends on the specific business requirements.

Example: Consider a Game Character System where creating a character involves loading graphics, skills, and configurations.

👁 character
Prototype pattern

Instead of creating a new character from scratch every time:

  • Clone an existing character.
  • Modify attributes like name, weapon, or level.
  • Now use the new customized character.

Real Life Software Examples

Many modern applications use the Builder Pattern to improve flexibility and maintainability in object creation.

1. Document and Content Management Systems

Systems that manage, store, and reuse digital documents and templates efficiently.

  • Existing document templates can be cloned to create new documents quickly.
  • Users can modify layout, fonts, or content without recreating the template from scratch.

2. Game Engines

Software frameworks used to create and manage game objects, characters, and environments.

  • Complex characters or environment objects are cloned instead of being reinitialized.
  • This improves performance by avoiding costly setup during gameplay or runtime.

Components

The pattern is built using multiple components that ensure flexible and controlled object creation.

👁 class_diagram_of_prototype_design_pattern
Components
  • Prototype Interface / Abstract Class: Defines the clone() method and sets a standard for all objects that can be cloned.
  • Concrete Prototype: Implements the prototype interface or extends the abstract class to provide actual cloning behavior.
  • Client: Uses the prototype to create new objects by calling the clone() method.
  • Clone Method: Specifies how an object is copied and is implemented by concrete prototypes.

Features

The Prototype pattern enables efficient object creation through duplication.

  • Reduces the need for expensive object initialization
  • Uses a clone() method to duplicate objects
  • Allows modification of selected properties after cloning

Uses

The Prototype pattern is useful when object creation is costly or complex.

  • When object creation is time-consuming or resource-intensive
  • When many similar objects are required with slight variations
  • When object initialization involves database calls or heavy computations

Implementation Example

Understand how prototype design pattern work with the help of an example:

Imagine you're working on a drawing application, and you need to create and manipulate various shapes. Each shape might have different attributes like color or size. Creating a new shape class for every variation becomes tedious. Also, dynamically adding or removing shapes during runtime can be challenging.

Solving this problem with the help of Prototype Design Pattern:

  • The Prototype Design Pattern helps in managing variations of shapes efficiently, promoting flexibility in shape creation, and simplifying the process of adding or removing shapes at runtime.
  • The Prototype Design Pattern addresses this by introducing a prototype interface (Shape) that declares common methods for cloning and drawing shapes.
  • Concrete prototypes like Circle implement this interface, providing their unique cloning logic.
  • The ShapeClient acts as a user, utilizing the prototype to create new shapes.
👁 class_diagram_of_above_example
Class Diagram

1. Prototype Interface (Shape):

Defines the blueprint for cloning and drawing shapes.

  • Declares common methods such as clone() and draw().

2. Concrete Prototype (Circle):

Implements the Shape interface and represents a specific shape.

  • Contains properties such as color, initialized through a constructor.
  • Implements the clone() method to create a copy with the same properties.
  • Implements the draw() method to define how the circle is rendere

3. Client (ShapeClient):

Uses the prototype to create new shape objects.

  • Holds a reference to a Shape prototype.
  • Accepts the prototype through its constructor.
  • Creates new shapes using the prototype’s clone() method.

4. Main Class (PrototypeExample):

Creates a concrete prototype (circlePrototype) with predefined properties.

  • Passes the prototype to ShapeClient.
  • Uses the client to create a new shape by cloning the prototype.
  • Calls the draw() method on the cloned object to display the result.

Output
Drawing a red circle.

Advantages

The Prototype pattern improves performance and flexibility in object creation.

  • Simplifies object creation process.
  • Reduces subclassing for different object configurations.
  • Allows dynamic addition or removal of object types at runtime.
  • Promotes flexibility in cloning and modifying objects.

Disadvantages

Although efficient, the Prototype pattern introduces certain challenges.

  • Cloning complex objects can be difficult.
  • Deep copy implementation can be complicated.
  • Requires careful handling of references to avoid shared state issues.
  • Every class must implement cloning logic properly.
Comment
Article Tags:

Explore