Skip to content

Commit edd9e63

Browse files
authored
Merge pull request #23 from bvwells/memento
Implement memento design pattern #14
2 parents d507164 + a2cef6f commit edd9e63

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Name | Description | Status
6666
[`Interpreter`](./behavioral/interpreter.go) | Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language. |
6767
[`Iterator`](./behavioral/iterator.go) | Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. | Implemented
6868
[`Mediator`](./behavioral/mediator.go) | Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it allows their interaction to vary independently. | Implemented
69-
[`Memento`](./behavioral/memento.go) | Without violating encapsulation, capture and externalize an object's internal state allowing the object to be restored to this state later. |
69+
[`Memento`](./behavioral/memento.go) | Without violating encapsulation, capture and externalize an object's internal state allowing the object to be restored to this state later. | Implemented
7070
[`Observer`](./behavioral/observer.go) | Define a one-to-many dependency between objects where a state change in one object results in all its dependents being notified and updated automatically. | Implemented
7171
[`State`](./behavioral/state.go) | Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. | Implemented
7272
[`Strategy`](./behavioral/strategy.go) | Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. | Implemented

behavioral/memento.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,46 @@
11
package behavioral
2+
3+
// Memento stores the state of the Number.
4+
type Memento struct {
5+
state int
6+
}
7+
8+
// NewMemento creates a new memento.
9+
func NewMemento(value int) *Memento {
10+
return &Memento{value}
11+
}
12+
13+
// Number represents an integer which can be operated on.
14+
type Number struct {
15+
value int
16+
}
17+
18+
// NewNumber creates a new Number.
19+
func NewNumber(value int) *Number {
20+
return &Number{value}
21+
}
22+
23+
// Dubble doubles the value of the number.
24+
func (n *Number) Dubble() {
25+
n.value = 2 * n.value
26+
}
27+
28+
// Half halves the value of the number.
29+
func (n *Number) Half() {
30+
n.value = n.value / 2
31+
}
32+
33+
// Value returns the value of the number.
34+
func (n *Number) Value() int {
35+
return n.value
36+
}
37+
38+
// CreateMemento creates a Memento with the current state of the number.
39+
func (n *Number) CreateMemento() *Memento {
40+
return NewMemento(n.value)
41+
}
42+
43+
// ReinstateMemento reinstates the value of the Number to the value of the memento.
44+
func (n *Number) ReinstateMemento(memento *Memento) {
45+
n.value = memento.state
46+
}

behavioral/memento_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package behavioral
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestMemento(t *testing.T) {
10+
11+
n := NewNumber(10)
12+
n.Dubble()
13+
assert.Equal(t, 20, n.Value())
14+
15+
memento := n.CreateMemento()
16+
17+
n.Half()
18+
assert.Equal(t, 10, n.Value())
19+
20+
n.ReinstateMemento(memento)
21+
assert.Equal(t, 20, n.Value())
22+
}

0 commit comments

Comments
 (0)