第四天:建立型模式--原型模式
阿新 • • 發佈:2019-01-06
零、原型模式
什麼是原型模式:
原型模式無非就是克隆一個物件,其最簡單的形式就是一個 clone() 函式,接受一個物件作為輸入引數,返回輸入物件的一個副本。在 Python 中可以使用 copy.deepcopy() 函式來完成。什麼是副本:
一個物件在 某一時間點 的 完全 複製,時間是一個重要元素,因為它會影響克隆所包含的內容。- 引用和副本間的區別:
如果A和B都持有同一個物件的引用,那麼A修改物件後,B就能看到物件的修改,反之亦然。
如果A和B都持有一個物件的副本,呢麼A修改所持有的副本,B是看不到修改的,反之亦然。因為A和B所持有的副本是兩個獨立的記憶體地址。
一、身邊的例子
- 細胞的分裂。細胞核分裂產生兩個新的細胞核,這兩個細胞都具有原來細胞相同的染色體、DNA等。
- VTK(視覺化工具套件)。VTK是一個開源的跨平臺的系統,用於三位計算圖形和圖片處理以及視覺化。VTK利用原型模式來建立幾何元素。
- Python中很多應用也都使用了原型模式。
二、什麼情況下使用
- 當我們已有一個物件,並希望建立該物件的一個完整副本時;
- 在我們知道物件的某些本分會被改變,但有希望保持原有的物件不變時;
- 我們想複製一個複雜的物件時。
三、應用案例
下面我們來看一個案例,利用原型模式展示一個圖書的各個版本資訊
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 = []
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, identifiter, obj):
self.objects[identifiter] = 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 Cprogramming Language', ('Brian W.Kernighan', 'Dennis M.Ritchie'), price=118,
publisher='Prentice Hall', length=228, publication_date='1978-02-22',
tags=('C', 'programming', 'algorithma', 'algorihma', 'data structures'))
prototype = Prototype()
cid = 'k&r-first'
prototype.register(cid, b1)
b2 = prototype.clone(cid, name='The CProgramming 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()