212. The Prototype Design Pattern
πΉ 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
Last updated