Last active
October 8, 2018 15:48
-
-
Save ZoranPandovski/edeb4daa2a622e90e32353ee7bc808be to your computer and use it in GitHub Desktop.
Singleton behavior using Borg pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Borg(object): | |
__shared_state = {} | |
def __init__(self): | |
self.__dict__ = self.__shared_state | |
class Singleton(Borg): | |
pass | |
if __name__ == '__main__': | |
obj1 = Singleton() | |
obj2 = Singleton() | |
obj1.name = 'Obj1' | |
obj2.name = 'Obj2' | |
print(obj1.name) | |
print(obj2.name) | |
#OUTPUT: | |
#Obj2 | |
#Obj2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment