File tree 1 file changed +41
-0
lines changed
1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ #
2
+ # Python Design Patterns: Singleton
3
+ # Author: Jakub Vojvoda [github.com/JakubVojvoda]
4
+ # 2016
5
+ #
6
+ # Source code is licensed under MIT License
7
+ # (for more details see LICENSE)
8
+ #
9
+
10
+ import sys
11
+
12
+ #
13
+ # Singleton
14
+ # has variable to hold one instance of the class and
15
+ # method which gives us a way to instantiate the class
16
+ #
17
+ # reference: http://stackoverflow.com/questions/31875
18
+ #
19
+ class Singleton :
20
+ def __init__ (self , instance ):
21
+ self ._instance = instance
22
+
23
+ def get (self ):
24
+ try :
25
+ return self ._only
26
+ except AttributeError :
27
+ self ._only = self ._instance ()
28
+ return self ._only
29
+
30
+ def __call__ (self ):
31
+ raise TypeError ("..." )
32
+
33
+ @Singleton
34
+ class Class :
35
+ def tell (self ):
36
+ print ("This is singleton." )
37
+
38
+
39
+ if __name__ == "__main__" :
40
+ singleton = Class .get ()
41
+ singleton .tell ()
You can’t perform that action at this time.
0 commit comments