Skip to content

Commit dccc3d8

Browse files
committed
Implement singleton
1 parent 32b187b commit dccc3d8

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

chapter03_decorator/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
**Decorator**: Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
44

5-
Not quite the same as python decorator syntax as in python you call the _decorated function_ and the decorating function is called first whereas the _decorating function_ must be called here.
5+
Not quite the same as python [decorator syntax](https://docs.python.org/3/reference/compound_stmts.html#grammar-token-decorators) as in python you call the _decorated function_ and the decorating function is called first whereas the _decorating function_ must be called here.
66

77
I subclass `ABC` and used the `@abstractmethod` decorator from the `abc` module here but do not use any of this functionality - it just serves as documentation.

chapter04_factory/readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
**Simple Factory**: A class which chooses which product class to instantiate and return, based upon method parameters.
44

5+
The Python standard library contains multiple references to factory objects, for instances in [dataclasses](https://docs.python.org/3/library/dataclasses.html?highlight=factory)
6+
57
**Factory Method**: Defines an interface for creating an object, but lets subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses.
68

79
For instance the `PizzaStore` abstract class in this repo provides an abstract `create_pizza` interface for creating one product.

chapter05_singleton/chocolate.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class SingletonError(Exception):
2+
pass
3+
4+
class Singleton(type):
5+
_instances = {}
6+
def __call__(cls, *args, **kwargs):
7+
if cls not in cls._instances:
8+
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
9+
return cls._instances[cls]
10+
11+
12+
class ChocolateBoiler(metaclass=Singleton):
13+
def __init__(self) -> None:
14+
self.empty = True
15+
self.boiled = False
16+
17+
def fill(self):
18+
if not self.empty:
19+
raise SingletonError
20+
self.empty = False
21+
self.boiled = False
22+
print("Full!")
23+
24+
def drain(self):
25+
if self.empty or (not self.boiled):
26+
raise SingletonError
27+
self.empty = True
28+
print("Empty!")
29+
30+
def boil(self):
31+
self.boiled = True
32+
print("boiled!")
33+
34+
def is_empty(self):
35+
return self.empty
36+
37+
def is_boiled(self):
38+
return self.boiled
39+
40+
41+
def chocolate_controller():
42+
boiler = ChocolateBoiler()
43+
boiler.fill()
44+
boiler.boil()
45+
boiler2 = ChocolateBoiler()
46+
boiler.drain()
47+
print(boiler2 is boiler)
48+
49+
50+
51+
if __name__ == '__main__':
52+
chocolate_controller()
53+
54+

0 commit comments

Comments
 (0)