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


(相关资料图)

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

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

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

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

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

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

下面来看代码实现:

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

interface.go文件:

package maintype Forces interface {  Execute(*HighCommand)  SetNext(Forces)}

artillery.go文件:

package mainimport "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 mainimport "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 mainimport "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 maintype EndChain struct{}func (f *EndChain) Execute(command *HighCommand) {}func (f *EndChain) SetNext(next Forces) {}

client.go文件:

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

main.go文件:

package mainfunc 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即可。

该例子运行结果为:

推荐内容