原型模式设计
(一)、什么是原形设计模式
原型设计模式(Prototype design pattern)帮助我们创建对象的克隆,其最简单的形式就是一 个clone()函数,接受一个对象作为输入参数,返回输入对象的一个副本。在Python中,这可以 使用copy.deepcopy()函数来完成。
(二)、应用案例
- 当我们已有一个对象,并希望创建该对象的一个完整副本时,原型模式就派上用场了。在我 们知道对象的某些部分会被变更但又希望保持原有对象不变之时,通常需要对象的一个副本。在 这样的案例中,重新创建原有对象是没有意义的。
- 当我们想复制一个复杂对象时,使用原型模式会很方便。对于复制复杂对象, 我们可以将对象当作是从数据库中获取的,并引用其他一些也是从数据库中获取的对象。若通过 多次重复查询数据来创建一个对象,则要做很多工作。在这种场景下使用原型模式要方便得多。
(三)、实现
使用原型模式创建一个展示图书信息的应用。
import copy
from collections import OrderedDict
class Book:
def __init__(self, name, authors, price, **rest):
'''rest的例子有: 出版商、长度、 标签、出版日期'''
self.name = name
self.authors = authors
self.price = price
self.__dict__.update(rest)
def __str__(self):
'''有序字典输出'''
mylist = list()
ordered = OrderedDict(sorted(self.__dict__.items()))
for i in ordered.keys():
mylist.append('{}: {}'.format(i, ordered[i]))
if i == 'price':
mylist.append('$')
mylist.append('\n')
return ''.join(mylist)
class Prototype:
def __init__(self):
self.objects = dict()
def register(self, identifier, obj):
self.objects[identifier] = obj
def unregister(self, identifier):
del self.objects[identifier]
def clone(self, identifier, **attr):
found = self.objects.get(identifier)
if not found:
raise ValueError('Incorrect object identifier: {}'.format(identifier))
obj = copy.deepcopy(found)
obj.__dict__.update(attr)
return obj
def main():
b1 = Book('The C Programming Language', ('Brian W. Kernighan', 'Dennis M.Ritchie'),
price=118, publisher='Prentice Hall', length=228, publication_date='1978-02-22',
tags=('C', 'programming', 'algorithms', 'data structures'))
prototype = Prototype()
cid = 'k&r-first'
prototype.register(cid, b1)
b2 = prototype.clone(cid, name='The C Programming Language(ANSI)', price=48.99,
length=274, publication_date='1988-04-01', edition=2)
for i in (b1, b2):
print(i)
print("ID b1 : {} != ID b2 : {}".format(id(b1), id(b2)))
if __name__ == '__main__':
main()