Python 设计模式:原型模式

297 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第 25 天,点击查看活动详情

定义

原型模式(Prototype Pattern) 是一种创建型设计模式, 使你能够复制已有对象, 而又无需使代码依赖它们所属的类。原型模式能够让我们利用克隆技术在现有对象的基础上创建对象。

说到克隆,一个著名的非技术性例子是名为多莉的绵羊,它是由苏格兰的研究人员通过克隆乳腺中的一个细胞创造的。

许多 Python 应用程序都使用了原型模式,但它很少被称为原型,因为克隆对象是语言的一个内置功能。

原型模式的想法是使用该对象的完整结构的拷贝来产生新的对象。我们将看到,这在 Python 中几乎是自然而然的,因为我们有一个拷贝功能,对使用这种技术有很大帮助。在创建一个对象的拷贝的一般情况下,所发生的是你对同一个对象做一个新的引用,这种方法叫做浅拷贝。但如果你需要复制对象,也就是原型的情况,你要做一个深拷贝。

实现

原型模式建议创建一个接口来创建现有对象的克隆。任何客户都可以依赖的对象。Python 中可以通过类来进行实例化:

  • 原型类: 一个超类,它将包含对象克隆将具有的所有必要属性和方法。此外,Prototype 有一个抽象的 clone() 方法,它必须由所有子类实现。
  • 具体类: 一旦我们创建了原型超类,我们就可以开始基于超类定义具体类。具体类是可选的,可以在应用程序中定义。

具体的类可以有自己的属性和方法,但它们总是有原始的原型属性和被覆盖的 clone() 版本。

在 Python 中,考虑为 Car 对象创建一个原型。让我们为汽车创建一个接口:

class Car:
    def __init__(self, engine="1500cc", color="D-white", seats=7):
        self.engine = engine
        self.color = color
        self.seats = seats

    def __str__(self):
        return f"{self.engine} | {self.color} | {self.seats}"

原型类:

  • 原型接口有一个字典数据结构来存储所有克隆的对象。
  • RegisterObject 方法在字典中添加元素,以新对象的名称为键,以现有对象为值。
  • DeregisterObject 方法从字典中删除条目。
  • 并且,最后通过 Clone 方法复制现有对象。克隆方法使用来自 Copy 模块的 deepcopy() 方法来克隆对象。
import copy


class Prototype:
    def __init__(self):
        """Dictionary that will stores cloned objects."""
        self._ClonedObjects = {}

    def RegisterObject(self, name, obj):
        """Method to store all clones of the existion objects."""
        self._ClonedObjects[name] = obj

    def DeregisterObject(self, name):
        """Method to delete the cloned object from the dictionary."""
        del self._ClonedObjects[name]

    def Clone(self, name, **kwargs):
        """Method to clone the object."""
        clonedObject = copy.deepcopy(self._ClonedObjects.get(name))
        clonedObject.__dict__.update(kwargs)
        return clonedObject

最后,我们利用 main 函数来测试:

if __name__ == "__main__":
    """The object that will be cloned.""" 
    defaultCar = Car()
    prototype = Prototype()

    """The object that will be cloned.""" 
    CarType1 = Car("1000cc", "Red", 4)

    """Registering the defaultCar in dictionary with its key as 'basicCar'"""
    prototype.RegisterObject('BasicCar', defaultCar)  
    prototype.RegisterObject('Type-1', CarType1)

    carOne = prototype.Clone('BasicCar', color = "Lake side brown")                                                      
    carTwo = prototype.Clone('Type-1',color = "Red") 
    carThree = prototype.Clone('Type-1', color = "Moon Dust Silver")

    print("Details of the default-car:", defaultCar)  
    print("Details of car-One:", carOne)        
    print("Details of car-Two:", carTwo)  
    print("Details of car-Three:", carThree)    

运行输出:

$ python protype.py 
Details of the default-car: 1500cc | D-white | 7
Details of car-One: 1500cc | Lake side brown | 7   
Details of car-Two: 1000cc | Red | 4
Details of car-Three: 1000cc | Moon Dust Silver | 4

总结

原型是一种创建型设计模式, 使你能够复制对象, 甚至是复杂对象, 而又无需使代码依赖它们所属的类。

所有的原型类都必须有一个通用的接口, 使得即使在对象所属的具体类未知的情况下也能复制对象。 原型对象可以生成自身的完整副本, 因为相同类的对象可以相互访问对方的私有成员变量。

原型模式减少了子类的数量,隐藏了创建对象的复杂性,并便于在运行时添加或删除对象。

参考链接:

  1. 深入设计模式:原型模式
  2. The Prototype Pattern
  3. The Prototype Design Pattern in Python