Skip to content

Commit aa1a787

Browse files
committed
updates
1 parent 075adc8 commit aa1a787

13 files changed

+36
-106
lines changed

facade/reports.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44

55
class Reports():
66
"A Singleton Dictionary of Reported Events"
7-
_instance = None
87
_reports: dict[int, tuple[float, str]] = {}
98
_row_id = 0
109

1110
def __new__(cls):
12-
if cls._instance is None:
13-
cls._instance = Reports
14-
return cls._instance
11+
return cls
1512

1613
@classmethod
1714
def get_history(cls) -> dict:

facade/users.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66

77
class Users():
88
"A Singleton Dictionary of Users"
9-
_instance = None
109
_users: dict[str, dict[str, str]] = {}
1110

1211
def __new__(cls):
13-
if cls._instance is None:
14-
cls._instance = Users
15-
return cls._instance
12+
return cls
1613

1714
@classmethod
1815
def register_user(cls, new_user: dict[str, str]) -> str:

facade/wallets.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55

66
class Wallets():
77
"A Singleton Dictionary of User Wallets"
8-
_instance = None
98
_wallets: dict[str, Decimal] = {}
109

1110
def __new__(cls):
12-
if cls._instance is None:
13-
cls._instance = Wallets
14-
return cls._instance
11+
return cls
1512

1613
@classmethod
1714
def create_wallet(cls, user_id: str) -> bool:

flyweight/flyweight_concept.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@ def __init__(self, code: int) -> None:
1616
class FlyweightFactory():
1717
"Creating the FlyweightFactory as a singleton"
1818

19-
_instance = None
2019
_flyweights: dict[int, Flyweight]= {}
2120

2221
def __new__(cls):
23-
if cls._instance is None:
24-
cls._instance = FlyweightFactory
25-
return cls._instance
22+
return cls
2623

2724
@classmethod
2825
def get_flyweight(cls, code: int) -> Flyweight:

flyweight/flyweight_factory.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
class FlyweightFactory():
66
"Creating the FlyweightFactory as a singleton"
77

8-
_instance = None
98
_flyweights: dict[int, Flyweight] = {}
109

1110
def __new__(cls):
12-
if cls._instance is None:
13-
cls._instance = FlyweightFactory
14-
return cls._instance
11+
return cls
1512

1613
@classmethod
1714
def get_flyweight(cls, code: int) -> Flyweight:

img/flyweight_example.svg

Lines changed: 1 addition & 1 deletion
Loading

img/observer_example.svg

Lines changed: 1 addition & 1 deletion
Loading

observer/data_controller.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
class DataController(IDataController):
66
"A Subject (a.k.a Observable)"
77

8-
_instance = None
98
_observers = set()
109

1110
def __new__(cls):
12-
if cls._instance is None:
13-
cls._instance = DataController
14-
return cls._instance
11+
return cls
1512

1613
@classmethod
1714
def subscribe(cls, observer):

singleton/README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Singleton Design Pattern
22

3+
## Videos
4+
5+
Section | Video Link
6+
-|-
7+
Overview | <a id="ytVideoLink" href="https://youtu.be/wc1b70LueGA" target="_blank" title="Overview"><img src="/img/yt_btn_sm.gif" alt="Overview"/></a>
8+
Example Use Case | <a id="ytVideoLink" href="https://youtu.be/-F7OYXMpVJw" target="_blank" title="Use Case"><img src="/img/yt_btn_sm.gif" alt="Use Case"/></a>
9+
Dictionary | <a id="ytVideoLink" href="https://youtu.be/L7IPuo6VOjo" target="_blank" title="Dictionary"><img src="/img/yt_btn_sm.gif" alt="Dictionary"/></a>
10+
311
## Book
412

513
Cover | Links |
@@ -8,7 +16,7 @@ Cover | Links |
816

917
## Overview
1018

11-
Sometimes you need an object in an application where there is only one instance. A constant or static variable for example.
19+
Sometimes you need an object in an application where there is only one instance.
1220

