欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Golang設(shè)計(jì)模式之責(zé)任鏈模式講解和代碼示例

 更新時間:2023年06月25日 08:58:29   作者:demo007x  
責(zé)任鏈?zhǔn)且环N行為設(shè)計(jì)模式, 允許你將請求沿著處理者鏈進(jìn)行發(fā)送, 直至其中一個處理者對其進(jìn)行處理,本文就詳細(xì)給大家介紹一下Golang 責(zé)任鏈模式,文中有詳細(xì)的代碼示例,需要的朋友可以參考下

Golang 責(zé)任鏈模式講解和代碼示例

該模式允許多個對象來對請求進(jìn)行處理, 而無需讓發(fā)送者類與具體接收者類相耦合。 鏈可在運(yùn)行時由遵循標(biāo)準(zhǔn)處理者接口的任意處理者動態(tài)生成。

概念示例

讓我們來看看一個醫(yī)院應(yīng)用的責(zé)任鏈模式例子。 醫(yī)院中會有多個部門, 如:

  • 前臺
  • 醫(yī)生
  • 藥房
  • 收銀

病人來訪時, 他們首先都會去前臺, 然后是看醫(yī)生、 取藥, 最后結(jié)賬。 也就是說, 病人需要通過一條部門鏈, 每個部門都在完成其職能后將病人進(jìn)一步沿著鏈條輸送。

此模式適用于有多個候選選項(xiàng)處理相同請求的情形, 適用于不希望客戶端選擇接收者 (因?yàn)槎鄠€對象都可處理請求) 的情形, 還適用于想將客戶端同接收者解耦時。 客戶端只需要鏈中的首個元素即可。

正如示例中的醫(yī)院, 患者在到達(dá)后首先去的就是前臺。 然后根據(jù)患者的當(dāng)前狀態(tài), 前臺會將其指向鏈上的下一個處理者。

department.go: 處理者接口

package main
type Department interface {
	execute(*Patient)
	setNext(Department)
}

reception.go: 具體處理者

package main
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)
	}
	fmt.Println("Reception registering patient")
	p.registrationDone = true
	r.next.execute(p)
}
func (r *Reception) setNext(next Department) {
	r.next = next
}

doctor.go: 具體處理者

package main
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
}

medical.go: 具體處理者

package main
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: 具體處理者

package main
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")
}
func (c *Cashier) setNext(next Department) {
	c.next = next
}

patient.go

package main
type Patient struct {
	name              string
	registrationDone  bool // 注冊狀態(tài)
	doctorCheckUpDone bool // 醫(yī)生是否檢查完成
	medicineDone      bool // 是否取完了藥品
	paymentDone       bool // 是否已經(jīng)支付
}

main.go: 客戶端代碼

package main
func main() {
	cashier := &Cashier{}
	// set next for medical department
	medical := &Medical{}
	medical.setNext(cashier)
	//Set next for doctor department
	doctor := &Doctor{}
	doctor.setNext(medical)
	//Set next for reception department
	reception := &Reception{}
	reception.setNext(doctor)
	patient := &Patient{name: "abc"}
	//Patient visiting
	reception.execute(patient)
}

utput.txt: 執(zhí)行結(jié)果

Reception registering patient
Doctor checking patient
Medical giving medicine to patient
Cashier getting money from patient patient

到此這篇關(guān)于Golang設(shè)計(jì)模式之責(zé)任鏈模式講解和代碼示例的文章就介紹到這了,更多相關(guān)Golang 責(zé)任鏈模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論