Jinal Desai

My thoughts and learnings

Demystifying Design Patterns: Prototype Design Pattern

Demystifying Design Patterns: Prototype Design Pattern
  1. Demystifying Design Patterns: Singleton Design Pattern
  2. Demystifying Design Patterns: Factory Method Design Pattern
  3. Demystifying Design Patterns: Abstract Factory Design Pattern
  4. Demystifying Design Patterns: Builder Design Pattern
  5. Demystifying Design Patterns: Prototype Design Pattern
  6. Demystifying Design Patterns: Adapter Design Pattern
  7. Demystifying Design Patterns: Bridge Design Pattern
  8. Demystifying Design Patterns: Composite Design Pattern
  9. Demystifying Design Patterns: Decorator Design Pattern
  10. Demystifying Design Patterns: Proxy Design Pattern
  11. Demystifying Design Patterns: Observer Design Pattern
  12. Demystifying Design Patterns: Strategy Design Pattern
  13. Demystifying Design Patterns: Command Design Pattern
  14. Demystifying Design Patterns: State Design Pattern
  15. Demystifying Design Patterns: Chain of Responsibility Design Pattern
  16. Demystifying Design Patterns: Visitor Design Pattern
  17. Demystifying Design Patterns: Template Method Design Pattern

Welcome to the fifth installment of our “Demystifying Design Patterns” series. In this article, we’ll delve into the Prototype Design Pattern—a creational pattern that deals with object cloning. We will explore the key concepts of the pattern, discuss the differences between shallow and deep copy, examine implementation in languages without built-in cloning support, and provide real-life examples in Java, C#, and Python. 

Introduction

The Prototype Design Pattern falls under the category of creational design patterns and focuses on object cloning. It provides a mechanism for creating new objects by copying an existing object, known as the prototype. This pattern is particularly useful when object creation is more efficient through cloning than through traditional instantiation.

Cloning Objects with the Prototype Pattern

At the core of the Prototype pattern are two key components:

– Prototype: The object that serves as the template for cloning. It defines a method for cloning itself.

– ConcretePrototype: Subclasses of the prototype that implement the cloning method to create new instances.

Shallow vs. Deep Copy in Prototype

There are two main approaches to object copying in the Prototype pattern: shallow copy and deep copy.

– Shallow Copy: Involves copying only the top-level object, leaving the internal references as they are. Changes to internal objects will affect both the original and the clone.

– Deep Copy: Copies the top-level object and all objects referenced within it recursively. This creates entirely independent copies, so changes to internal objects won’t affect the original.

The choice between shallow and deep copy depends on the specific requirements of your application.

Implementing Prototype in Languages without Built-in Cloning

Some programming languages, like Java and C#, provide built-in support for object cloning through interfaces like `Cloneable` and `ICloneable`. However, in languages that lack built-in cloning support, you can implement the Prototype pattern manually by defining custom cloning methods.

Use Cases for the Prototype Pattern

The Prototype pattern is beneficial in various scenarios:

– Efficient Object Creation: When creating objects is resource-intensive, cloning can be faster and more efficient.

– Complex Object Initialization: When objects have complex initialization processes, cloning can simplify object creation.

– Immutable Objects: In situations where immutability is crucial, cloning ensures that objects remain unmodifiable after creation.

– Configuration Management: For managing configurations or instances with slight variations, like different types of documents in a document editing application.

Real-Life Examples

Example 1: Document Cloning

Consider a document editing application that uses the Prototype pattern to clone documents:

example-1-prototype-design-pattern

Java Example:

class Document implements Cloneable {
    private String content;

    public Document(String content) {
        this.content = content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getContent() {
        return content;
    }

    @Override
    public Document clone() throws CloneNotSupportedException {
        return (Document) super.clone();
    }
}

C# Example:

class Document : ICloneable
{
    private string content;

    public Document(string content)
    {
        this.content = content;
    }

    public void SetContent(string content)
    {
        this.content = content;
    }

    public string GetContent()
    {
        return content;
    }

    public object Clone()
    {
        return MemberwiseClone();
    }
}

Python Example:

import copy

class Document:
    def __init__(self, content):
        self.content = content

    def set_content(self, content):
        self.content = content

    def get_content(self):
        return self.content

    def clone(self):
        return copy.deepcopy(self)

Example 2: Game Character Cloning

In a video game, you can use the Prototype pattern to clone game characters with different attributes:

example-2-prototype-design-pattern

Java Example:

class GameCharacter implements Cloneable {
    private String name;
    private String weapon;

    public GameCharacter(String name, String weapon) {
        this.name = name;
        this.weapon = weapon;
    }

    public void setWeapon(String weapon) {
        this.weapon = weapon;
    }

    public String getWeapon() {
        return weapon;
    }

    @Override
    public GameCharacter clone() throws CloneNotSupportedException {
        return (GameCharacter) super.clone();
    }
}

C# Example:

class GameCharacter : ICloneable
{
    private string name;
    private string weapon;

    public GameCharacter(string name, string weapon)
    {
        this.name = name;
        this.weapon = weapon;
    }

    public void SetWeapon(string weapon)
    {
        this.weapon = weapon;
    }

    public string GetWeapon()
    {
        return weapon;
    }

    public object Clone()
    {
        return MemberwiseClone();
    }
}

Python Example:

import copy

class GameCharacter:
    def __init__(self, name, weapon):
        self.name = name
        self.weapon = weapon

    def set_weapon(self, weapon):
        self.weapon = weapon

    def get_weapon(self):
        return self.weapon

    def clone(self):
        return copy.deepcopy(self)

Conclusion

The Prototype Design Pattern is a valuable tool for efficient object creation through cloning. It provides flexibility in object copying by allowing you to choose between shallow and deep copy strategies. Whether you’re working with document editing applications, video games, or any other domain, the Prototype pattern simplifies object creation and promotes code reusability.

In the next article of our series, we will continue our exploration of design patterns. Stay tuned for more insights and demystification!

Leave a Reply

Your email address will not be published. Required fields are marked *