"use strict"; class SafeDepositBoxState { render() { /* Abstract */ } } class CombinationLock { constructor(combination) { this.combination = combination; this.reset(); this.initialCombination = "12345"; } reset() { // Estado inicial de reiniciada this.currentState = new Locked(this); this.currentState.render(); } enterDigit(digit) { if (this.initialCombination === digit) { this.currentState = new Open(this); } else { this.currentState = new Error(this); } this.currentState.render(); } change(newState) { this.currentState = newState; this.currentState.render(); } } class Locked extends SafeDepositBoxState { constructor(context) { super(); this.context = context; } render() { console.log(`La caja fuerte ha sido bloqueada. Ingresa la contraseña...`); } } class Open extends SafeDepositBoxState { constructor(context) { super(); this.context = context; } render() { console.log(`La caja fuerte ha sido abierta.`); console.log(`La caja fuerte se cerrará en 10segundos...`); this.context.change(new Locked(this.context)); } } class Error extends SafeDepositBoxState { constructor(context) { super(); this.context = context; } render() { console.log( `Has intentado introducir una secuencia de dígitos incorrecta.` ); this.context.change(new Locked(this.context)); } } /* En la caja fuerte hay 3 estados * LOCKED: Que la caja ha sido bloqueada, (O estado inicial) * OPEN: La caja fuerte está abierta y el usuario ha digitado el la clave correcta. Después se cerrará la caja fuerte * ERROR: La secuencia es incorrecta, entra en estado error. Y volverá al estado inicial. */ let cajaFuerte = new CombinationLock(); cajaFuerte.enterDigit("123"); /* La caja fuerte ha sido bloqueada. Ingresa la contraseña... Has intentado introducir una secuencia de dígitos incorrecta. La caja fuerte ha sido bloqueada. Ingresa la contraseña... */ cajaFuerte.enterDigit("12345"); /* La caja fuerte ha sido abierta. La caja fuerte se cerrará en 10segundos... La caja fuerte ha sido bloqueada. Ingresa la contraseña... */