Skip to content

Commit 3dea5f0

Browse files
committed
End Basic
1 parent 594fdf3 commit 3dea5f0

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

Roadmap/23 - SINGLETON/python/rigo93acosta.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 23 - SINGLETOn
1+
## 23 - SINGLETON
22
'''
33
* EJERCICIO:
44
* Explora el patrón de diseño "singleton" y muestra cómo crearlo
@@ -11,6 +11,29 @@
1111
* recuperar los datos del usuario y borrar los datos de la sesión.
1212
*/
1313
'''
14+
## refactoring -- guru
15+
16+
## SINGLETON: unica instancia de una clase
17+
'''
18+
Ejercicio
19+
'''
20+
21+
22+
class Singleton:
23+
24+
_instance = None
25+
26+
def __new__(cls):
27+
# Estructura común para implementar Singleton
28+
if not cls._instance:
29+
cls._instance = super(Singleton, cls).__new__(cls)
30+
31+
return cls._instance
32+
33+
singleton_1 = Singleton()
34+
singleton_2 = Singleton()
35+
36+
print(singleton_1 is singleton_2) # Es la misma clase
37+
1438

15-
## Ejercicio
1639

0 commit comments

Comments
 (0)