Go设计模式之状态模式讲解和代码示例

 更新时间:2023年08月11日 08:27:09   作者:demo007x  
状态是一种行为设计模式, 让你能在一个对象的内部状态变化时改变其行为,该模式将与状态相关的行为抽取到独立的状态类中, 让原对象将工作委派给这些类的实例, 而不是自行进行处理,本文将通过代码示例给大家简单的介绍一下Go状态模式

Go 状态模式讲解和代码示例

概念示例

让我们在一台自动售货机上使用状态设计模式。 为简单起见, 让我们假设自动售货机仅会销售一种类型的商品。 同时, 依然为了简单起见, 我们假设自动售货机可处于 4 种不同的状态中:

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

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

  • 选择商品
  • 添加商品
  • 插入纸币
  • 提供商品

当对象可以处于许多不同的状态中时应使用状态设计模式, 同时根据传入请求的不同, 对象需要变更其当前状态。

在我们的例子中, 自动售货机可以有多种不同的状态, 同时会在这些状态之间持续不断地互相转换。 我们假设自动售货机处于 商品已请求状态中。 在 “插入纸币” 的操作发生后, 机器将自动转换至 收到纸币状态。

根据其当前状态, 机器可就相同请求采取不同的行为。 例如, 如果用户想要购买一件商品, 机器将在 有商品状态时继续操作, 而在 无商品状态时拒绝操作。

自动售货机的代码不会被这一逻辑污染; 所有依赖于状态的代码都存在于各自的状态实现中。

vendingMachine.go: 背景

package main
import "fmt"
type VendingMachine struct {
	hasItem       State
	itemRequested State
	hasMoney      State
	noItem        State
	currentState State // 当前状态
	itemCount    int
	itemPrice    int
}
func newVendingMachine(itemCount, itemPrice int) *VendingMachine {
	v := &VendingMachine{
		itemCount: itemCount,
		itemPrice: itemPrice,
	}
	hasItemState := &HasItemState{vendingMachine: v}
	itemRequestState := &ItemRequestedState{vendingMachine: v}
	hasMoneyState := &HasMoneyState{vendingMachine: v}
	noItemState := &NoItemState{vendingMachine: v}
	v.setState(hasItemState)
	v.hasItem = hasItemState
	v.itemRequested = itemRequestState
	v.hasMoney = hasMoneyState
	v.noItem = noItemState
	return 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 = count + v.itemCount
}

state.go: 状态接口

package main
type State interface {
	addItem(int) error
	requestItem() error
	insertMoney(money int) error
	dispenseItem() error
}

noItemState.go: 具体状态

package main
import "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 main
import "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 requested \n")
	i.vendingMachine.setState(i.vendingMachine.itemRequested)
	return nil
}
func (i *HasItemState) addItem(count int) error {
	fmt.Printf("%d item added", 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 main
import "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 main
import "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 - 1
	if i.vendingMachine.itemCount == 0 {
		i.vendingMachine.setState(i.vendingMachine.noItem)
	} else {
		i.vendingMachine.setState(i.vendingMachine.hasItem)
	}
	return nil
}

main.go: 客户端代码

package main
import (
	"fmt"
	"log"
)
func main() {
	vendingMachine := newVendingMachine(1, 10)
	// 获取一个商品
	if err := vendingMachine.requestItem(); err != nil {
		log.Fatalf(err.Error())
	}
	if err := vendingMachine.insertMoney(10); err != nil {
		log.Fatal(err.Error())
	}
	if err := vendingMachine.dispenseItem(); err != nil {
		log.Fatal(err.Error())
	}
	fmt.Println("================")
	if err := vendingMachine.addItem(2); err != nil {
		log.Fatal(err.Error())
	}
	fmt.Println()
	if err := vendingMachine.requestItem(); err != nil {
		log.Fatal(err.Error())
	}
	if err := vendingMachine.insertMoney(10); err != nil {
		log.Fatal(err.Error())
	}
	if err := vendingMachine.dispenseItem(); err != nil {
		log.Fatal(err.Error())
	}
}

output.txt: 执行结果

Item requested 
Money entered is ok
Dispensing Item
================
Adding 2 items 

Item requested 
Money entered is ok
Dispensing Item

到此这篇关于Go设计模式之状态模式讲解和代码示例的文章就介绍到这了,更多相关Go状态模式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 在Visual Studio Code中配置GO开发环境的详细教程

    在Visual Studio Code中配置GO开发环境的详细教程

    这篇文章主要介绍了在Visual Studio Code中配置GO开发环境的详细教程,需要的朋友可以参考下
    2017-02-02
  • go-cqhttp权限管理系统的实现代码

    go-cqhttp权限管理系统的实现代码

    这篇文章主要介绍了go-cqhttp权限管理,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-09-09
  • 关于go-zero服务自动收集问题分析

    关于go-zero服务自动收集问题分析

    这篇文章主要介绍了关于go-zero服务自动收集问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-12-12
  • GO接收GET/POST参数及发送GET/POST请求的实例详解

    GO接收GET/POST参数及发送GET/POST请求的实例详解

    这篇文章主要介绍了GO接收GET/POST参数及发送GET/POST请求,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • 精选Golang高频面试题和答案分享

    精选Golang高频面试题和答案分享

    这篇文章给大家整理了17道Go语言中的高频面试题和答案详解,每道题都给出了代码示例,方便大家更好的理解,需要的小伙伴可以收藏一下
    2023-06-06
  • Golang使用lua脚本实现redis原子操作

    Golang使用lua脚本实现redis原子操作

    这篇文章主要介绍了Golang使用lua脚本实现redis原子操作,本文通过实例代码给大家介绍的非常详细,对大家的工作或学习具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03
  • 如何避免Go语言常见错误之意外的变量隐藏

    如何避免Go语言常见错误之意外的变量隐藏

    在Go语言中,变量隐藏(Variable Shadowing)是一个常见的错误来源,变量隐藏发生在一个内部作用域中声明的变量与外部作用域的变量同名时,这可能导致开发者无意中使用了错误的变量,造成难以追踪的bug,本文讲解一些关于变量隐藏的常见错误和如何避免它们的方法
    2024-01-01
  • 深入探讨Golang中如何进行并发发送HTTP请求

    深入探讨Golang中如何进行并发发送HTTP请求

    在 Golang 领域,并发发送 HTTP 请求是优化 Web 应用程序的一项重要技能,本文探讨了实现此目的的各种方法,文中的示例代码讲解详细,希望对大家有所帮助
    2024-01-01
  • go语言中time包的各种函数总结

    go语言中time包的各种函数总结

    时间和日期是我们编程中经常会用到的,下面这篇文章主要给大家介绍了关于go语言中time包的各种函数总结的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-04-04
  • golang时间处理工具箱now的使用详解

    golang时间处理工具箱now的使用详解

    这篇文章主要介绍了golang时间处理工具箱now的使用详解,帮助大家更好的理解和学习使用golang,感兴趣的朋友可以了解下
    2021-02-02

最新评论