1321
You don't want there to be many versions, for example, you have a game with a score and you want to adjust it. You may have accidentally created several instances of the class holding the score object. Or, you may be opening a database connection, there is no need to create many, when you can use the existing one that is already in memory. You may want a logging component, and you want to ensure all classes use the same instance. So, every class could declare their own logger component, but behind the scenes, they all point to the same memory address (id).
1422

@@ -99,7 +107,9 @@ The Dictionary is similar to a [List](/builder#python-list), except that the ite
99107

100108
The Dictionary can store multiple `key:value` pairs, they can be changed, can be added and removed, can be re-ordered, can be pre-filled with `key:value` pairs when instantiated and is very flexible.
101109

102-
Since Python 3.7, dictionaries are ordered in the same way that they are created and the keys are unique.
110+
Since Python 3.7, dictionaries are ordered in the same way that they are created.
111+
112+
The keys of the dictionary are unique.
103113

104114
You can refer to the dictionary items by key, which will return the value.
105115

@@ -147,8 +157,8 @@ PS> python
147157

148158
## Summary
149159

150-
* To be a Singleton, there must only be one instance of the Singleton, no matter how many times, or in which class it was instantiated.
160+
* To be a Singleton, there must only be one copy of the Singleton, no matter how many times, or in which class it was instantiated.
151161
* You want the attributes or methods to be globally accessible across your application, so that other classes may be able to use the Singleton.
152-
* You can extend classes with a Singleton method, as I did with the leaderboard, but it still points to the same Singleton memory location regardless.
162+
* You can use Singletons in other classes, as I did with the leaderboard, and they will all use the same Singleton regardless.
153163
* You want controlled access to a sole instance.
154164
* For a class to act as a singleton, it should not contain any references to `self`.

singleton/leaderboard.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33

44
class Leaderboard():
55
"The Leaderboard as a Singleton"
6-
_instance = None
76
_table = {}
87

98
def __new__(cls):
10-
if cls._instance is None:
11-
cls._instance = Leaderboard
12-
return cls._instance
9+
return cls
1310

1411
@classmethod
1512
def print(cls):

singleton/multiprocessing_client.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

singleton/multiprocessing_game_state.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

singleton/singleton_concept.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,34 @@
22
"Singleton Concept Sample Code"
33
import copy
44

5-
65
class Singleton():
76
"The Singleton Class"
8-
_instance = None
7+
value = []
98

109
def __new__(cls):
11-
if cls._instance is None:
12-
cls._instance = Singleton
13-
return cls._instance
10+
return cls
11+
12+
# def __init__(self):
13+
# print("in init")
14+
15+
@staticmethod
16+
def static_method():
17+
"Use @staticmethod if no inner variables required"
1418

1519
@classmethod
1620
def class_method(cls):
17-
"A class level method"
18-
21+
"Use @classmethod to access class level variables"
22+
print(cls.value)
1923

2024
# The Client
2125
# All uses of singleton point to the same memory address (id)
2226
print(f"id(Singleton)\t= {id(Singleton)}")
23-
print(f"id(Singleton())\t= {id(Singleton())}")
2427

2528
OBJECT1 = Singleton()
2629
print(f"id(OBJECT1)\t= {id(OBJECT1)}")
2730

2831
OBJECT2 = copy.deepcopy(OBJECT1)
2932
print(f"id(OBJECT2)\t= {id(OBJECT2)}")
3033

31-
# And all singleton class methods will also all point to the same id
32-
print(
33-
f"id(Singleton.class_method())\t= {id(Singleton.class_method())}")
34-
print(
35-
f"id(Singleton().class_method())\t= {id(Singleton().class_method())}")
36-
print(
37-
f"id(OBJECT1.class_method())\t= {id(OBJECT1.class_method())}")
38-
print(
39-
f"id(OBJECT2.class_method())\t= {id(OBJECT2.class_method())}")
34+
OBJECT3 = Singleton()
35+
print(f"id(OBJECT1)\t= {id(OBJECT3)}")

0 commit comments

Comments
 (0)