Golang流水線設計模式實踐
到目前為止,我已經將Golang整合到項目中有一段時間了,Golang是一種非常強大的語言,我渴望在其生態系統中進一步磨練技能。
基于項目的特定需求,我需要實現流水線模式(Pipeline Pattern),數據需要通過多個過濾器,以順序的方式進行處理。讓我解釋一下:
假設我們有一個很長的字符串。第一步是根據特定標準對其進行解析。接下來,需要對解析后的數據進行一些調整。隨后,將解析后的數據保存到數據庫中。正如你所見,有多個任務需要處理,而每個任務都是相互關聯的。
基礎流水線
在軟件開發中,可以用流水線設計模式(pipeline design pattern) 來管理這種場景,該模式是為順序處理對象修改而設計的。想象有一條裝配流水線,每個工位都是一段"管道(pipe)",當某個物體通過整個流水線后,就發生了變化。從本質上講,流水線負責將值通過一系列可調用的"管道(pipe)"(無論是中間件、過濾器還是處理器)進行順序傳遞。在將該值傳遞給序列中的后續管道之前,每個管道段都有可能改變該值。該模式在諸如請求處理、數據處理或轉換等場景中特別有用,提供了一種干凈、可維護和可測試的方法。
作為解決方案,我準備了一個簡單的Golang包,可以在處理流程中使用流水線模式,它建立在責任鏈(chain of responsibility, CoR) 設計模式之上,可以將其安裝到項目中并使用:
go get github.com/izniburak/pipeline-go
包安裝之后,可以做一個簡單演示。首先需要一些新的結構體,這些結構體具有從PipeInterface實現的Handle方法。因為pipeline包需要多個流水線,所以我們用Handle方法來運行每個流水線:
package main
import (
"strings"
"github.com/izniburak/pipeline-go"
)
type UpperCasePipe struct{}
func (u *UpperCasePipe) Handle(value pipeline.PipeValue, next pipeline.PipeNext) pipeline.PipeValue {
// get value
text := value.(string)
capitalized := strings.ToUpper(text)
return next(capitalized)
}
type TrimSpacePipe struct{}
func (t *TrimSpacePipe) Handle(value pipeline.PipeValue, next pipeline.PipeNext) pipeline.PipeValue {
// get value
text := value.(string)
trimmed := strings.Trim(text, " ")
return next(trimmed)
}
然后可以開始使用流水線:
package main
import (
"fmt"
"strings"
"github.com/izniburak/pipeline-go"
)
type UpperCasePipe struct{}
func (u *UpperCasePipe) Handle(value pipeline.PipeValue, next pipeline.PipeNext) pipeline.PipeValue {
// get value
text := value.(string)
capitalized := strings.ToUpper(text)
return next(capitalized)
}
type TrimSpacePipe struct{}
func (t *TrimSpacePipe) Handle(value pipeline.PipeValue, next pipeline.PipeNext) pipeline.PipeValue {
// get value
text := value.(string)
trimmed := strings.Trim(text, " ")
return next(trimmed)
}
func main() {
text := " buki.dev "
pipes := []pipeline.PipeInterface{
new(UpperCasePipe),
new(TrimSpacePipe),
}
result := pipeline.Send(text).Through(pipes).ThenReturn()
fmt.Println(result) // BUKI.DEV
}
如你所見,我們使用了兩個不同的管道,分別是UpperCasePipe和TrimSpacePipe。輸入是 buki.dev ,兩邊都有空格,輸出是BUKI.DEV。
就是這樣。流水線非常有用,是吧?
可以在GitHub上查看pipeline包[2]。
參考資料:
- [1]Using a Pipeline Pattern in Golang: https://tech.jotform.com/pipeline-go-pipeline-pattern-in-golang-5a51e5d811a
- [2]pipeline-go: https://github.com/izniburak/pipeline-go