前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >责任链模式是一种行为型设计模式

责任链模式是一种行为型设计模式

原创
作者头像
用户7108768
修改2021-10-08 14:08:36
1650
修改2021-10-08 14:08:36
举报

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档