implemented memento pattern

This commit is contained in:
ismayilmalik 2018-01-17 00:23:17 +03:00
parent 7cda9422a1
commit c6e0b16804
2 changed files with 42 additions and 0 deletions

View File

@ -98,5 +98,8 @@ Define an object that encapsulates how a set of objects interact.Mediator promot
### Template ### Template
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm structure. Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm structure.
### Memento
Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.
## Concurrency patterns ## Concurrency patterns
Pattenrs for concurrent work and parallel execution in Go. Pattenrs for concurrent work and parallel execution in Go.

View File

@ -0,0 +1,39 @@
package memento
import (
"errors"
)
type State struct {
Description string
}
type memento struct {
state State
}
type originator struct {
state State
}
func (o *originator) StoreStateToMemento() memento {
return memento{state: o.state}
}
func (o *originator) StoreStateFromMemento(m memento) {
o.state = m.state
}
type careTaker struct {
mementos []memento
}
func (c *careTaker) Push(m memento) {
c.mementos = append(c.mementos, m)
}
func (c *careTaker) Get(i int) (memento, error) {
var m memento
if len(c.mementos) < i || i< 0 {
return m, errors.New("Index is out of range")
}
return c.mementos[i], nil
}