diff --git a/src/Memento/Book/index.ts b/src/Memento/Book/index.ts index 5b679ed..e6dcc6b 100644 --- a/src/Memento/Book/index.ts +++ b/src/Memento/Book/index.ts @@ -1,65 +1,64 @@ export interface Memento { - getName(): string; - getSnapshotDate(): Date; - getText(): string; + getName(): string; + getSnapshotDate(): Date; } class Editor { - private text: string; + private text: string; - constructor(private name: string) { - this.text = ""; - } + constructor(private name: string) { + this.text = ''; + } - makeSnapshot(): Memento { - return new Snapshot(this.name, this.text); - } + makeSnapshot(): Memento { + return new Snapshot(this.name, this.text); + } - restore(memento: Memento) { - this.text = memento.getText(); - } + restore(memento: Memento) { + this.text = (memento as Snapshot).getText(); + } - editText(newText: string) { - this.text = newText; - } + editText(newText: string) { + this.text = newText; + } - displayText() { - console.log("Current Text: " + this.text); - } + displayText() { + console.log(`Current Text: ${this.text}`); + } } class Snapshot implements Memento { - private name: string; - private text: string; - private date: Date; - - constructor(name: string, text: string) { - this.name = name; - this.text = text; - this.date = new Date(); - } - - getName(): string { - return this.name; - } - - getSnapshotDate(): Date { - return this.date; - } - - getText(): string { - return this.text; - } + private name: string; + private text: string; + private date: Date; + + constructor(name: string, text: string) { + this.name = name; + this.text = text; + this.date = new Date(); + } + + getName(): string { + return this.name; + } + + getSnapshotDate(): Date { + return this.date; + } + + getText(): string { + return this.text; + } } -const editor = new Editor("Document 1"); +const editor = new Editor('Document 1'); -editor.editText("This is the initial text"); +editor.editText('This is the initial text'); editor.displayText(); const snapshot1 = editor.makeSnapshot(); -editor.editText("Text after editing"); +editor.editText('Text after editing'); editor.displayText(); editor.restore(snapshot1); diff --git a/src/Memento/Conceptual/index.ts b/src/Memento/Conceptual/index.ts index 77ed9da..69d1f1a 100644 --- a/src/Memento/Conceptual/index.ts +++ b/src/Memento/Conceptual/index.ts @@ -73,7 +73,7 @@ class Originator { * RU: Восстанавливает состояние Создателя из объекта снимка. */ public restore(memento: Memento): void { - this.state = memento.getState(); + this.state = (memento as ConcreteMemento).getState(); console.log(`Originator: My state has changed to: ${this.state}`); } } @@ -87,10 +87,7 @@ class Originator { * как дата создания или название. Однако он не раскрывает состояние Создателя. */ interface Memento { - getState(): string; - getName(): string; - getDate(): string; } @@ -103,7 +100,6 @@ interface Memento { */ class ConcreteMemento implements Memento { private state: string; - private date: string; constructor(state: string) { diff --git a/src/Memento/RealWorld/index.ts b/src/Memento/RealWorld/index.ts index ad47e64..9a87dda 100644 --- a/src/Memento/RealWorld/index.ts +++ b/src/Memento/RealWorld/index.ts @@ -8,7 +8,11 @@ */ const DEFAULT_MONTHLY_EXPENSES_LIMIT = 0; -class EmployeeOriginator { +interface Originator { + saveSnapshot: () => Memento; +} + +class EmployeeOriginator implements Originator { private _name: string; private _salary: number; private _monthlyExpensesLimit: number; @@ -33,17 +37,17 @@ class EmployeeOriginator { this._monthlyExpensesLimit = newLimit; } - public saveSnapshot(): Memento { - return new EmployeeMemento({ + public saveSnapshot(): Memento { + return new EmployeeMemento(this,{ salary: this._salary, monthlyExpensesLimit: this._monthlyExpensesLimit, }); } - public restore(memento: Memento): void { - this._salary = memento.state.salary; - this._monthlyExpensesLimit = memento.state.monthlyExpensesLimit; - console.log(`Originator: Restored state from memento: ${memento.name}`); + public setState(mementoName: string, state: EmployeeState): void { + this._salary = state.salary; + this._monthlyExpensesLimit = state.monthlyExpensesLimit; + console.log(`Originator: Restored state from memento: ${mementoName}`); } public showState(): void { @@ -58,10 +62,10 @@ class EmployeeOriginator { * monthlyExpensesLimit in the memento constructor and moving the restore * method to the memento class instead of the originator */ -interface Memento { - readonly state: T; +interface Memento { readonly name: string; readonly date: Date; + readonly restore: () => void; } /** @@ -73,12 +77,14 @@ interface EmployeeState { monthlyExpensesLimit: number; } -class EmployeeMemento implements Memento { +class EmployeeMemento implements Memento { private _state: EmployeeState; private _date: Date; private _name: string; + private _originator: EmployeeOriginator; - constructor(state: EmployeeState) { + constructor(originator: EmployeeOriginator, state: EmployeeState) { + this._originator = originator; this._state = state; this._date = new Date(); this._name = `date=${this._date.toISOString().substring(0, 10)}, \ @@ -89,14 +95,17 @@ ${this._state.monthlyExpensesLimit}`; get state() { return this._state; } get name() { return this._name; } get date() { return this._date; } + + public restore(): void { + this._originator.setState(this._name, this._state); + } } /** * EN: The Caretaker doesn't need to access the state of the originator. */ class EmployeeCaretaker { - private _employeeMementos: Memento[] = []; - + private _employeeMementos: Memento[] = []; private _employee: EmployeeOriginator; constructor(employee: EmployeeOriginator) { @@ -115,7 +124,7 @@ class EmployeeCaretaker { const employeeMementoToRestore = this._employeeMementos.pop(); console.log(`Employee caretaker: Restoring memento: ${employeeMementoToRestore.name}`); - this._employee.restore(employeeMementoToRestore); + employeeMementoToRestore.restore(); } public showHistory(): void {