File tree Expand file tree Collapse file tree 1 file changed +25
-2
lines changed
Roadmap/23 - SINGLETON/python Expand file tree Collapse file tree 1 file changed +25
-2
lines changed Original file line number Diff line number Diff line change 1- ## 23 - SINGLETOn
1+ ## 23 - SINGLETON
22'''
33 * EJERCICIO:
44 * Explora el patrón de diseño "singleton" y muestra cómo crearlo
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
You can’t perform that action at this time.
0 commit comments