TUTORIAL SERIES

Design Patterns in Python: Prototype

The Shallow and Deep

Amir Lavasani
7 min readOct 31, 2023

Have you encountered recurring coding challenges? Imagine having a toolbox of tried-and-true solutions readily available. That’s precisely what design patterns provide. In this series, we’ll explore what these patterns are and how they can elevate your coding skills.

Understanding the Prototype Pattern

What is the Prototype Design Pattern?

The Prototype design pattern is one of the creational design patterns. Its primary goal is to create new objects by copying an existing object, known as the prototype. This pattern is particularly useful when object creation is complex or resource-intensive. By cloning an existing object, we can achieve efficient object creation while customizing the copied object’s properties as needed.

How Does the Prototype Pattern Work?

At its core, the Prototype pattern relies on the concept of cloning. Instead of creating new objects from scratch, we create copies of existing objects, known as prototypes. These prototypes serve as templates, allowing us to replicate their structure and attributes. When a new object is needed, we clone the prototype, saving both time and resources.

Cloning: The Key Concept

Cloning involves duplicating the state of an existing object, resulting in a new object with the same properties. The Prototype pattern typically provides two types of cloning: shallow copy and deep copy.

Dall-E generated image with the following concept: An image of a mirror with an object in front and its clone appearing behind it, reflecting the idea of creating identical objects through cloning, symbolizing the Prototype pattern

Shallow vs. Deep Copy

A shallow copy replicates the top-level structure of an object but does not create copies of its nested objects. In contrast, a deep copy replicates both the top-level structure and all nested objects, creating entirely independent copies.

Shallow copying is often faster and more straightforward, but it can lead to shared references within nested objects. Deep copying, on the other hand, ensures complete independence between the original and cloned objects but can be more complex and resource-intensive.

Terminology and Key Components

we’ll explore two types of prototype implementations: Basic Implementation and Prototype Registry Implementation.

Basic Implementation

  1. Prototype Interface: Declares cloning methods, typically a single clone() method.
  2. Concrete Prototype: Implements the cloning method, copying data from the original object to the clone.
  3. Client: Produces object copies by interacting with Concrete Prototypes through the Prototype Interface.
Prototype design pattern registry implementation.
Prototype design pattern basic implementation. Image from refactoring.guru

Prototype Registry Implementation

  1. Prototype Registry: Central repository for frequently-used prototypes, facilitating easy access. It stores pre-built objects ready for cloning, often as a name-to-prototype hash map or a more complex registry for advanced search criteria.
Prototype design pattern registry implementation.
Prototype design pattern registry implementation. Image from refactoring.guru

Implementing the Prototype Pattern in Python

Python’s Built-in Support

Python offers seamless support for implementing the Prototype pattern, thanks to its built-in capabilities for object cloning. The copy module provides functions for both shallow and deep copying of objects, streamlining the process of creating prototypes and their clones.

Basic Implementation

To apply the Prototype pattern, we begin by defining a custom prototype class that encapsulates the structure and attributes of the object. Cloning becomes straightforward by copying the prototype’s state.

import copy

class Prototype:
def __init__(self):
self.data = []

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

# Create a prototype
prototype_instance = Prototype()

# Clone the prototype
clone_instance = prototype_instance.clone()

Prototype Registry Implementation

The Prototype Registry acts as a centralized repository for frequently-used prototypes, simplifying their storage and retrieval. It typically relies on name-to-prototype mapping for efficient access and cloning.

import copy

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

class ConcretePrototypeA(Prototype):
def __init__(self, data):
self.data = data

class ConcretePrototypeB(Prototype):
def __init__(self, data):
self.data = data

class PrototypeRegistry:
def __init__(self):
self.prototypes = {}

def add_prototype(self, name, prototype):
self.prototypes[name] = prototype

def get_prototype(self, name):
if name in self.prototypes:
return self.prototypes[name].clone()
else:
raise ValueError(f"Prototype '{name}' not found.")

# Create prototype instances
prototype_a = ConcretePrototypeA("Prototype A Data")
prototype_b = ConcretePrototypeB("Prototype B Data")

# Create and populate the Prototype Registry
registry = PrototypeRegistry()
registry.add_prototype("PrototypeA", prototype_a)
registry.add_prototype("PrototypeB", prototype_b)

# Clone prototypes from the registry
cloned_prototype_a = registry.get_prototype("PrototypeA")
cloned_prototype_b = registry.get_prototype("PrototypeB")

# Verify cloned data
print(cloned_prototype_a.data) # Output: Prototype A Data
print(cloned_prototype_b.data) # Output: Prototype B Data

GitHub Repo 🎉

Explore all code examples and design pattern implementations on GitHub!

