Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ __Creational Patterns__:
| [lazy_evaluation](lazy_evaluation.py) | lazily-evaluated property pattern in Python |
| [pool](pool.py) | preinstantiate and maintain a group of instances of the same type |
| [prototype](prototype.py) | use a factory and clones of a prototype for new instances (if instantiation is expensive) |
| [singleton](singleton.py) | a singleton with overriding __new__ method |

__Structural Patterns__:

Expand Down
50 changes: 50 additions & 0 deletions singleton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


class Singleton(object):
"""
Assume that only one instance should be created of this class and
it is counting the users connected to the system.
"""

_instance = None

def __new__(cls, *args, **kwargs):
"""
For each class, creating only one instance.
If an instance has been created before, returns the created one.
"""
if not cls._instance:
cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)

# your init codes here
cls.user_count = 0
# end of your init code

return cls._instance

def connect(self):
self.user_count += 1

def disconnect(self):
self.user_count -= 1


if __name__ == '__main__':

s1 = Singleton()
s2 = Singleton()

if id(s1) == id(s2):
print "Same instances"
else:
print "Different instances"

s1.connect() # user count: 1
s1.connect() # user count: 2
s2.connect() # same instance with s1, so user count: 3 for s1 and s2
s1.disconnect() # same instance with s2, so user count: 2 for s1 and s2

print s1.user_count
print s2.user_count