Created
February 24, 2017 16:23
-
-
Save BolajiOlajide/76f783d02949c03c2d45d2d6c3a226ba to your computer and use it in GitHub Desktop.
Singleton 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 Singleton: | |
the_actual_instance = None | |
class _Singleton: | |
""" | |
The actual implementation of the Singleton class should reside here | |
""" | |
def __init__(self): | |
self.required_attribute = 'mayor' | |
print('this should only be printed once.. i.e this class should only be instantiated once.') | |
def create_room(self, room_name): | |
print 'I see you are trying to create a new room ', room_name | |
def __init__(self): | |
if not Singleton.the_actual_instance: | |
print('Fresh start') | |
Singleton.the_actual_instance = Singleton._Singleton() | |
print Singleton.the_actual_instance, '.....' | |
else: | |
pass | |
def __getattr__(self, required_attribute): | |
return getattr(self.the_actual_instance, required_attribute) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment