Go语言实现23种设计模式之行为型模式(中)
admin
2024-04-22 20:22:03
0次

使用Go实现23种设计模式——行为型模式(中)

观察者模式

定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新

适用场景

  1. 对一个对象状态或数据的更新需要其他对象同步更新,或者一个对象的更新需要依赖另一个对象的更新
  2. 对象仅需要将自己的更新通知给其他对象而不需要知道其他对象的细节,如消息推送等

Go语言实现

type Event struct {Data string
}type IObServer interface {Update(event *Event)
}type ISubject interface {Register(server IObServer)Remove(server IObServer)Notify(event *Event)
}type ObServer struct {Id int
}func (s *ObServer) Update(event *Event) {fmt.Printf("observer %d msg: %s", s.Id, event.Data)
}type Subject struct {ObServers map[IObServer]struct{}
}func NewSubject() *Subject {return &Subject{ObServers: make(map[IObServer]struct{})}
}func (s *Subject) Register(server IObServer) {s.ObServers[server] = struct{}{}
}func (s *Subject) Remove(server IObServer) {delete(s.ObServers, server)
}func (s *Subject) Notify(event *Event) {for ob, _ := range s.ObServers {ob.Update(event)}
}func main() {s := NewSubject()o1 := ObServer{Id: 1}o2 := ObServer{Id: 2}s.Register(&o1)s.Register(&o2)s.Notify(&Event{Data: "123"})
}

观察者模式优点

  1. 你无需修改被观察者代码就能引入新的观察者
  2. 你可以在运行时建立对象之间的联系

观察者模式缺点

  1. 观察者的通知顺序是随机的

备忘录模式

在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态

适用场景

  1. 回退场景、撤销重做
  2. 备份与还原、快照与恢复

Go语言实现

type Memento struct {State string
}func (m *Memento) SetState(s string) {m.State = s
}func (m *Memento) GetState() string {return m.State
}func (m *Memento) CreateMemento() *Memento {return &Memento{State: m.State}
}type CareTaker struct {memento *Memento
}func (c *CareTaker) GetMemento() *Memento {return c.memento
}func (c *CareTaker) SetMemento(m *Memento) {c.memento = m
}func main() {o := Memento{State: "running"}fmt.Printf("当前状态: %s", o.State)c := new(CareTaker)c.SetMemento(o.CreateMemento())o.SetState("finish")fmt.Printf("修改后的状态: %s", o.State)o.SetState(c.GetMemento().GetState())fmt.Printf("还原后的状态: %s", o.State)
}

备忘录模式优点

  1. 能够很方便的回到某个历史状态
  2. 实现了信息的封装,使用方不需要关心状态的保存细节

备忘录模式缺点

  1. 消耗资源

中介者模式

定义一个中介对象来简化原有对象之间的交互关系,降低系统中对象间的耦合度,使原有对象之间不必相互了解

适用场景

  1. 系统中的对象之间存在复杂的引用关系,如相互依赖
  2. 存在交互的公共行为,如果需要改变行为可增加中介者类

Go语言实现

type Item interface {SendMsg(msg string)GetMsg(msg string)
}type User struct {mediator *Mediator
}func (u *User) SendMsg(msg string) {u.mediator.ForwardMsg(u, msg)
}func (u *User) GetMsg(msg string) {fmt.Printf("user get msg: %s: ", msg)
}type Server struct {mediator *Mediator
}func (s *Server) SendMsg(msg string) {s.mediator.ForwardMsg(s, msg)
}func (s *Server) GetMsg(msg string) {fmt.Printf("server get msg: %s", msg)
}type Mediator struct {UserServer
}func (m *Mediator) ForwardMsg(item Item, msg string) {switch item.(type) {case *User:m.Server.GetMsg(msg)breakcase *Server:m.User.GetMsg(msg)breakdefault:fmt.Printf("Error")}
}func main() {mediator := Mediator{}u := User{}s := Server{}mediator.User = umediator.Server = su.SendMsg("Hello")s.SendMsg("Hi")
}

中介者模式优点

  1. 减少类间的依赖,降低类间耦合

中介者模式缺点

  1. 当同事类越多时中介者就越臃肿

相关内容

热门资讯

WPK德州局!红龙PoKer软... WPK德州局!红龙PoKer软件透视挂ai,aapoker怎么拿好牌,真是真的是有ai透视挂课程一、...
WPK安卓!AApoker软件... 您好,wpk可以作弊吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩...
WPK链接!红龙扑克技巧辅助器... WPK链接!红龙扑克技巧辅助器ai,来玩德州破解器,果然真的是有ai辅助器窍要1、这是跨平台的来玩德...
WPK透视版!AApoker神... WPK透视版!AApoker神器辅助器ai,哈糖大菠萝辅助器,真是有ai辅助器操作1、WPK透视版!...
WPK安卓!wpk攻略透视ai... WPK安卓!wpk攻略透视ai,约局吧德州真的有透视挂吗,好像是真的有ai透视资料1、每一步都需要思...
WPK透视版!德州扑克软件辅助... WPK透视版!德州扑克软件辅助器ai,约局吧app有挂吗,竟然存在有ai辅助器项目1、下载好约局吧a...
WPK方法!AApoker工具... WPK方法!AApoker工具辅助器ai,拱趴大菠萝十三水作弊,竟然是真的有ai辅助器策略1、游戏颠...
WPK真假!红龙PoKer攻略... WPK真假!红龙PoKer攻略辅助器ai,AApoker中牌率,都是真的是有ai辅助器窍要1、AAp...
WPK透视版!红龙扑克教程开挂... WPK透视版!红龙扑克教程开挂ai,poker红龙辅助,本来真的是有ai开挂积累一、poker红龙辅...
WPK真假!智星菠萝德州工具透... WPK真假!智星菠萝德州工具透视挂ai,德州局透视脚本下载安装最新版本,真是有ai透视挂举措1、完成...