clone()
方法用于复制对象。class person:
def __init__(self, name, age):
self.name = name
self.age = age
def todict(self):
return {"name": self.name, "age": self.age}
if __name__ == '__main__':
a = person("John", 25)
b = person("John", 25)
c = person("John", 25)
print(a.todict())
print(b.todict())
print(c.todict())
if __name__ == '__main__':
a = person("John", 25)
b = a
c = a
a.name = "Jane"
print(a.todict())
print(b.todict())
print(c.todict())
import copy
# 原型接口
class Prototype:
def clone(self):
pass
# 具体原型类
class ConcretePrototype(Prototype):
def __init__(self, name, interests):
self.name = name
self.interests = interests
def get_name(self):
return self.name
def get_interests(self):
return self.interests
def clone(self):
# 使用浅拷贝
return copy.copy(self)
# 客户端代码
if __name__ == "__main__":
prototype1 = ConcretePrototype("jack", ['classical music', 'rock music'])
prototype2 = prototype1.clone()
print(f"Prototype 1: {prototype1.get_name()}, Items: {prototype1.get_interests()}")
print(f"Prototype 2: {prototype2.get_name()}, Items: {prototype2.get_interests()}")
clone()
方法用于复制对象。在上面的示例代码中,尝试下修改属性
if __name__ == "__main__":
prototype1 = ConcretePrototype("jack", ['classical music', 'rock music'])
prototype2 = prototype1.clone()
# 修改 prototype2 的 interests 属性
prototype2.get_interests().append('jazz music')
prototype1.name = "jane"
print(f"Prototype 1: {prototype1.get_name()}, Items: {prototype1.get_interests()}")
print(f"Prototype 2: {prototype2.get_name()}, Items: {prototype2.get_interests()}")
# Output:
# Prototype 1: jane, Items: ['classical music', 'rock music', 'jazz music']
# Prototype 2: jack, Items: ['classical music', 'rock music', 'jazz music']
不难发现修改name
属性,不会相互影响,但是修改interests
属性,prototype1
和prototype2
都受到了影响。主要原因是:浅拷贝中的元素,只拷贝了表面的一层,因此,如果原对象中包含了引用对象,改变其也会影响拷贝后的对象
clone
函数即可import copy
# 原型接口
class Prototype:
def clone(self):
pass
# 具体原型类
class ConcretePrototype(Prototype):
def __init__(self, name, interests):
self.name = name
self.interests = interests
def get_name(self):
return self.name
def get_interests(self):
return self.interests
def clone(self):
# 使用浅拷贝
return copy.deepcopy(self)
# 客户端代码
if __name__ == "__main__":
prototype1 = ConcretePrototype("jack", ['classical music', 'rock music'])
prototype2 = prototype1.clone()
# 修改 prototype2 的 interests 属性
prototype2.get_interests().append('jazz music')
prototype1.name = "jane"
print(f"Prototype 1: {prototype1.get_name()}, Items: {prototype1.get_interests()}")
print(f"Prototype 2: {prototype2.get_name()}, Items: {prototype2.get_interests()}")
# Output:
# Prototype 1: jane, Items: ['classical music', 'rock music']
# Prototype 2: jack, Items: ['classical music', 'rock music', 'jazz music']
import copy
# 原型接口
class Prototype:
def clone(self):
pass
# 具体原型类
class ConcretePrototype(Prototype):
def __init__(self, name, interests):
self.name = name
self.interests = interests
def get_name(self):
return self.name
def get_interests(self):
return self.interests
def clone(self):
# 返回一个克隆对象
return copy.deepcopy(self)
# 工厂类,用于生产对象
class PrototypeFactory:
def __init__(self):
# 初始化时可以设置多个原型对象
self.prototypes = {}
def register_prototype(self, name, prototype):
""" 注册原型对象 """
self.prototypes[name] = prototype
def create_prototype(self, name):
""" 根据名字克隆原型对象 """
prototype = self.prototypes.get(name)
if prototype:
return prototype.clone()
else:
print(f"Prototype {name} not found!")
return None
# 客户端代码
if __name__ == "__main__":
# 创建原型对象
prototype1 = ConcretePrototype("jack", ['classical music', 'rock music'])
prototype2 = ConcretePrototype("alice", ['jazz music', 'pop music'])
# 创建工厂对象并注册原型
factory = PrototypeFactory()
factory.register_prototype("jackPrototype", prototype1)
factory.register_prototype("alicePrototype", prototype2)
# 通过工厂创建新的对象
cloned_object1 = factory.create_prototype("jackPrototype")
cloned_object2 = factory.create_prototype("alicePrototype")
# 修改克隆对象的属性
cloned_object1.get_interests().append("hip hop")
cloned_object2.get_name() # "alice"
# 输出克隆对象的属性
print(f"Cloned Object 1: {cloned_object1.get_name()}, Interests: {cloned_object1.get_interests()}")
print(f"Cloned Object 2: {cloned_object2.get_name()}, Interests: {cloned_object2.get_interests()}")
clone()
方法来创建对象实例,而不直接使用构造函数