Fundamentals 14 min read

Prototype Pattern: Definition, UML Diagram, Implementation, Advantages, Disadvantages, and Use Cases

This article explains the Prototype design pattern, covering its definition, UML class diagram, Java implementation with shallow and deep copy examples, advantages and drawbacks, comparisons with Factory Method and Singleton patterns, and practical scenarios where cloning objects provides an efficient alternative to costly object creation.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Prototype Pattern: Definition, UML Diagram, Implementation, Advantages, Disadvantages, and Use Cases

Why study design patterns? They improve the ability to read framework source code, enhance design skills for complex business logic, and lay a solid foundation for interviews and career advancement.

Prototype Pattern (Prototype Pattern)

The Prototype pattern offers an optimal way to create objects by cloning existing instances, solving object duplication problems and fitting scenarios such as building complex object graphs, snapshots, and restoration.

Author Mike Chen presents this as part of the "Deep Dive into Design Patterns" series (article 11). The full PDF contains over 20,000 words, 50+ diagrams, and detailed coverage of 7 principles and 23 patterns.

01. Definition of Prototype Pattern

The Prototype pattern creates new objects by cloning existing ones, avoiding frequent instantiation and belonging to the creational patterns.

In simple terms, a prototype object serves as a template; cloning it yields multiple objects with identical properties and behavior.

Example: Sun Wukong’s "Clone Technique" creates many identical copies, analogous to the Prototype pattern.

02. UML Class Diagram

The three key roles are:

Client : uses the concrete prototype’s clone() method to obtain new objects.

Prototype (Abstract Prototype) : an abstract class or interface that declares the clone() method.

ConcretePrototype : implements clone() and represents the cloneable object.

In Java, the Cloneable interface enables cloning, though it is not mandatory; other techniques can be used.

03. Two Implementations: Shallow Copy and Deep Copy

Shallow copy creates a new object whose fields reference the same sub‑objects as the original; only primitive fields are duplicated.

Object.clone() // copies the object itself, not internal arrays or referenced objects

Deep copy clones the entire object graph, producing independent sub‑objects.

// Example of deep copy logic

Deep copy is slower and consumes more memory but guarantees that cloned objects do not affect each other.

04. Implementation Example

We clone business cards into a personal card library.

Concrete Prototype Class

public class BusinessCard implements Cloneable {
    private String name;
    private String company;
    public BusinessCard(){
        System.out.println("Executing BusinessCard constructor");
    }
    public void setName(String name) { this.name = name; }
    public void setCompany(String company) { this.company = company; }
    @Override
    public BusinessCard clone() {
        BusinessCard businessCard = null;
        try { businessCard = (BusinessCard) super.clone(); }
        catch (CloneNotSupportedException e) { e.printStackTrace(); }
        return businessCard;
    }
    public void show() {
        System.out.println("name:" + name);
        System.out.println("company:" + company);
    }
}

The class implements Cloneable ; overriding clone() enables copying.

Client Code

public class Client {
    public static void main(String[] args) {
        BusinessCard businessCard = new BusinessCard();
        businessCard.setName("Qian San");
        businessCard.setCompany("Alibaba");
        // Clone cards
        BusinessCard cloneCard1 = businessCard.clone();
        cloneCard1.setName("Zhao Si");
        cloneCard1.setCompany("Baidu");
        BusinessCard cloneCard2 = businessCard.clone();
        cloneCard2.setName("Sun Wu");
        cloneCard2.setCompany("Tencent");
        businessCard.show();
        cloneCard1.show();
        cloneCard2.show();
    }
}

Output demonstrates that the constructor runs only once; cloned objects share no constructor execution.

Executing BusinessCard constructor
name:Qian San
company:Alibaba
name:Zhao Si
company:Baidu
name:Sun Wu
company:Tencent

Shallow vs. Deep Copy with Reference Types

When fields are reference types (e.g., a Company object), shallow copying leads to shared references.

public class DeepBusinessCard implements Cloneable {
    private String name;
    private Company company = new Company();
    // setters omitted for brevity
    @Override
    public DeepBusinessCard clone() {
        DeepBusinessCard businessCard = null;
        try { businessCard = (DeepBusinessCard) super.clone(); }
        catch (CloneNotSupportedException e) { e.printStackTrace(); }
        return businessCard; // shallow copy of company
    }
}

Running the shallow‑copy version shows all clones referencing the last set Company (“Tencent”).

To achieve deep copy, Company also implements Cloneable and its clone() is invoked inside DeepBusinessCard.clone() :

public class Company implements Cloneable {
    private String name;
    private String address;
    @Override
    public Company clone() {
        try { return (Company) super.clone(); }
        catch (CloneNotSupportedException e) { e.printStackTrace(); return null; }
    }
}

public class DeepBusinessCard implements Cloneable {
    private String name;
    private Company company = new Company();
    @Override
    public DeepBusinessCard clone() {
        DeepBusinessCard copy = null;
        try { copy = (DeepBusinessCard) super.clone(); }
        catch (CloneNotSupportedException e) { e.printStackTrace(); }
        copy.company = this.company.clone(); // deep copy of company
        return copy;
    }
}

Output now shows each clone retaining its own Company data.

name:Qian San
company:Alibaba-address-Beijing Wangjing
name:Zhao Si
company:Baidu-address-Beijing Xierqi
name:Sun Wu
company:Tencent-address-Beijing Zhongguancun

05. Advantages and Disadvantages

Hides cloning details from the client, reducing coupling.

Avoids constructor constraints.

Allows dynamic addition or removal of product classes.

Simplifies object creation structures.

Requires implementing Cloneable .

Modifying existing classes to support cloning can violate the Open‑Closed Principle.

Deep copy implementations can be complex.

06. Use Cases

When creating a large number of similar objects.

When object creation is expensive due to complex initialization.

When objects need to be created or modified dynamically at runtime.

07. Comparison with Factory Method Pattern

The Factory Method creates objects via a factory method, suitable for different types or complex creation logic, while the Prototype creates objects by cloning existing instances.

08. Comparison with Singleton Pattern

Singleton ensures a single instance globally; Prototype focuses on producing many independent copies.

Summary

Through this article we have covered the Prototype pattern’s concept, UML diagram, Java implementation (both shallow and deep copy), advantages, disadvantages, and typical scenarios. Understanding when to use shallow versus deep copying enables developers to create objects efficiently, improve performance, and maintain clean, reusable code.

design patternsJavadeep copyObject Cloningshallow copyPrototype Pattern
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.