冬季促销!
备忘录

Go 备忘录模式讲解和代码示例

备忘录是一种行为设计模式 允许生成对象状态的快照并在以后将其还原

备忘录不会影响它所处理的对象的内部结构 也不会影响快照中保存的数据

概念示例

备忘录模式让我们可以保存对象状态的快照 你可使用这些快照来将对象恢复到之前的状态 这在需要在对象上实现撤销-重做操作时非常实用

originator.go: 原发器

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
}

memento.go: 备忘录

package main

type Memento struct {
	state string
}

func (m *Memento) getSavedState() string {
	return m.state
}

caretaker.go: 负责人

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

main.go: 客户端代码

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

}

output.txt: 执行结果

originator Current State: A
originator Current State: B
originator Current State: C
Restored to State: B
Restored to State: A

备忘录在其他编程语言中的实现

C# 备忘录模式讲解和代码示例 C++ 备忘录模式讲解和代码示例 Java 备忘录模式讲解和代码示例 PHP 备忘录模式讲解和代码示例 Python 备忘录模式讲解和代码示例 Ruby 备忘录模式讲解和代码示例 Rust 备忘录模式讲解和代码示例 Swift 备忘录模式讲解和代码示例 TypeScript 备忘录模式讲解和代码示例