Python 享元模式

# -*- coding: utf-8 -*-
"""
Created on Wed Mar 28 20:41:17 2018

@author: mz
"""

class Share(object):
    def __init__(self):
        self.d = {}
    
    def Attach(self, key, value):
        if key in self.d:
            return
        else:
            self.d[key] = value
    
    def Show(self):
        for dic in self.d :
           print ("dict[\"%s\"]=" % dic,self.d[dic])

    

if "__main__" == __name__:
    share = Share()
    
    share.Attach("z", 5)
    share.Attach("s", 6)
    
    share.Show()
    
    print("\r\n--attach same z--")
    share.Attach("z", 10)
    share.Show()
原型结果:
dict["z"]= 5
dict["s"]= 6

--attach same z--
dict["z"]= 5
dict["s"]= 6


猜你喜欢

转载自blog.csdn.net/mz5111089/article/details/79733858