责任链模式-Golang实现

​责任链模式的定义:是一种行为型设计模式,请求会沿着处理者链进行传递。收到请求后,每个处理者均可对请求进行处理,或将其传递给链的下个处理者。

在实际工作中,常常会遇到功能扩充,有可能会导致代码越来越臃肿或逻辑越来越复杂。维护的程序员也可能不止一个,也会导致一些混乱。

责任链模式会解决这种问题。增加功能时,增加链上的处理者即可。不同的维护者维护自己的处理者。

责任链模式满足单一职责原则,请求和处理者进行了解耦,只要将请求发给一个处理者即可;各个处理者也完成各自的功能。

责任链模式满足开闭原则。可以在不更改现有代码的情况下在程序中新增处理者。

责任链模式中可以控制请求处理的顺序。

下面来看代码实现:

我们来模拟最高指挥部对各个作战部队下达指令的情形。请求者就是最高指挥部(HighCommand),处理者有炮兵部队(Artillery)、导弹部队(MissileForce)、核部队(NuclearForce)。各部队对最高指挥部发出的攻击指令进行各自的处理。UML图如下:

interface.go文件:

package main

type Forces interface {
Execute(*HighCommand)
SetNext(Forces)
}

artillery.go文件:

package main

import "fmt"

type Artillery struct {
next Forces
}

func (f *Artillery) Execute(command *HighCommand) {
if command.ShowCommand() == "shell attack" {
fmt.Println("Shell Attack!")
return
}
fmt.Println("Send to next")
f.next.Execute(command)
}

func (f *Artillery) SetNext(next Forces) {
f.next = next
}

missileForce.go文件:

package main

import "fmt"

type MissileForce struct {
next Forces
}

func (f *MissileForce) Execute(command *HighCommand) {
if command.ShowCommand() == "missile attack" {
fmt.Println("Missile Attack!")
return
}
fmt.Println("Send to next")
f.next.Execute(command)
}

func (f *MissileForce) SetNext(next Forces) {
f.next = next
}

nuclearForce.go文件:

package main

import "fmt"

type NuclearForce struct {
next Forces
}

func (f *NuclearForce) Execute(command *HighCommand) {
if command.ShowCommand() == "nuclear attack" {
fmt.Println("Nuclear Attack!")
return
}
fmt.Println("Send to next")
f.next.Execute(command)
}

func (f *NuclearForce) SetNext(next Forces) {
f.next = next
}

end.go文件(链尾):

package main

type EndChain struct{}

func (f *EndChain) Execute(command *HighCommand) {}

func (f *EndChain) SetNext(next Forces) {}

client.go文件:

package main

type HighCommand struct {
name string
}

func (c *HighCommand) ShowCommand() string {
return c.name
}

main.go文件:

package main

func main() {
// setup chain
end := &EndChain{}
nuclearForce := &NuclearForce{}
nuclearForce.SetNext(end)
missileForce := &MissileForce{}
missileForce.SetNext(nuclearForce)
artillery := &Artillery{}
artillery.SetNext(missileForce)

command := &HighCommand{name: "nuclear attack"}
artillery.Execute(command)
}

运行go run *.go即可。

该例子运行结果为:

名称栏目:责任链模式-Golang实现
分享地址:http://www.csdahua.cn/qtweb/news1/457101.html

网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网