go语言设计模式之visitor
这个确实没有调通,也要记录一下
visitor.go
package visitor import ( "fmt" "io" "os" ) type MessageA struct { Msg string Output io.Writer } func (m *MessageA) Accept(v Visitor) { //nothing v.VisitA(m) } func (m *MessageA) Print() { //nothing if m.Output == nil { m.Output = os.Stdout } fmt.Printf("A: %s\n", m.Msg) fmt.Fprintf(m.Output, "A: %s", m.Msg) } type MessageB struct { Msg string Output io.Writer } func (m *MessageB) Accept(v Visitor) { //nothing v.VisitB(m) } func (m *MessageB) Print() { //nothing if m.Output == nil { m.Output = os.Stdout } fmt.Printf("B: %s\n", m.Msg) fmt.Fprintf(m.Output, "B: %s", m.Msg) } type Visitor interface { VisitA(*MessageA) VisitB(*MessageB) } type Visitable interface { Accept(Visitor) } type MessageVisitor struct{} func (mf *MessageVisitor) VisitA(m *MessageA) { //nothing m.Msg = fmt.Sprintf("%s %s", m.Msg, "(Visited A)") } func (mf *MessageVisitor) VisitB(m *MessageB) { //nothing m.Msg = fmt.Sprintf("%s %s", m.Msg, "(Visited B)") }
visitor_test.go
package visitor import ( "fmt" "testing" ) type TestHelper struct { Received string } func (t TestHelper) Write(p []byte) (int, error) { t.Received = string(p) return len(p), nil } func Test_Overall(t *testing.T) { testHelper := &TestHelper{} visitor := &MessageVisitor{} t.Run("MessageA test", func(t *testing.T) { msg := MessageA{ Msg: "Hello World", Output: testHelper, } msg.Accept(visitor) msg.Print() fmt.Print(testHelper) expected := "A: Hello World (Visited A)" if testHelper.Received != expected { t.Errorf("Expected result was incorrect. %s != %s", testHelper.Received, expected) } }) t.Run("MessageB test", func(t *testing.T) { msg := MessageB{ Msg: "Hello World", Output: testHelper, } msg.Accept(visitor) msg.Print() expected := "B: Hello World (Visited B)" if testHelper.Received != expected { t.Errorf("Expected result was incorrect. %s != %s", testHelper.Received, expected) } }) }
go语言设计模式之visitor的更多相关文章
- Go语言设计模式之函数式选项模式
Go语言设计模式之函数式选项模式 本文主要介绍了Go语言中函数式选项模式及该设计模式在实际编程中的应用. 为什么需要函数式选项模式? 最近看go-micro/options.go源码的时候,发现了一段 ...
- Go语言设计模式汇总
目录 设计模式背景和起源 设计模式是什么 Go语言模式分类 个人观点 Go语言从面世就受到了业界的普遍关注,随着区块链的火热Go语言的地位也急速蹿升,为了让读者对设计模式在Go语言中有一个初步的了解和 ...
- C语言设计模式
一 .C语言和设计模式(继承.封装.多态) C++有三个最重要的特点,即继承.封装.多态.我发现其实C语言也是可以面向对象的,也是可以应用设计模式的,关键就在于如何实现面向对象语言的三个重要属性. ( ...
- 设计模式之visitor模式,人人能懂的有趣实例
设计模式,现在在网上随便搜都一大堆,为什么我还要写"设计模式"的章节呢? 两个原因: 1.本人觉得这是一个有趣的设计模式使用实例,所以记下来: 2.看着设计模式很牛逼,却不知道怎么 ...
- C语言设计模式-封装-继承-多态
快过年了,手头的工作慢慢也就少了,所以,研究技术的时间就多了很多时间,前些天在CSDN一博客看到有大牛在讨论C的设计模式,正好看到了,我也有兴趣转发,修改,研究一下. 记得读大学的时候,老师就告诉我们 ...
- Go语言设计模式实践:迭代器(Iterator)
关于本系列 决定开个新坑. 这个系列首先是关于Go语言实践的.在项目中实际使用Go语言也有段时间了,一个体会就是不论是官方文档.图书还是网络资料,关于Go语言惯用法(idiom)的介绍都比较少,基本只 ...
- Go语言设计模式实践:组合(Composite)
关于本系列 这个系列首先是关于Go语言实践的.在项目中实际使用Go语言也有段时间了,一个体会就是不论是官方文档.图书还是网络资料,关于Go语言惯用法(idiom)的介绍都比较少,基本只能靠看标准库源代 ...
- 设计模式:visitor模式
核心:将数据结构和数据的处理分开 注意:注意函数的参数传递和调用关系 例子: class Element; class Visitor { public: virtual void Visit(Ele ...
- 设计模式之Visitor(访问者)(转)
Visitor定义 作用于某个对象群中各个对象的操作. 它可以使你在不改变这些对象本身的情况下,定义作用于这些对象的新操作. 在Java中,Visitor模式实际上是分离了collection结构中的 ...
随机推荐
- javascript中的slice()方法
JavaScript中的Array对象提供了一个slice()方法,用于从已有的数组中返回选定的元素. arrayObject.slice(start, end) 参数说明 start 必需(否则没有 ...
- Git实战指南----跟着haibiscuit学Git(第十一篇)
笔名: haibiscuit 博客园: https://www.cnblogs.com/haibiscuit/ Git地址: https://github.com/haibiscuit?tab=re ...
- Linux下安装及使用mysql
(注:本人在centos7进行的安装及使用) 1.安装wget yum install wget 2.下载mysql安装包 wget http://repo.mysql.com/mysql57-com ...
- 如何在linux上有2个python的情况下安装gensim
安装python的问题 https://blog.51cto.com/liqingbiao/2083869 安装gensim https://blog.csdn.net/zhujiyao/articl ...
- ORA-27468: ""."" is locked by another process
You have a scheduler job that generated an error. When the error occurred, you attempted to disable ...
- 程序员:May the Force be with you!
程序员如何理解:May the Force be with you! 我们并没有向其他人那样讨论现象级产品的生成原因,因为我们并不清楚这个原因是什么. 我们也不知道足记是否会重复过去一些现象级产品忽生 ...
- Go语言系列:(2)go get 命令介绍
Go语言的代码被托管于 Github.com 网站,该网站是基于 Git 代码管理工具的,很多有名的项目都在该网站托管代码.其他类似的托管网站还有 code.google.com.bitbucket. ...
- centos7.6 jumpserver 堡垒机 重启启动顺序
cd /sdata/usr/local python3. -m venv py3 source /sdata/usr/local/py3/bin/activate cd /sdata/usr/loca ...
- leetcode——链表
206. 反转链表(易) 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->N ...
- 按位或:多项式,FWT,min-max容斥
Description: 刚开始你有一个数字0,每一秒钟你会随机选择一个$[0,2^n)$的数字,与你手上的数字进行或(C++, C 的 |, Pascal 的 or)操作. 选择数字i的概率是$p_ ...