Package elisa :: Package core :: Package utils :: Module singleton
[hide private]
[frames] | no frames]

Source Code for Module elisa.core.utils.singleton

 1  # -*- coding: utf-8 -*- 
 2  # Elisa - Home multimedia server 
 3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
 4  # All rights reserved. 
 5  # 
 6  # This file is available under one of two license agreements. 
 7  # 
 8  # This file is licensed under the GPL version 3. 
 9  # See "LICENSE.GPL" in the root of this distribution including a special 
10  # exception to use Elisa with Fluendo's plugins. 
11  # 
12  # The GPL part of Elisa is also available under a commercial licensing 
13  # agreement from Fluendo. 
14  # See "LICENSE.Elisa" in the root directory of this distribution package 
15  # for details on that license. 
16   
17   
18  __maintainer__ = 'Florian Boucault <florian@fluendo.com>' 
19   
20   
21 -class Singleton(object):
22 23 """ 24 Implements the Singleton pattern. 25 26 Every class inheriting from Singleton will have one and only one instance 27 of its kind at any time. 28 29 NOTE: uses __new__ which has been introduced in Python 2.2 30 """ 31 32 __instance = None 33
34 - def __new__(cls, *args, **kwargs):
35 if not cls.__instance: 36 cls.__instance = object.__new__(cls, *args, **kwargs) 37 38 return cls.__instance
39 40 41 #if __name__ == "__main__": 42 # class ClassA(Singleton): 43 # pass 44 # 45 # class ClassB(ClassA): 46 # pass 47 # 48 # # There will be only one instance for ClassA and ClassB 49 # 50 # class ClassC(Singleton): 51 # pass 52 # 53 # instance1 = ClassA() 54 # instance2 = ClassA() 55 # instance3 = ClassB() 56 # instance4 = ClassB() 57 # 58 # instance5 = ClassC() 59 # instance6 = ClassC() 60 # 61 # print "instance1 id=", id(instance1) 62 # print "instance2 id=", id(instance2) 63 # print "instance3 id=", id(instance3) 64 # print "instance4 id=", id(instance4) 65 # 66 # print "instance5 id=", id(instance5) 67 # print "instance6 id=", id(instance6) 68