memento.go

package memento

import (
	"fmt"
)

type State struct {
	Description string
}

type memento struct {
	state State
}

type originator struct {
	state State
}

func (o *originator) NewMemento() memento {
	return memento{state: o.state}
}

func (o *originator) ExtractAndStoreState(m memento) {
	//Does nothing
	o.state = m.state
}

type careTaker struct {
	mementoList []memento
}

func (c *careTaker) Add(m memento) {
	//Does nothing
	c.mementoList = append(c.mementoList, m)

}

func (c *careTaker) Memento(i int) (memento, error) {
	if len(c.mementoList) < i || i < 0 {
		return memento{}, fmt.Errorf("Index not found\n")
	}
	return c.mementoList[i], nil
}

  

memento_test.go

package memento

import (
	"testing"
)

func TestCareTaker_add(t *testing.T) {
	originator := originator{}
	originator.state = State{Description: "Idle"}

	careTaker := careTaker{}
	mem := originator.NewMemento()
	if mem.state.Description != "Idle" {
		t.Error("Expected state was not found")
	}

	currentLen := len(careTaker.mementoList)
	careTaker.Add(mem)

	if len(careTaker.mementoList) != currentLen+1 {
		t.Error("No new elements were added on the list")
	}
}

func TestCareTaker_Memento(t *testing.T) {
	originator := originator{}
	careTaker := careTaker{}

	originator.state = State{"Idle"}
	careTaker.Add(originator.NewMemento())

	mem, err := careTaker.Memento(0)
	if err != nil {
		t.Fatal(err)
	}

	if mem.state.Description != "Idle" {
		t.Error("Unexpected state")
	}

	mem, err = careTaker.Memento(-1)

	if err == nil {
		t.Fatal("An error is expected when asking for a negative number but no error was found")
	}
}

func TestOriginator_ExtractAndStoreState(t *testing.T) {
	originator := originator{state: State{"Idle"}}
	idleMemento := originator.NewMemento()

	originator.ExtractAndStoreState(idleMemento)
	if originator.state.Description != "Idle" {
		t.Error("Unexpected state found")
	}
}

  

go语言设计模式之memento的更多相关文章

  1. Go语言设计模式之函数式选项模式

    Go语言设计模式之函数式选项模式 本文主要介绍了Go语言中函数式选项模式及该设计模式在实际编程中的应用. 为什么需要函数式选项模式? 最近看go-micro/options.go源码的时候,发现了一段 ...

  2. Go语言设计模式汇总

    目录 设计模式背景和起源 设计模式是什么 Go语言模式分类 个人观点 Go语言从面世就受到了业界的普遍关注,随着区块链的火热Go语言的地位也急速蹿升,为了让读者对设计模式在Go语言中有一个初步的了解和 ...

  3. C语言设计模式-封装-继承-多态

    快过年了,手头的工作慢慢也就少了,所以,研究技术的时间就多了很多时间,前些天在CSDN一博客看到有大牛在讨论C的设计模式,正好看到了,我也有兴趣转发,修改,研究一下. 记得读大学的时候,老师就告诉我们 ...

  4. Go语言设计模式实践:迭代器(Iterator)

    关于本系列 决定开个新坑. 这个系列首先是关于Go语言实践的.在项目中实际使用Go语言也有段时间了,一个体会就是不论是官方文档.图书还是网络资料,关于Go语言惯用法(idiom)的介绍都比较少,基本只 ...

  5. Go语言设计模式实践:组合(Composite)

    关于本系列 这个系列首先是关于Go语言实践的.在项目中实际使用Go语言也有段时间了,一个体会就是不论是官方文档.图书还是网络资料,关于Go语言惯用法(idiom)的介绍都比较少,基本只能靠看标准库源代 ...

  6. C语言设计模式

    一 .C语言和设计模式(继承.封装.多态) C++有三个最重要的特点,即继承.封装.多态.我发现其实C语言也是可以面向对象的,也是可以应用设计模式的,关键就在于如何实现面向对象语言的三个重要属性. ( ...

  7. 设计模式之Memento(备忘机制)

    Memento备望录模式定义:memento是一个保存另外一个对象内部状态拷贝的对象.这样以后就可以将该对象恢复到原先保存的状态. Memento模式相对也比较好理解,我们看下列代码: public ...

  8. 备忘录模式设计模式入门Memento

    //备忘录模式定义: //在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态. //这样以后就能够将该对象恢复到原先保存的状态 //实例:測试两种方案.两种方案在第一阶段的过程 ...

  9. 设计模式之Memento(备忘机制)(转)

    Memento定义: memento是一个保存另外一个对象内部状态拷贝的对象.这样以后就可以将该对象恢复到原先保存的状态. Memento模式相对也比较好理解,我们看下列代码: public clas ...

随机推荐

  1. 同步IO, 异步IO的理解

    1. 什么是IO? 在计算机中无时无刻不存在着对数据的访问和读取(数据都存储在物理的媒介上,例如寄存器,高速缓存,内存,磁盘,网卡等等),这些操作被称为IO. 2. 阻塞IO (1)当用户线程发起IO ...

  2. Parallel.ForEach 使用多线遍历循环

    Parallel.ForEach相对于foreach是多线程,并行操作;foreach是单线程品德操作. static void Main(string[] args) { Console.Write ...

  3. delphi使用Chilkat 组件和库从SFTP下载文件

    官网地址:https://www.example-code.com/delphiDll/default.asp 实例代码:(不包括全局解锁)  密码生成器:https://www.cnblogs.co ...

  4. 【转载】C#string.Formart的字符串格式化

    String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项.Stri ...

  5. 百度地图在jsp页面加载大量轨迹导致地图卡顿

    原画线方式: //存储大量点轨迹json数组:historyPathList for(var i=0;i<historyPathList.length-1;i++){ drawColorLine ...

  6. SQL索引碎片的产生,处理过程。

    本文参考 https://www.cnblogs.com/CareySon/archive/2011/12/22/2297568.html https://www.jb51.net/softjc/12 ...

  7. spring+cxf No bean named 'cxf' available

    最近项目中需要用到webservice,在spring中集成cxf时一直报错: 严重: StandardWrapper.Throwable org.springframework.beans.fact ...

  8. 学习Linux必看的命令(一)

    学习Linux必看的命令(一) 一:什么是Linux ​ Linux命令是对Linux系统进行管理的命令.对于Linux系统来说,无论是中央处理器.内存.磁盘驱动器.键盘.鼠标,还是用户等都是文件,L ...

  9. mysql登录错误:'Access denied for user 'root'@'localhost'

    首先是不知道怎么忽然mysql用命令行,workbench都登录不了,都提示'Access denied for user 'root'@'localhost'. 数据库卸载重装了几次都不行.好像感觉 ...

  10. 12c Data guard Switchover Best Practices using SQLPLUS (Doc ID 1578787.1)

    12c Data guard Switchover Best Practices using SQLPLUS (Doc ID 1578787.1) APPLIES TO: Oracle Databas ...