跳到主要內容


python-設計模式-原型模式

python
設計模式
原型模式


import copy 
class Prototype: 
   def __init__(self): 
      self.obj = {} 
   def Registor(self, key,new_obj): 
      self.obj[key] = new_obj 
   def UnRegistor(self, key): 
      del self.obj[key]
   def DeepClone(self, name, **attr): 
      obj_clone = copy.deepcopy(self.obj.get(name)) 
      obj_clone.__dict__.update(attr) 
      return obj_clone 
   def Clone(self, name, **attr): 
      obj_clone = copy.copy(self.obj.get(name)) 
      obj_clone.__dict__.update(attr) 
      return obj_clone 

class a: 
   def __init__(self): 
      self.name = ['a1','a2'] 

class b: 
   def __init__(self): 
      self.name = 'b' 

a = a() 
b = b() 

P = Prototype()
P.Registor('a',a)
P.Registor('b',b)


aDeepClone = P.DeepClone('a') 
aClone = P.Clone('a')
a.name[0] = 'new_a' 

bDeepClone = P.DeepClone('a') 
bClone = P.Clone('a') 
b.name = 'new_b' 

print('A DeepClone') 
print(a.name, aDeepClone.name) 
print('A Clone') print(a.name, aClone.name) 
print('----------------------') 
print('B DeepClone') 
print(b.name, bDeepClone.name) 
print('B Clone') 
print(b.name, bClone.name) P.UnRegistor('b')


程式碼說明:
在python實作prototype需要使用copy模組來實行,在做深層複製時沒有問題,在做淺層複製時,若複製對象不是list對於結果會有錯誤的預期

prototype類別定義prototype的工作,複製、註冊物件、刪除物件。
定義需要複製的類別A、類別B,並註冊到prototype物件中
類別A為list形式
類別B為字串形式
分別進行深層複製、淺層複製。
在修蓋內容,list則修正部分,會發現到list跟一般定義的深層複製、淺層複製結果會一樣,但在一般變數的部分則不。

需要探討為什麼複製結果會這樣。

留言

這個網誌中的熱門文章

Python-資料庫-mongodb-pymongo

Python 資料庫 mongodb-pymongo 安裝: linux、mac:pip3 install pymongo windows: import pymongo client = pymongo.MongoClient("mongodb://localhost:27017/") db = client['demo_db'] col = db['demo_col'] dict1 = { "name": "ab123ab456g", "day": "1890-04-05" } result = col.insert_one(dict1)  dict2 = [   { "name": "ki", "day": "1666-1-1"},   { "name": "aa", "day": "1222-11-11"},   { "name": "gg-gg", "day": "1333-02-22"},   { "name": "T-T", "day": "1444-03-02"},   { "name": "f-f", "day": "1555-01-01"} ] result = col.insert_many(dict2) result = col.find_one() print(result) results = col.find() for result in col.find(): print(result) results = col.find() query = {'...

語言學習-English-Lights

Song Lyric Title : Lights Singer :Ellie Goulding Album :  Bright Lights Release Date :  2011 I had a way then losing it all on my own I had a heart then but the queen has been overthrown And I'm not sleeping now the dark is too hard to beat And I'm not keeping up the strength I need to push me You show the lights that stop me turn to stone You shine it when I'm alone And so I tell myself that I'll be strong And dreaming when they're gone 'Cause they're calling, calling, calling me home Calling, calling, calling home You show the lights that stop me turn to stone You shine them when I'm alone Noises, I play within my head Touch my own skin and hope they'll still be there And I think back to when my brother and my sister slept In and on my place the only time I feel safe You show the lights that stop me turn to stone You shine it when I'm alone And so I tell myself that I'...

程式語言學習概論(1)

程式語言 介紹