|
| 1 | +/* |
| 2 | + * EJERCICIO: |
| 3 | + * Explora el "Principio SOLID de Inversión de Dependencias (Dependency Inversion |
| 4 | + * Principle, DIP)" y crea un ejemplo simple donde se muestre su funcionamiento |
| 5 | + * de forma correcta e incorrecta. |
| 6 | +*/ |
| 7 | + |
| 8 | +// Forma incorrecta |
| 9 | + |
| 10 | +class Game { |
| 11 | + start() { |
| 12 | + return 'start' |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +class getTypeOfGame { |
| 17 | + constructor() { |
| 18 | + this.game = new Game() |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// Forma correcta |
| 23 | + |
| 24 | +class Game { |
| 25 | + start() { |
| 26 | + return 'start' |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +class GameType { |
| 31 | + constructor(typeOfGame) { |
| 32 | + this.typeOfGame = typeOfGame |
| 33 | + } |
| 34 | + |
| 35 | + displayGame() { |
| 36 | + return this.typeOfGame.start() |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +const gameInstance = new Game() |
| 41 | +const gameType = new GameType(gameInstance) |
| 42 | +console.log(gameType.displayGame()) |
| 43 | + |
| 44 | +/* |
| 45 | + * DIFICULTAD EXTRA (opcional): |
| 46 | + * Crea un sistema de notificaciones. |
| 47 | + * Requisitos: |
| 48 | + * 1. El sistema puede enviar Email, PUSH y SMS (implementaciones específicas). |
| 49 | + * 2. El sistema de notificaciones no puede depender de las implementaciones específicas. |
| 50 | + * Instrucciones: |
| 51 | + * 1. Crea la interfaz o clase abstracta. |
| 52 | + * 2. Desarrolla las implementaciones específicas. |
| 53 | + * 3. Crea el sistema de notificaciones usando el DIP. |
| 54 | + * 4. Desarrolla un código que compruebe que se cumple el principio. |
| 55 | +*/ |
| 56 | + |
| 57 | +class Notification { |
| 58 | + send(message) { |
| 59 | + throw new Error('Método send() debe ser implementado') |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +class EmailNotification extends Notification { |
| 64 | + send(email) { |
| 65 | + console.log(`Sending Email: ${email}`) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class PushNotification extends Notification { |
| 70 | + send(push) { |
| 71 | + console.log(`Sending Push: ${push}`) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +class SMSNotification extends Notification { |
| 76 | + send(sms) { |
| 77 | + console.log(`Sending SMS: ${sms}`) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +class NotificationSystem { |
| 82 | + constructor(notificationService) { |
| 83 | + this.notificationService = notificationService |
| 84 | + } |
| 85 | + |
| 86 | + sendNotification(message) { |
| 87 | + this.notificationService.send(message) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +const emailService = new EmailNotification() |
| 92 | +const pushService = new PushNotification() |
| 93 | +const smsService = new SMSNotification() |
| 94 | + |
| 95 | +const emailSystem = new NotificationSystem(emailService) |
| 96 | +emailSystem.sendNotification('Hello via Email') |
| 97 | + |
| 98 | +const pushSystem = new NotificationSystem(pushService) |
| 99 | +pushSystem.sendNotification('Hello via Push') |
| 100 | + |
| 101 | +const smsSystem = new NotificationSystem(smsService) |
| 102 | +smsSystem.sendNotification('Hello via SMS') |
0 commit comments