脚本宝典收集整理的这篇文章主要介绍了GoLang设计模式07 - 责任链模式,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
责任链模式是一种行为型设计模式。在这种模式中,会为请求创建一条由多个Handler组成的链路。每一个进入的请求,都会经过这条链路。这条链路上的Handler可以选择如下操作:
下面是责任链模式的用例图:
关于责任链模式的用途最好还是用个案例来说明下。
以医院为例。在一个医院中会有如下职责成员:
当病人来医院看病时,他会先去挂号处挂号,然后找医生看病,看完病后去收银处缴费,最后去药房拿药。在这个过程中,病人需要经过一个由四个职责部门组成的链条。病人在一个职责部门的事务结束后才能进入后续的职责部门,在这个过程中我们可以清晰地看到一条职责链。
现在可以说说责任链模式的适用场景了:
下面是病人到医院看病这个实例的类图:
实现代码如下:
department.go
tyPE department interface { execute(*patient) setNext(department) }
medical.go
import "fmt" type medical struct { next department } func (m *medical) execute(p *patient) { if p.medicineDone { fmt.PRintln("Medicine already given to patient") m.next.execute(p) return } fmt.Println("Medical giving medicine to patient") p.medicineDone = true m.next.execute(p) } func (m *medical) setNext(next department) { m.next = next }
cashier.go
import "fmt" type cashier struct { next department } func (c *cashier) execute(p *patient) { if p.paymentDone { fmt.Println("Payment Done") } fmt.Println("Cashier getting money From patient patient") c.next.execute(p) } func (c *cashier) setNext(next department) { c.next = next }
doctor.go
import "fmt" type doctor struct { next department } func (d *doctor) execute(p *patient) { if p.doctorCheckUpDone { fmt.Println("Doctor checkup already done") d.next.execute(p) return } fmt.Println("Doctor checking patient") p.doctorCheckUpDone = true d.next.execute(p) } func (d *doctor) setNext(next department) { d.next = next }
reception.go
import "fmt" type reception struct { next department } func (r *reception) execute(p *patient) { if p.registrationDone { fmt.Println("Patient registration already done") r.next.execute(p) return } fmt.Println("Reception registering patient") p.registrationDone = true r.next.execute(p) } func (r *reception) setNext(next department) { r.next = next }
patient.go
type patient struct { name string registrationDone bool doctorCheckUpDone bool medicineDone bool paymentDone bool }
main.go
func main() { medical := &medical{} //Set next for cashier department cashier := &cashier{} cashier.setNext(medical) //Set next for doctor department doctor := &doctor{} doctor.setNext(cashier) //Set next for reception department reception := &reception{} reception.setNext(doctor) patient := &patient{name: "abc"} //Patient visITing reception.execute(patient) }
执行结果如下:
Reception registering patient Doctor checking patient Cashier getting money from patient patient Medical giving medicine to patient
代码已上传至GitHub:zhyea / go-patterns / Chain of Responsibility Design Pattern
END!
以上是脚本宝典为你收集整理的GoLang设计模式07 - 责任链模式全部内容,希望文章能够帮你解决GoLang设计模式07 - 责任链模式所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。