212. The Prototype Design Pattern
🔹 The Prototype Design Pattern in Python
🔹 1. Basic Prototype Pattern
import copy
class Prototype:
def clone(self):
return copy.deepcopy(self)
class Person(Prototype):
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Alice", 25)
p2 = p1.clone()
print(p1.name, p1.age) # Alice 25
print(p2.name, p2.age) # Alice 25
print(p1 is p2) # False (Different objects)🔹 2. Cloning a List in an Object
🔹 3. Deep Copy to Avoid Shared Mutable Objects
🔹 4. Cloning Objects with Nested Structures
🔹 5. Storing Prototypes in a Registry
🔹 6. Prototype with a Factory Method
🔹 7. Cloning Objects with Custom Adjustments
🔹 8. Preventing Cloning (Singleton-Like Behavior)
🔹 9. Prototype with Class-Level Attributes
🔹 10. Thread-Safe Cloning in a Multi-Threaded Environment
🚀 Summary
🚀 Best Practices
Last updated