Cloning Your Way to Efficiency
In essence, the Prototype Design Pattern empowers you to leverage the success of existing objects, making your creation process faster, more efficient, and more flexible.
import copy
class Prototype:
def clone(self):
return copy.deepcopy(self) # Create a deep copy of the object
class ConcretePrototype1(Prototype):
def __init__(self, field1):
self.field1 = field1
class ConcretePrototype2(Prototype):
def __init__(self, field2):
self.field2 = field2
# Client code
prototype1 = ConcretePrototype1("Prototype 1 data")
prototype2 = ConcretePrototype2("Prototype 2 data")
# Clone and modify prototypes
clone1 = prototype1.clone()
clone1.field1 = "Modified data for clone 1"
clone2 = prototype2.clone()
clone2.field2 = "Modified data for clone 2"
print(clone1.field1) # Output: Modified data for clone 1
print(clone2.field2) # Output: Modified data for clone 2