Posts

Showing posts with the label Design Pattern

[ Level 2 ] Test Singleton Implementation in Python.

Days ago, just search how to use implement "Singleton" design pattern. ( http://stackoverflow.com/questions/6760685/crea(ing-a-singleton-in-python ) And today, I just need to implement for it. In my case, I need to create mutliple loggers and I also want to use "Singleton" to reduce system resource usage. Therefore, I create a Singleton meta class and also use a parameter called "singleton_id" to define the instance category. Source Code: #!/bin/env python class Singleton(object): _singleton_key = 'singleton_id' _instance = {} def __new__(class_, *args, **kwargs): if class_._singleton_key not in kwargs.keys(): kwargs[class_._singleton_key] = '' if class_._singleton_key in kwargs and kwargs[class_._singleton_key] not in class_._instance.keys(): class_._instance[kwargs[class_._singleton_key]] = object.__new__(class_) return class_._instance[kwargs[class_._singleton_key]] class my...

[Level 2] Flyweight in Python.

#!/usr/bin/env python import random class Black(object): __instance = None def __new__(cls): if not cls.__instance: cls.__instance = super(Black, cls).__new__(cls) return cls.__instance def __str__(self): return 'black' class White(object): __instance = None def __new__(cls): if not cls.__instance: cls.__instance = super(White, cls).__new__(cls) return cls.__instance def __str__(self): return 'white' class BlackAndWhite(object): __black = Black() __white = White() def __new__(cls, index): if index == 0: return cls.__black else: return cls.__white def __init__(self, index): pass class Board(object): def __init__(self, size): self.__size = size self.__planet = [ [ None for i in range(self.__size) ] for j in range(self.__size) ] def setPlanet(self): while True: x =...

[Level 2] Singleton in Python.

#!/usr/bin/env python class Singleton(object): _instance = None def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs) return cls._instance print me print you print me == you print id(me) == id(you) $ ./testSingleton.py True True $ You could also use metaclass to implement it. #!/usr/bin/env python class Singleton(type): def __init__(self, *args, **kwargs): super(Singleton, self).__init__(*args, **kwargs) self.__instance = None def __call__(self, *args, **kwargs): if self.__instance is None: self.__instance = super(Singleton, self).__call__(*args, **kwargs) return self.__instance class s(object): __metaclass__ = Singleton def __init__(self, size): self.__size = size def getSize(self): return self.__size class n(object): def __init__(self, size): self.__size = size def ...

[Level 3] IoC/AOP in Python.

More Ioc info, please refer to here . More AOP info, please refer to here . Wish this helps. regards, Stanley Huang

[Level 3] Design Pattern in Python(1): Singleton

If you want to implement singleton in Python, you can refer the  following sample code. #!/usr/bin/env python c lass SingleFactory:     __single = None     def __init__( self ):         if not SingleFactory.__single:             SingleFactory.__single = self     def getInstance( self ):         return SingleFactory.__single if __name__ == '__main__':     f = SingleFactory()     a = f.getInstance()     b = f.getInstance()     print a     print b     a.count = 1     print a.count     print b.count Execute it. $ ./t.py <__main__.SingleFactory instance at 0x1004d1290> <__main__.SingleFactory instance at 0x1004d1290> 1 1 Wish this helps. regards, Stanley Huang

[Level 3] Python Desing Pattern -- Episode 0 -- Intro.

One Pattern a day, keep refactoring away~ Wish this helps. regards, Stanley Huang