The “GOF (Gang Of Four) Design Patterns” is a framework that has been used in various OOP (Object Oriented Programming) environments for a long time. This article describes the Prototype Pattern (which adds an abstraction layer over multiple creational pattern implementations). The Prototype Pattern is part of what is commonly called “Creational Patterns“.
In simple terms, this pattern is good when creating new objects that will require more resources than what may be available. Resources can be saved by just creating a copy of any existing object that is already in memory.
Below, I have presented one Class Model diagram and two Sequence Model diagrams to illustrate the power of the Prototype Pattern. To make this clear, the terminologies used in the diagrams are the following:
- Client: This object uses and creates the Prototype.
- Prototype Interface: This describes the clone() method.
- Prototype: Any object that implements the Prototype interface.
Class Diagram
The diagram below includes everything: “Clients” – “MyClass” and “Document”, and the “IPrototype” interface.
- IProtoType enforces the clone() contract.
- MyClass stores a field (mutable, e.g., a list) and implements clone() as a shallow copy.
- Document works in 3 “modes” and shows both the shallow and deep copying scenario options. [One can visualize “copying” documents in the real world!]
All the relationships are clear:
- Both MyClass and Document classes define the possible objects that can be created to implement the “IPrototype” interface (which is shown in the diagram as “IProtoType”.
Sequence Diagrams
- Python provides two types of “copying” – Shallow and Deep.
- The first sequence diagram (shown below) is a demonstration of the basic “Shallow Clone (Copy)” scenario – demonstrating the creating of an object, cloning (copying) it, and modifying the clone (copy).
- The second sequence diagram (shown below) depicts a potential “Deep Clone (Copy)” scenario.
- If the clone() method is modified for deeper copying, the flow would differ slightly.
All of the above diagrams have their respective Python Program code.
If anybody is interested, I can share the actual files.



