forked from bvwells/go-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memento.go
46 lines (37 loc) · 976 Bytes
/
memento.go
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
package behavioral
// Memento stores the state of the Number.
type Memento struct {
state int
}
// NewMemento creates a new memento.
func NewMemento(value int) *Memento {
return &Memento{value}
}
// Number represents an integer which can be operated on.
type Number struct {
value int
}
// NewNumber creates a new Number.
func NewNumber(value int) *Number {
return &Number{value}
}
// Dubble doubles the value of the number.
func (n *Number) Dubble() {
n.value = 2 * n.value
}
// Half halves the value of the number.
func (n *Number) Half() {
n.value /= 2
}
// Value returns the value of the number.
func (n *Number) Value() int {
return n.value
}
// CreateMemento creates a Memento with the current state of the number.
func (n *Number) CreateMemento() *Memento {
return NewMemento(n.value)
}
// ReinstateMemento reinstates the value of the Number to the value of the memento.
func (n *Number) ReinstateMemento(memento *Memento) {
n.value = memento.state
}