Mein Blog entwickelt sich zur Python Ecke ... es könnte schlimmer kommen. Obwohl Python im Gegensatz zu Java keine Sichtbarkeitseinschränkungen kennt kann man dennoch einige aus Java wohlbekannte Designpatterns auch in Python implementieren. Hier als Beispiel eine threadsichere Implementierung des Singleton Patterns.
from threading import Lock
class Singleton(object):
"""
A simple example implementing the singleton design pattern in python
"""
#we need two class attributes (which translate to static attributes in java)
__lock = Lock() #a lock for thread safety
__instance = None #and to remember the one instance
#first of all: make pythons usual way of creating objects unusable because
#we cannot just hide them as one would in java or C++
def __new__(cls, *args, **kwargs):
pass
def __init__(self):
pass
#mind that a java static translates into a python classmethod and not a
#staticmethod (which would lack the cls which is a reference to the class)
@classmethod
def getInstance(cls, *args, **kwargs):
"""
The famous gatInstance method which resturns the one instance of our
class.
params:
cls - reference to the class
*args - the tuple of arguments paassed by position
**kwargs - the dictionary of arguments passed by keyword
"""
#enter critical section
cls.__lock.acquire()
try:
if cls.__instance is None:
#use the superclasses __new__ for creation
cls.__instance = object.__new__(cls, *args, **kwargs)
#place initialisation code (everything which would usually
#happen in __init__) here
cls.__instance.SomeInt = 1
finally:
#leave critical section
cls.__lock.release()
return cls.__instance
if __name__ == "__main__":
instance1 = Singleton.getInstance()
instance2 = Singleton.getInstance()
print hash(instance1)==hash(instance2) #True
instance3 = Singleton()
print instance3 == None #True
from threading import Lock
class Singleton(object):
"""
A simple example implementing the singleton design pattern in python
"""
#we need two class attributes (which translate to static attributes in java)
__lock = Lock() #a lock for thread safety
__instance = None #and to remember the one instance
#first of all: make pythons usual way of creating objects unusable because
#we cannot just hide them as one would in java or C++
def __new__(cls, *args, **kwargs):
pass
def __init__(self):
pass
#mind that a java static translates into a python classmethod and not a
#staticmethod (which would lack the cls which is a reference to the class)
@classmethod
def getInstance(cls, *args, **kwargs):
"""
The famous gatInstance method which resturns the one instance of our
class.
params:
cls - reference to the class
*args - the tuple of arguments paassed by position
**kwargs - the dictionary of arguments passed by keyword
"""
#enter critical section
cls.__lock.acquire()
try:
if cls.__instance is None:
#use the superclasses __new__ for creation
cls.__instance = object.__new__(cls, *args, **kwargs)
#place initialisation code (everything which would usually
#happen in __init__) here
cls.__instance.SomeInt = 1
finally:
#leave critical section
cls.__lock.release()
return cls.__instance
if __name__ == "__main__":
instance1 = Singleton.getInstance()
instance2 = Singleton.getInstance()
print hash(instance1)==hash(instance2) #True
instance3 = Singleton()
print instance3 == None #True
Keine Kommentare:
Kommentar veröffentlichen