Skip to content

Instantly share code, notes, and snippets.

@BolajiOlajide
Created February 24, 2017 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BolajiOlajide/76f783d02949c03c2d45d2d6c3a226ba to your computer and use it in GitHub Desktop.
Save BolajiOlajide/76f783d02949c03c2d45d2d6c3a226ba to your computer and use it in GitHub Desktop.
Singleton Pattern
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