Real-world Use Cases: Prototype in Action

  1. Document Cloning: Clone documents with different structures while preserving formatting in document processing applications.
  2. Database Connection Pooling: Efficiently manage database connections by using prototypes to create and reuse connection instances.
  3. Configuration Handling: Manage software configurations by cloning a base prototype and adapting it for different environments.
  4. Machine Learning Initialization: Initialize machine learning models, including network architectures and weights, using prototypes to save time during setup.
  5. Web Page Templates: Manage web page templates in content management systems (CMS) using prototypes. Clone and tailor templates for specific content.
Dall-E generated image with the following concept: An image of a mirror with an object in front and its clone appearing behind it, reflecting the idea of creating identical objects through cloning, symbolizing the Prototype pattern

Advantages of Using the Prototype Pattern

  1. Reusability: The Prototype pattern offers reusability by cloning existing prototypes with pre-configured attributes and structures, saving development time and ensuring consistency.
  2. Customizable Object Creation: It enables easy customization of object creation by cloning and modifying prototypes, providing flexibility for specific requirements, especially useful for similar object variations.
  3. Improved Performance: The prototype pattern improves performance by avoiding resource-intensive object creation, as cloning existing objects is often faster and more resource-efficient than creating new ones from scratch, especially in scenarios with performance bottlenecks.

Considerations and Potential Drawbacks

  1. Complexity: While simplifying object creation, the Prototype pattern can introduce complexity, particularly with complex object structures, prototype registration, deep copying, and object consistency management, challenging as the system scales.
  2. Deep Copy Challenges: Implementing deep copying for complete object independence can be complex. Careful consideration of object relationships, avoidance of circular references, and vigilant development are necessary to prevent unintended side effects.
  3. Limited Applicability: The Prototype pattern may not suit all scenarios. It excels in resource-intensive or highly customizable object creation situations but may introduce unnecessary overhead in simple and straightforward cases.

Best Practices for Using the Prototype Pattern

  1. Keep Prototype Registration Centralized: To maintain consistency and manage prototypes efficiently, it’s a good practice to centralize prototype registration. This ensures that all prototypes are registered in one place, making it easier to access and clone them.
  2. Clone Objects Carefully: When implementing the Prototype pattern, pay close attention to object cloning. Ensure that deep copying is correctly handled to avoid unintended sharing of references within nested objects.
Dall-E generated image with the following concept: futuristic conscious robot cloning itself, emphasizing the idea of creating copies with identical properties

Prototype Relations with Other Patterns

Factory Method, Abstract Factory, and Builder

  • Factory Method Evolution: Many designs begin with the Factory Method for simplicity and customization via subclasses. They may later evolve towards Abstract Factory, Prototype, or Builder for increased flexibility and complexity.
  • Abstract Factory Composition: Abstract Factory classes can rely on Factory Methods, but they can also incorporate Prototype to compose methods, enhancing their capabilities.

Command and Prototype

  • Command History: Prototype proves valuable when saving copies of Commands into a history, aiding in undo and redo functionality.

Composite and Decorator

  • Complex Structures: Designs heavily utilizing Composite and Decorator can benefit from Prototype. It allows the cloning of intricate structures instead of reconstructing them from scratch, enhancing efficiency.

Prototype and Inheritance

  • Inheritance Drawbacks: The prototype avoids inheritance-related drawbacks, providing flexibility without inheritance’s complexities. It does, however, involve complex initialization of cloned objects.

Prototype and Memento

  • Simpler Alternative: Prototype can serve as a simpler alternative to Memento, especially when storing straightforward object states without extensive links to external resources.

Singleton Patterns

  • Singleton Implementations: Abstract Factories, Builders, and Prototypes can all be implemented as Singletons when required, offering centralized control and resource management.

Conclusion

In this comprehensive exploration of the Prototype design pattern, we have learned how it enables efficient object creation through cloning. We discussed its use cases, advantages, and disadvantages, and provided practical examples of its implementation in Python.

When considering the Prototype pattern for your projects, remember that it excels in scenarios where object creation is resource-intensive or when customization is required. Embracing design patterns like Prototype empowers you to build more maintainable, flexible, and efficient software solutions.

Hope you enjoyed the Prototype pattern exploration 🙌 Happy coding! 👨‍💻

Next on the Series 🚀

Read More 📜

The Series 🧭

References

  1. Design Patterns: Elements of Reusable Object-Oriented Software (Book)
  2. refactoring.guru Prototype
  3. Head First Design Patterns (Book)
  4. Prototype Method — Python Design Patterns
  5. The Prototype Design Pattern in Python

--

--

Amir Lavasani

I delve into machine learning 🤖 and software architecture 🏰 to enhance my expertise while sharing insights with Medium readers. 📃