当前位置: 首页 > news >正文

[go] 状态模式

状态模式

允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。

模型说明

  • 上下文 (Context) 保存了对于一个具体状态对象的引用, 并会将所有与该状态相关的工作委派给它。 上下文通过状态接口与状态对象交互, 且会提供一个设置器用于传递新的状态对象。

  • 状态 (State) 接口会声明特定于状态的方法。 这些方法应能被其他所有具体状态所理解, 因为你不希望某些状态所拥有的方法永远不会被调用。

  • 具体状态 (Concrete States) 会自行实现特定于状态的方法。 为了避免多个状态中包含相似代码, 你可以提供一个封装有部分通用行为的中间抽象类。

  • 状态对象可存储对于上下文对象的反向引用。 状态可以通过该引用从上下文处获取所需信息, 并且能触发状态转移。

  • 上下文和具体状态都可以设置上下文的下个状态, 并可通过替换连接到上下文的状态对象来完成实际的状态转换。

优缺点

1.优点

  • 单一职责原则: 将与特定状态相关的代码放在单独的类中。
  • 开闭原则: 无需修改已有状态类和上下文就能引入新状态。
  • 通过消除臃肿的状态机条件语句简化上下文代码。

2.缺点

  • 如果状态机只有很少的几个状态,或者很少发生改变,那么应用该模式可能会显得小题大作。

使用场景

  • 如果对象需要根据自身当前状态进行不同行为,同时状态的数量非常多且与状态相关的代码会频繁变更的话,可使用状态模式。
  • 如果某个类需要根据成员变量的当前值改变自身行为,从而需要使用大量的条件语句时,可使用该模式。
  • 当相似状态和基于条件的状态机转换中存在许多重复代码时,可使用状态模式。

参考代码
一台自动售货机上使用状态设计模式:

  • 有商品 (has­Item)
  • 无商品 (no­Item)
  • 商品已请求 (item­Requested)
  • 收到纸币 (has­Money)

同时, 自动售货机也会有不同的操作。 再一次的, 为了简单起见, 我们假设其只会执行 4 种操作:

  • 选择商品
  • 添加商品
  • 插入纸币
  • 提供商品
