mirror of
https://github.com/tmrts/go-patterns.git
synced 2025-02-16 12:33:16 +03:00
Added memento pattern
This commit is contained in:
parent
f978e42036
commit
365730a9e6
@ -39,7 +39,7 @@ A curated collection of idiomatic design & application patterns for Go language.
|
|||||||
| [Chain of Responsibility](/behavioral/chain_of_responsibility.md) | Avoids coupling a sender to receiver by giving more than object a chance to handle the request | ✘ |
|
| [Chain of Responsibility](/behavioral/chain_of_responsibility.md) | Avoids coupling a sender to receiver by giving more than object a chance to handle the request | ✘ |
|
||||||
| [Command](/behavioral/command.md) | Bundles a command and arguments to call later | ✘ |
|
| [Command](/behavioral/command.md) | Bundles a command and arguments to call later | ✘ |
|
||||||
| [Mediator](/behavioral/mediator.md) | Connects objects and acts as a proxy | ✘ |
|
| [Mediator](/behavioral/mediator.md) | Connects objects and acts as a proxy | ✘ |
|
||||||
| [Memento](/behavioral/memento.md) | Generate an opaque token that can be used to go back to a previous state | ✘ |
|
| [Memento](/behavioral/memento.md) | Generate an opaque token that can be used to go back to a previous state | ✔ |
|
||||||
| [Observer](/behavioral/observer.md) | Provide a callback for notification of events/changes to data | ✔ |
|
| [Observer](/behavioral/observer.md) | Provide a callback for notification of events/changes to data | ✔ |
|
||||||
| [Registry](/behavioral/registry.md) | Keep track of all subclasses of a given class | ✘ |
|
| [Registry](/behavioral/registry.md) | Keep track of all subclasses of a given class | ✘ |
|
||||||
| [State](/behavioral/state.md) | Encapsulates varying behavior for the same object based on its internal state | ✘ |
|
| [State](/behavioral/state.md) | Encapsulates varying behavior for the same object based on its internal state | ✘ |
|
||||||
|
10
behavioral/memento.md
Normal file
10
behavioral/memento.md
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
The [Memento pattern](https://en.wikipedia.org/wiki/Memento_pattern) allows the state of an object to be saved and restored. It helps us write undo-redo operations through the use of the three componants:
|
||||||
|
|
||||||
|
* Originator: It is the actual object whose state is saved as a memento.
|
||||||
|
* Memento: This is the object which saves the state of the originator
|
||||||
|
* Caretaker: This is the object that saves multiple mementos. Given an index, it returns the corresponding memento.
|
||||||
|
|
||||||
|
The originator defines two methods. savememento() and restorememento()
|
||||||
|
|
||||||
|
* savememento()- in this method the originator saves its internal state into a memento object.
|
||||||
|
* restorememento()- this method takes input as a memento object. The originator restores itself to the pass memento. Hence a previous state is restored.
|
13
behavioral/memento/caretaker.go
Normal file
13
behavioral/memento/caretaker.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type caretaker struct {
|
||||||
|
mementoArray []*memento
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *caretaker) addMemento(m *memento) {
|
||||||
|
c.mementoArray = append(c.mementoArray, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *caretaker) getMemento(index int) *memento {
|
||||||
|
return c.mementoArray[index]
|
||||||
|
}
|
30
behavioral/memento/main.go
Normal file
30
behavioral/memento/main.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
caretaker := &caretaker{
|
||||||
|
mementoArray: make([]*memento, 0),
|
||||||
|
}
|
||||||
|
originator := &originator{
|
||||||
|
state: "A",
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Originator Current State: %s\n", originator.getState())
|
||||||
|
caretaker.addMemento(originator.createMemento())
|
||||||
|
|
||||||
|
originator.setState("B")
|
||||||
|
fmt.Printf("Originator Current State: %s\n", originator.getState())
|
||||||
|
|
||||||
|
caretaker.addMemento(originator.createMemento())
|
||||||
|
originator.setState("C")
|
||||||
|
|
||||||
|
fmt.Printf("Originator Current State: %s\n", originator.getState())
|
||||||
|
caretaker.addMemento(originator.createMemento())
|
||||||
|
|
||||||
|
originator.restoreMemento(caretaker.getMemento(1))
|
||||||
|
fmt.Printf("Restored to State: %s\n", originator.getState())
|
||||||
|
|
||||||
|
originator.restoreMemento(caretaker.getMemento(0))
|
||||||
|
fmt.Printf("Restored to State: %s\n", originator.getState())
|
||||||
|
}
|
9
behavioral/memento/memento.go
Normal file
9
behavioral/memento/memento.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type memento struct{
|
||||||
|
state string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *memento) getSavedState() string{
|
||||||
|
return m.state
|
||||||
|
}
|
21
behavioral/memento/originator.go
Normal file
21
behavioral/memento/originator.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type originator struct {
|
||||||
|
state string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *originator) createMemento() *memento {
|
||||||
|
return &memento{state: e.state}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *originator) restoreMemento(m *memento) {
|
||||||
|
e.state = m.getSavedState()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *originator) setState(state string) {
|
||||||
|
e.state = state
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *originator) getState() string {
|
||||||
|
return e.state
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user