codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎭

Design Patterns

10 / 100 topics
5Introduction to Creational Patterns6Singleton Pattern7Factory Method Pattern8Abstract Factory Pattern9Builder Pattern10Prototype Pattern31Practical Exercises for Creational Patterns
Tutorials/Design Patterns/Prototype Pattern
🎭Design Patterns

Prototype Pattern

Updated 2026-05-15
10 min read

Prototype Pattern

Introduction

In software design, the Prototype Pattern is a creational pattern that allows you to create new objects by copying existing ones. This pattern is particularly useful when creating an object is complex or resource-intensive, and you want to avoid the overhead of repeatedly initializing objects from scratch.

The Prototype Pattern promotes the idea of cloning objects rather than creating them from scratch. This approach can be more efficient and flexible, especially when dealing with objects that have a large number of properties or require complex initialization logic.

Concept

The core concept behind the Prototype Pattern is to create a prototype object that serves as a template for other objects. When you need to create a new object, you clone the prototype and modify it as needed. This pattern leverages the built-in cloning capabilities provided by many programming languages, such as JavaScript's Object.create() method or Python's copy module.

Key Benefits

  1. Efficiency: Cloning an existing object is generally faster than creating a new one from scratch.
  2. Flexibility: You can easily modify the cloned object without affecting the original prototype.
  3. Simplicity: Reduces the complexity of object creation by reusing existing objects.

Key Considerations

  1. Deep vs Shallow Cloning: Be aware of whether you need a deep or shallow clone. A shallow clone copies only the top-level properties, while a deep clone recursively copies all nested objects.
  2. Complexity in Initialization: If your objects have complex initialization logic, consider using a factory method to handle cloning and initialization.

Examples

Let's explore some practical examples of how to implement the Prototype Pattern in different programming languages.

Example 1: JavaScript

In JavaScript, you can use the Object.create() method to clone an object. Here’s a simple example:

JavaScript
1// Define a prototype object
2const vehiclePrototype = {
3init(type, wheels) {
4 this.type = type;
5 this.wheels = wheels;
6},
7displayInfo() {
8 console.log(`Vehicle Type: ${this.type}, Wheels: ${this.wheels}`);
9}
10};
11
12// Create a new car by cloning the prototype
13const car = Object.create(vehiclePrototype);
14car.init('Car', 4);
15
16// Display information about the car
17car.displayInfo(); // Output: Vehicle Type: Car, Wheels: 4
18
19// Create a new motorcycle by cloning the prototype
20const motorcycle = Object.create(vehiclePrototype);
21motorcycle.init('Motorcycle', 2);
22
23// Display information about the motorcycle
24motorcycle.displayInfo(); // Output: Vehicle Type: Motorcycle, Wheels: 2

Example 2: Python

In Python, you can use the copy module to clone objects. Here’s how you can implement the Prototype Pattern:

Python
1import copy
2
3# Define a prototype class
4class VehiclePrototype:
5 def __init__(self, type, wheels):
6 self.type = type
7 self.wheels = wheels
8
9 def display_info(self):
10 print(f"Vehicle Type: {self.type}, Wheels: {self.wheels}")
11
12# Create a new car by cloning the prototype
13car_prototype = VehiclePrototype('Car', 4)
14car = copy.deepcopy(car_prototype)
15car.type = 'Sedan'
16
17# Display information about the car
18car.display_info() # Output: Vehicle Type: Sedan, Wheels: 4
19
20# Create a new motorcycle by cloning the prototype
21motorcycle_prototype = VehiclePrototype('Motorcycle', 2)
22motorcycle = copy.deepcopy(motorcycle_prototype)
23motorcycle.type = 'Sport'
24
25# Display information about the motorcycle
26motorcycle.display_info() # Output: Vehicle Type: Sport, Wheels: 2

Example 3: Java

In Java, you can implement cloning by implementing the Cloneable interface and overriding the clone() method. Here’s an example:

Java
1// Define a prototype class
2class VehiclePrototype implements Cloneable {
3 private String type;
4 private int wheels;
5
6 public VehiclePrototype(String type, int wheels) {
7 this.type = type;
8 this.wheels = wheels;
9 }
10
11 public void displayInfo() {
12 System.out.println("Vehicle Type: " + type + ", Wheels: " + wheels);
13 }
14
15 @Override
16 protected Object clone() throws CloneNotSupportedException {
17 return super.clone();
18 }
19}
20
21public class PrototypePatternExample {
22 public static void main(String[] args) {
23 try {
24 // Create a new car by cloning the prototype
25 VehiclePrototype carPrototype = new VehiclePrototype("Car", 4);
26 VehiclePrototype car = (VehiclePrototype) carPrototype.clone();
27 car.setType("Sedan");
28
29 // Display information about the car
30 car.displayInfo(); // Output: Vehicle Type: Sedan, Wheels: 4
31
32 // Create a new motorcycle by cloning the prototype
33 VehiclePrototype motorcyclePrototype = new VehiclePrototype("Motorcycle", 2);
34 VehiclePrototype motorcycle = (VehiclePrototype) motorcyclePrototype.clone();
35 motorcycle.setType("Sport");
36
37 // Display information about the motorcycle
38 motorcycle.displayInfo(); // Output: Vehicle Type: Sport, Wheels: 2
39 } catch (CloneNotSupportedException e) {
40 e.printStackTrace();
41 }
42 }
43}

What's Next?

In this tutorial, we explored the Prototype Pattern and how it can be used to create objects by cloning a prototype object. In the next section, we will delve into Structural Patterns, which focus on organizing classes and objects in a way that simplifies their composition.

Stay tuned for more insights into software design patterns!


PreviousBuilder PatternNext Introduction to Structural Patterns

Recommended Gear

Builder PatternIntroduction to Structural Patterns