Skip to content

Commit c275a96

Browse files
committed
Singleton readme
1 parent dccc3d8 commit c275a96

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

chapter05_singleton/chocolate.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
class SingletonError(Exception):
22
pass
33

4+
45
class Singleton(type):
56
_instances = {}
7+
68
def __call__(cls, *args, **kwargs):
79
if cls not in cls._instances:
810
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
911
return cls._instances[cls]
10-
12+
1113

1214
class ChocolateBoiler(metaclass=Singleton):
1315
def __init__(self) -> None:
1416
self.empty = True
1517
self.boiled = False
16-
18+
1719
def fill(self):
1820
if not self.empty:
1921
raise SingletonError
@@ -47,8 +49,5 @@ def chocolate_controller():
4749
print(boiler2 is boiler)
4850

4951

50-
51-
if __name__ == '__main__':
52+
if __name__ == "__main__":
5253
chocolate_controller()
53-
54-

chapter05_singleton/readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Chapter 5: Singleton design pattern
2+
3+
**Singleton**: ensures a class has only one instance, and provides a global point of access to it.
4+
5+
Based this [stackoverflow post](https://stackoverflow.com/questions/6760685/creating-a-singleton-in-python?noredirect=1&lq=1).

0 commit comments

Comments
 (0)