Skip to content

Fix: memento state access #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 42 additions & 43 deletions src/Memento/Book/index.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
6 changes: 1 addition & 5 deletions src/Memento/Conceptual/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
Expand All @@ -87,10 +87,7 @@ class Originator {
* как дата создания или название. Однако он не раскрывает состояние Создателя.
*/
interface Memento {
getState(): string;

getName(): string;

getDate(): string;
}

Expand All @@ -103,7 +100,6 @@ interface Memento {
*/
class ConcreteMemento implements Memento {
private state: string;

private date: string;

constructor(state: string) {
Expand Down
37 changes: 23 additions & 14 deletions src/Memento/RealWorld/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,17 +37,17 @@ class EmployeeOriginator {
this._monthlyExpensesLimit = newLimit;
}

public saveSnapshot(): Memento<EmployeeState> {
return new EmployeeMemento({
public saveSnapshot(): Memento {
return new EmployeeMemento(this,{
salary: this._salary,
monthlyExpensesLimit: this._monthlyExpensesLimit,
});
}

public restore(memento: Memento<EmployeeState>): 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 {
Expand All @@ -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<T> {
readonly state: T;
interface Memento {
readonly name: string;
readonly date: Date;
readonly restore: () => void;
}

/**
Expand All @@ -73,12 +77,14 @@ interface EmployeeState {
monthlyExpensesLimit: number;
}

class EmployeeMemento implements Memento<EmployeeState> {
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)}, \
Expand All @@ -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<EmployeeState>[] = [];

private _employeeMementos: Memento[] = [];
private _employee: EmployeeOriginator;

constructor(employee: EmployeeOriginator) {
Expand All @@ -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 {
Expand Down