Created
January 30, 2014 22:34
-
-
Save atsuya046/8721524 to your computer and use it in GitHub Desktop.
GoF design pattern - Singleton.py
This file contains hidden or 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""http://python-3-patterns-idioms-test.readthedocs.org/en/latest/Singleton.html""" | |
class OnlyOne: | |
class __OnlyOne: | |
def __init__(self, arg): | |
self.val = arg | |
def __str__(self): | |
return repr(self) + self.val | |
instance = None | |
def __init__(self, arg): | |
if not OnlyOne.instance: | |
OnlyOne.instance = OnlyOne.__OnlyOne(arg) | |
else: | |
OnlyOne.instance.val = arg | |
def __getattr__(self, name): | |
return getattr(self.instance, name) | |
def main(): | |
x = OnlyOne('sausage') | |
print(x) | |
y = OnlyOne('eggs') | |
print(y) | |
z = OnlyOne('spam') | |
print(z) | |
print(x) | |
print(y) | |
print(`x`) | |
print(`y`) | |
print(`z`) | |
''' | |
Output | |
<__main__.__OnlyOne instance at 0076B7AC>sausage | |
<__main__.__OnlyOne instance at 0076B7AC>eggs | |
<__main__.__OnlyOne instance at 0076B7AC>spam | |
<__main__.__OnlyOne instance at 0076B7AC>spam | |
<__main__.__OnlyOne instance at 0076B7AC>spam | |
<__main__.OnlyOne instance at 0076C54C> | |
<__main__.OnlyOne instance at 0076DAAC> | |
<__main__.OnlyOne instance at 0076AA3C> | |
''' | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment