-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-basic-singleton-pattern.py
More file actions
296 lines (202 loc) · 7.82 KB
/
01-basic-singleton-pattern.py
File metadata and controls
296 lines (202 loc) · 7.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""Question: Create a class Logger with a private class attribute _instance
to implement the Singleton pattern.
Ensure that only one instance of the class can be created.
"""
# LEARNING CHALLENGE
#
# Before looking at any solution below, please try to solve this yourself first!
#
# Tips for success:
# - Read the question carefully
# - Think about what classes and methods you need
# - Start with a simple implementation
# - Test your code step by step
# - Don't worry if it's not perfect - learning is a process!
#
# Remember: The best way to learn programming is by doing, not by reading solutions!
#
# Take your time, experiment, and enjoy the learning process!
# Try to implement your solution here:
# (Write your code below this line)
# HINT SECTION (Only look if you're really stuck!)
#
# Think about:
# - What is the Singleton pattern? (design pattern ensuring only one instance exists)
# - What is the __new__ method? (controls object creation before __init__)
# - How do you store a class-level instance? (class attribute)
# - How do you check if an instance already exists?
#
# Remember: Start simple and build up complexity gradually!
# ===============================================================================
# STEP-BY-STEP SOLUTION
# ===============================================================================
#
# CLASSROOM-STYLE WALKTHROUGH
#
# Let's solve this problem step by step, just like in a programming class!
# Each step builds upon the previous one, so you can follow along and understand
# the complete thought process.
#
# ===============================================================================
# Step 1: Define the Logger class with class attribute
# ===============================================================================
# Explanation:
# Let's start by creating our Logger class with a private class attribute to store the single instance.
# Class attributes are shared by all instances of the class.
class Logger:
_instance = None
# What we accomplished in this step:
# - Created Logger class with private class attribute _instance
# Step 2: Override the __new__ method
# ===============================================================================
# Explanation:
# The __new__ method is called before __init__ and controls object creation.
# We'll use it to ensure only one instance is ever created.
class Logger:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
# What we accomplished in this step:
# - Overrode __new__ method to implement Singleton pattern
# Step 3: Add initialization method
# ===============================================================================
# Explanation:
# Let's add an __init__ method, but we need to be careful not to reinitialize
# the same instance multiple times.
class Logger:
_instance = None
_initialized = False
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self):
if not Logger._initialized:
Logger._initialized = True
print("Logger instance created")
# What we accomplished in this step:
# - Added __init__ method with initialization guard
# Step 4: Add logging functionality
# ===============================================================================
# Explanation:
# Now let's add the actual logging functionality to make our Logger useful.
# This demonstrates that our Singleton works with real methods.
class Logger:
_instance = None
_initialized = False
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self):
if not Logger._initialized:
Logger._initialized = True
print("Logger instance created")
def log(self, message):
print(f"Log: {message}")
def error(self, message):
print(f"ERROR: {message}")
def warning(self, message):
print(f"WARNING: {message}")
# What we accomplished in this step:
# - Added logging methods (log, error, warning)
# Step 5: Add method to get instance (alternative approach)
# ===============================================================================
# Explanation:
# Let's add a class method that provides an alternative way to get the instance.
# This is a common pattern in Singleton implementations.
class Logger:
_instance = None
_initialized = False
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self):
if not Logger._initialized:
Logger._initialized = True
print("Logger instance created")
@classmethod
def get_instance(cls):
if cls._instance is None:
cls._instance = cls()
return cls._instance
def log(self, message):
print(f"Log: {message}")
def error(self, message):
print(f"ERROR: {message}")
def warning(self, message):
print(f"WARNING: {message}")
# What we accomplished in this step:
# - Added get_instance class method for alternative access
# Step 6: Test the Singleton pattern
# ===============================================================================
# Explanation:
# Finally, let's test our Singleton implementation to make sure it works correctly.
# We'll create multiple "instances" and verify they're actually the same object.
class Logger:
_instance = None
_initialized = False
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self):
if not Logger._initialized:
Logger._initialized = True
print("Logger instance created")
@classmethod
def get_instance(cls):
if cls._instance is None:
cls._instance = cls()
return cls._instance
def log(self, message):
print(f"Log: {message}")
def error(self, message):
print(f"ERROR: {message}")
def warning(self, message):
print(f"WARNING: {message}")
# Test our Singleton:
print("Testing Singleton pattern:")
# Create first instance
logger1 = Logger()
# Create second instance
logger2 = Logger()
# Test if they're the same object
print(f"logger1 is logger2: {logger1 is logger2}")
print(f"logger1 id: {id(logger1)}")
print(f"logger2 id: {id(logger2)}")
# Test using class method
logger3 = Logger.get_instance()
print(f"logger1 is logger3: {logger1 is logger3}")
# Test logging functionality
print("\nTesting logging functionality:")
logger1.log("This is a log message from logger1")
logger2.error("This is an error message from logger2")
logger3.warning("This is a warning message from logger3")
print("\nAll loggers are the same instance!")
# What we accomplished in this step:
# - Created and tested our complete Singleton Logger implementation
# - Verified that only one instance exists regardless of how it's created
# ===============================================================================
# CONGRATULATIONS!
#
# You've successfully completed the step-by-step solution!
#
# Key concepts learned:
# - Singleton design pattern implementation
# - __new__ method for controlling object creation
# - Class attributes for shared state
# - Initialization guards to prevent re-initialization
# - Object identity testing with 'is' operator
#
# Try it yourself:
# 1. Start with Step 1 and code along
# 2. Test each step before moving to the next
# 3. Understand WHY each step is necessary
# 4. Experiment with modifications (try adding file logging or different log levels!)
#
# Remember: The best way to learn is by doing!
# ===============================================================================