// vendingMachine.go 自动售货机
package mainimport "fmt"type VendingMachine struct {hasItem       StateitemRequested StatehasMoney      StatenoItem        StatecurrentState StateitemCount intitemPrice int
}func newVendingMachine(itemCount, itemPrice int) *VendingMachine {v := &VendingMachine{itemCount: itemCount,itemPrice: itemPrice,}hasItemState := &HasItemState{vendingMachine: v,}itemRequestedState := &ItemRequestedState{vendingMachine: v,}hasMoneyState := &HasMoneyState{vendingMachine: v,}noItemState := &NoItemState{vendingMachine: v,}v.setState(hasItemState)v.hasItem = hasItemStatev.itemRequested = itemRequestedStatev.hasMoney = hasMoneyStatev.noItem = noItemStatereturn v
}func (v *VendingMachine) requestItem() error {return v.currentState.requestItem()
}func (v *VendingMachine) addItem(count int) error {return v.currentState.addItem(count)
}func (v *VendingMachine) insertMoney(money int) error {return v.currentState.insertMoney(money)
}func (v *VendingMachine) dispenseItem() error {return v.currentState.dispenseItem()
}func (v *VendingMachine) setState(s State) {v.currentState = s
}func (v *VendingMachine) incrementItemCount(count int) {fmt.Printf("Adding %d items\n", count)v.itemCount = v.itemCount + count
}
// state.go 状态接口
package maintype State interface {addItem(int) errorrequestItem() errorinsertMoney(money int) errordispenseItem() error
}
// noItemState.go 无商品状态
package mainimport "fmt"type NoItemState struct {vendingMachine *VendingMachine
}func (i *NoItemState) requestItem() error {return fmt.Errorf("Item out of stock")
}func (i *NoItemState) addItem(count int) error {i.vendingMachine.incrementItemCount(count)i.vendingMachine.setState(i.vendingMachine.hasItem)return nil
}func (i *NoItemState) insertMoney(money int) error {return fmt.Errorf("Item out of stock")
}
func (i *NoItemState) dispenseItem() error {return fmt.Errorf("Item out of stock")
}
// hasItemState.go 有商品状态
package mainimport "fmt"type HasItemState struct {vendingMachine *VendingMachine
}func (i *HasItemState) requestItem() error {if i.vendingMachine.itemCount == 0 {i.vendingMachine.setState(i.vendingMachine.noItem)return fmt.Errorf("No item present")}fmt.Printf("Item requestd\n")i.vendingMachine.setState(i.vendingMachine.itemRequested)return nil
}func (i *HasItemState) addItem(count int) error {fmt.Printf("%d items added\n", count)i.vendingMachine.incrementItemCount(count)return nil
}func (i *HasItemState) insertMoney(money int) error {return fmt.Errorf("Please select item first")
}
func (i *HasItemState) dispenseItem() error {return fmt.Errorf("Please select item first")
}
// itemRequestedState.go 请求商品
package mainimport "fmt"type ItemRequestedState struct {vendingMachine *VendingMachine
}func (i *ItemRequestedState) requestItem() error {return fmt.Errorf("Item already requested")
}func (i *ItemRequestedState) addItem(count int) error {return fmt.Errorf("Item Dispense in progress")
}func (i *ItemRequestedState) insertMoney(money int) error {if money < i.vendingMachine.itemPrice {return fmt.Errorf("Inserted money is less. Please insert %d", i.vendingMachine.itemPrice)}fmt.Println("Money entered is ok")i.vendingMachine.setState(i.vendingMachine.hasMoney)return nil
}
func (i *ItemRequestedState) dispenseItem() error {return fmt.Errorf("Please insert money first")
}
// hasMoneyState.go 收到钱
package mainimport "fmt"type HasMoneyState struct {vendingMachine *VendingMachine
}func (i *HasMoneyState) requestItem() error {return fmt.Errorf("Item dispense in progress")
}func (i *HasMoneyState) addItem(count int) error {return fmt.Errorf("Item dispense in progress")
}func (i *HasMoneyState) insertMoney(money int) error {return fmt.Errorf("Item out of stock")
}
func (i *HasMoneyState) dispenseItem() error {fmt.Println("Dispensing Item")i.vendingMachine.itemCount = i.vendingMachine.itemCount - 1if i.vendingMachine.itemCount == 0 {i.vendingMachine.setState(i.vendingMachine.noItem)} else {i.vendingMachine.setState(i.vendingMachine.hasItem)}return nil
}
// main.go 客户端
package mainimport ("fmt""log"
)func main() {vendingMachine := newVendingMachine(1, 10)err := vendingMachine.requestItem()if err != nil {log.Fatalf(err.Error())}err = vendingMachine.insertMoney(10)if err != nil {log.Fatalf(err.Error())}err = vendingMachine.dispenseItem()if err != nil {log.Fatalf(err.Error())}fmt.Println()err = vendingMachine.addItem(2)if err != nil {log.Fatalf(err.Error())}fmt.Println()err = vendingMachine.requestItem()if err != nil {log.Fatalf(err.Error())}err = vendingMachine.insertMoney(10)if err != nil {log.Fatalf(err.Error())}err = vendingMachine.dispenseItem()if err != nil {log.Fatalf(err.Error())}
}

output:

Item requestd
Money entered is ok
Dispensing ItemAdding 2 itemsItem requestd
Money entered is ok
Dispensing Item
http://www.lryc.cn/news/446077.html

相关文章:

  • uniapp沉浸式导航栏+自定义导航栏组件
  • 光伏仿真:排布设计如何优化用户体验?
  • Vue使用axios二次封装、解决跨域问题
  • 鸿萌数据恢复:如何降低 RAM 故障风险,以避免数据丢失?
  • 使用java实现ffmpeg的各种操作
  • 【ArcGIS微课1000例】0122:经纬网、方里网、参考格网绘制案例教程
  • 电路板上电子元件检测系统源码分享
  • 综合体第三题(DHCP报文分析)
  • 企业级-pdf预览-前后端
  • 为什么 qt 成为 c++ 界面编程的第一选择?
  • Day1-顺序表
  • PostgreSQL - pgvector 插件构建向量数据库并进行相似度查询
  • UR机器人坐标系转化
  • 【每日一题】LeetCode 2306.公司命名(位运算、数组、哈希表、字符串、枚举)
  • 240922-chromadb的基本使用
  • 工厂模式和抽象工厂模式的实验报告
  • C标准库<string.h>-str、strn开头的函数
  • Anaconda/Miniconda的删除和安装
  • 【Harmony】轮播图特效,持续更新中。。。。
  • Go 并发模式:管道的妙用
  • CAN通信详解
  • 52 文本预处理_by《李沐:动手学深度学习v2》pytorch版
  • 【python】字符串扩展-格式化的精度控制
  • C++第一次练习
  • 计算机毕业设计 基于Python的医疗预约与诊断系统 Django+Vue 前后端分离 附源码 讲解 文档
  • JAVA基础:正则表达式,String的intern方法,StringBuilder可变字符串特点与应用,+连接字符串特点
  • 前端接口报错302 [已解决]
  • 【网络安全】利用未授权API接口实现创建Support Ticket
  • 气压高度加误差的两种方法(直接添加 vs 换算到气压误差),附MATLAB程序
  • Word 制作会议名牌教程