|
| 1 | +//#23 - PATRONES DE DISEÑO: SINGLETON |
| 2 | +/* |
| 3 | + * EJERCICIO: |
| 4 | + * Explora el patrón de diseño "singleton" y muestra cómo crearlo |
| 5 | + * con un ejemplo genérico. |
| 6 | + * |
| 7 | + * DIFICULTAD EXTRA (opcional): |
| 8 | + * Utiliza el patrón de diseño "singleton" para representar una clase que |
| 9 | + * haga referencia a la sesión de usuario de una aplicación ficticia. |
| 10 | + * La sesión debe permitir asignar un usuario (id, username, nombre y email), |
| 11 | + * recuperar los datos del usuario y borrar los datos de la sesión. |
| 12 | + */ |
| 13 | + |
| 14 | +let log = console.log; |
| 15 | + |
| 16 | +window.addEventListener('load', ()=>{ |
| 17 | + const body = document.querySelector('body'); |
| 18 | + const title = document.createElement('h1'); |
| 19 | + |
| 20 | + body.style.setProperty('background', '#000'); |
| 21 | + body.style.setProperty('text-align', 'center'); |
| 22 | + |
| 23 | + title.textContent = 'Retosparaprogramadores #23.'; |
| 24 | + title.style.setProperty('font-size', '3.5vmax'); |
| 25 | + title.style.setProperty('color', '#fff'); |
| 26 | + title.style.setProperty('line-height', '100vh'); |
| 27 | + |
| 28 | + body.appendChild(title); |
| 29 | + |
| 30 | + setTimeout(()=>{ |
| 31 | + alert('Retosparaprogramadores #23. Please open the Browser Developer Tools.'); |
| 32 | + }, 2000); |
| 33 | + log( 'Retosparaprogramadores #23'); |
| 34 | +}); |
| 35 | + |
| 36 | + |
| 37 | +/* |
| 38 | +The Singleton design pattern ensures that a class has a single instance and provides a global point of access to that instance. Below, I will show you how to implement the Singleton pattern in JavaScript, followed by an example that represents a class for managing user sessions. |
| 39 | +
|
| 40 | + Implementation of the Singleton Pattern |
| 41 | + */ |
| 42 | + |
| 43 | +class Singleton{ |
| 44 | + constructor() { |
| 45 | + if (Singleton.instance) { |
| 46 | + return Singleton.instance; |
| 47 | + } |
| 48 | + Singleton.instance = this; |
| 49 | + // Initialize any properties you need here |
| 50 | + } |
| 51 | + |
| 52 | + // Example method |
| 53 | + someMethod() { |
| 54 | + log("This is a method of the singleton."); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Using the Singleton |
| 59 | +const instance1 = new Singleton(); |
| 60 | +const instance2 = new Singleton(); |
| 61 | + |
| 62 | +//Note: both variables point to the same instance |
| 63 | +log(instance1 === instance2); // true |
| 64 | + |
| 65 | +//EXTRA DIFICULTY EXERCISE |
| 66 | + |
| 67 | +class UserSession { |
| 68 | + constructor() { |
| 69 | + if (UserSession.instance) { |
| 70 | + return UserSession.instance; |
| 71 | + } |
| 72 | + UserSession.instance = this; |
| 73 | + this.user = null; |
| 74 | + } |
| 75 | + |
| 76 | + setUser(id, username, name, email) { |
| 77 | + this.user = { id, username, name, email }; |
| 78 | + } |
| 79 | + |
| 80 | + getUser() { |
| 81 | + return this.user; |
| 82 | + } |
| 83 | + |
| 84 | + clearSession() { |
| 85 | + this.user = null; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +const session1 = new UserSession(); |
| 90 | +session1.setUser(1, 'FritzCat', 'Fritz Cat', '[email protected]'); |
| 91 | + |
| 92 | +log(session1.getUser()); // Object { id: 1, username: "FritzCat", name: "Fritz Cat", email: "fritzcat@proton.me" } |
| 93 | + |
| 94 | +const session2 = new UserSession(); |
| 95 | +log(session2.getUser()); // Object { id: 1, username: "FritzCat", name: "Fritz Cat", email: "fritzcat@proton.me" } |
| 96 | + |
| 97 | +session2.clearSession(); |
| 98 | +log(session1.getUser()); // null |
0 commit comments