1
0
mirror of https://github.com/tmrts/go-patterns.git synced 2024-11-21 20:46:08 +03:00

Added memento pattern

This commit is contained in:
gmgale 2020-12-24 06:11:19 +01:00
parent f978e42036
commit 365730a9e6
6 changed files with 84 additions and 1 deletions

View File

@ -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 | ✘ |
| [Command](/behavioral/command.md) | Bundles a command and arguments to call later | ✘ |
| [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 | ✔ |
| [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 | ✘ |

10
behavioral/memento.md Normal file
View 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.

View 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]
}

View 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())
}

View File

@ -0,0 +1,9 @@
package main
type memento struct{
state string
}
func (m *memento) getSavedState() string{
return m.state
}

View 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
}