设计模式之创建型模式: 原型模式
一、简介
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。例如,一个对象需要在一个高代价的数据库操作之后被创建。我们可以缓存该对象,在下一个请求时返回它的克隆,在需要的时候更新数据库,以此来减少数据库调用。
意图:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
主要解决:在运行期建立和删除原型。
何时使用:
- 当一个系统应该独立于它的产品创建,构成和表示时。
- 当要实例化的类是在运行时刻指定时,例如,通过动态装载。
- 为了避免创建一个与产品类层次平行的工厂类层次时。
- 当一个类的实例只能有几个不同状态组合中的一种时。建立相应数目的原型并克隆它们可能比每次用合适的状态手工实例化该类更方便一些。
如何解决:利用已有的一个原型对象,快速地生成和原型对象一样的实例。
二、具体实现:以图层对象为例
from copy import copy, deepcopy class simpleLayer: """ 设计一个图层对象,用background表示背景的RGBA,简单用content表示内容,除了直接绘画,还可以设置透明度。 """ background = [0, 0, 0, 0] content = "blank" def getContent(self): return self.content def getBackground(self): return self.background def paint(self, painting): self.content = painting def setParent(self, p): self.background[3] = p def fillBackground(self, back): self.background = back def clone(self): return copy(self) def deep_clone(self): return deepcopy(self)
使用方式一:初始化一个原型对象,再复制一个一模一样的对象
if __name__ == "__main__": dog_layer = simpleLayer() # 初始化一个图层 dog_layer.paint("Dog") # 图层上画一只狗狗 dog_layer.fillBackground([0, 0, 255, 0]) # 修改图层背景颜色 print("Background:", dog_layer.getBackground()) print("Painting:", dog_layer.getContent()) another_dog_layer = dog_layer.clone() # 通过复制(clone)这个动作实现画一只同样的狗 print("Background:", another_dog_layer.getBackground()) print("Painting:", another_dog_layer.getContent())
输出结果:
# Background: [0, 0, 255, 0] # Painting: Dog # Background: [0, 0, 255, 0] # Painting: Dog
使用方式二:浅拷贝
print('--------------------浅拷贝--------------------------') if __name__ == "__main__": dog_layer = simpleLayer() dog_layer.paint("Dog") dog_layer.fillBackground([0, 0, 255, 0]) print("Original Background:", dog_layer.getBackground()) print("Original Painting:", dog_layer.getContent()) another_dog_layer = dog_layer.clone() # another_dog_layer=dog_layer.deep_clone() another_dog_layer.setParent(128) another_dog_layer.paint("Puppy") print("Original Background:", dog_layer.getBackground()) print("Original Painting:", dog_layer.getContent()) print("Copy Background:", another_dog_layer.getBackground()) print("Copy Painting:", another_dog_layer.getContent())
--------------------浅拷贝-------------------------- Original Background: [0, 0, 255, 0] Original Painting: Dog Original Background: [0, 0, 255, 128] Original Painting: Dog Copy Background: [0, 0, 255, 128] Copy Painting: Puppy
使用方式三:深拷贝
print('--------------------深拷贝--------------------------') #深拷贝 if __name__=="__main__": dog_layer=simpleLayer() dog_layer.paint("Dog") dog_layer.fillBackground([0,0,255,0]) print("Original Background:",dog_layer.getBackground()) print("Original Painting:",dog_layer.getContent()) #another_dog_layer=dog_layer.clone() another_dog_layer=dog_layer.deep_clone() another_dog_layer.setParent(128) another_dog_layer.paint("Puppy") print("Original Background:", dog_layer.getBackground()) print("Original Painting:", dog_layer.getContent()) print("Copy Background:", another_dog_layer.getBackground()) print("Copy Painting:", another_dog_layer.getContent())
--------------------深拷贝-------------------------- Original Background: [0, 0, 255, 0] Original Painting: Dog Original Background: [0, 0, 255, 0] Original Painting: Dog Copy Background: [0, 0, 255, 128] Copy Painting: Puppy
三、原型模式的优缺点和使用场景
优点: 1、性能提高。 2、逃避构造函数的约束。
缺点: 1、配备克隆方法需要对类的功能进行通盘考虑,这对于全新的类不是很难,但对于已有的类不一定很容易,特别当一个类引用不支持串行化的间接对象,或者引用含有循环结构的时候。 2、必须实现 Cloneable 接口。
使用场景: 1、资源优化场景。 2、类初始化需要消化非常多的资源,这个资源包括数据、硬件资源等。 3、性能和安全要求的场景。 4、通过 new 产生一个对象需要非常繁琐的数据准备或访问权限,则可以使用原型模式。 5、一个对象多个修改者的场景。 6、一个对象需要提供给其他对象访问,而且各个调用者可能都需要修改其值时,可以考虑使用原型模式拷贝多个对象供调用者使用。 7、在实际项目中,原型模式很少单独出现,一般是和工厂方法模式一起出现,通过 clone 的方法创建一个对象,然后由工厂方法提供给调用者。原型模式已经与 Java 融为浑然一体,大家可以随手拿来使用。
注意事项:与通过对一个类进行实例化来构造新对象不同的是,原型模式是通过拷贝一个现有对象生成新对象的。浅拷贝实现 Cloneable,重写,深拷贝是通过实现 Serializable 读取二进制流。

浙公网安备 33010602011771号