-
Notifications
You must be signed in to change notification settings - Fork 75
/
Memento.js
61 lines (51 loc) · 1.46 KB
/
Memento.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
class Originator {
constructor() {
console.log('Originator Class created');
this.state = 'a';
console.log('Originator created. State= ' + this.state);
}
setMemento(Memento) {
console.log('Originator.setMemento invoked');
this.state = Memento.getState();
console.log('Originator.setMemento State= ' + this.state);
}
createMemento(state) {
console.log('Originator.createMemento invoked');
return new Memento(state);
}
}
class Memento {
constructor(state) {
console.log('Memento Class created');
this.state = state;
console.log('Memento created. State= ' + this.state);
}
getState() {
console.log('Memento.getState invoked');
return this.state;
}
setState(state) {
console.log('Memento.setState invoked');
this.state = state;
}
}
class Caretaker {
constructor() {
console.log('Caretaker Class created');
this.mementos = [];
}
addMemento(memento) {
console.log('Caretaker.addMemento invoked');
this.mementos.push(memento)
}
setMemento() {
console.log('Caretaker.setMemento invoked');
return this.mementos[this.mementos.length - 1]
}
}
let caretaker = new Caretaker();
let originator = new Originator();
caretaker.addMemento(originator.createMemento('b'));
originator.setMemento(caretaker.setMemento());
console.log(originator.state);