摘要:訪問者模式的目的是,解耦資料結構和演算法,使得系統能夠在不改變現有代碼結構的基礎上,為物件新增一種新的操作,
本文分享自華為云社區《【Go實作】實踐GoF的23種設計模式:訪問者模式》,作者:元閏子 ,
簡介
GoF 對訪問者模式(Visitor Pattern)的定義如下:
Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
訪問者模式的目的是,解耦資料結構和演算法,使得系統能夠在不改變現有代碼結構的基礎上,為物件新增一種新的操作,
上一篇介紹的迭代器模式也做到了資料結構和演算法的解耦,不過它專注于遍歷演算法,訪問者模式,則在遍歷的同時,將操作作用到資料結構上,一個常見的應用場景是語法樹的決議,
UML 結構
場景背景關系
在 簡單的分布式應用系統(示例代碼工程)中,db 模塊用來存盤服務注冊和監控資訊,它是一個 key-value 資料庫,另外,我們給 db 模塊抽象出 Table 物件:
// demo/db/table.go package db // Table 資料表定義 type Table struct { name string metadata map[string]int // key為屬性名,value屬性值的索引, 對應到record上存盤 records map[interface{}]record iteratorFactory TableIteratorFactory // 默認使用隨機迭代器 }
目的是提供類似于關系型資料庫的按列查詢能力,比如:
上述的按列查詢只是等值比較,未來還可能會實作正則運算式匹配等方式,因此我們需要設計出可供未來擴展的介面,這種場景,使用訪問者模式正合適,
代碼實作
// demo/db/table_visitor.go package db // 關鍵點1: 定義表查詢的訪問者抽象介面,允許后續擴展查詢方式 type TableVisitor interface { // 關鍵點2: Visit方法以Element作為入參,這里的Element為Table物件 Visit(table *Table) ([]interface{}, error) } // 關鍵點3: 定義Visitor抽象介面的實作物件,這里FieldEqVisitor實作按列等值查詢邏輯 type FieldEqVisitor struct { field string value interface{} } // 關鍵點4: 為FieldEqVisitor定義Visit方法,實作具體的等值查詢邏輯 func (f *FieldEqVisitor) Visit(table *Table) ([]interface{}, error) { result := make([]interface{}, 0) idx, ok := table.metadata[f.field] if !ok { return nil, ErrRecordNotFound } for _, r := range table.records { if reflect.DeepEqual(r.values[idx], f.value) { result = append(result, r) } } if len(result) == 0 { return nil, ErrRecordNotFound } return result, nil } func NewFieldEqVisitor(field string, value interface{}) *FieldEqVisitor { return &FieldEqVisitor{ field: field, value: value, } } // demo/db/table.go package db type Table struct {...} // 關鍵點5: 為Element定義Accept方法,入參為Visitor介面 func (t *Table) Accept(visitor TableVisitor) ([]interface{}, error) { return visitor.Visit(t) }
客戶端可以這么使用:
func client() { table := NewTable("testRegion").WithType(reflect.TypeOf(new(testRegion))) table.Insert(1, &testRegion{Id: 1, Name: "beijing"}) table.Insert(2, &testRegion{Id: 2, Name: "beijing"}) table.Insert(3, &testRegion{Id: 3, Name: "guangdong"}) visitor := NewFieldEqVisitor("name", "beijing") result, err := table.Accept(visitor) if err != nil { t.Error(err) } if len(result) != 2 { t.Errorf("visit failed, want 2, got %d", len(result)) } }
總結實作訪問者模式的幾個關鍵點:
- 定義訪問者抽象介面,上述例子為 TableVisitor, 目的是允許后續擴展表查詢方式,
- 訪問者抽象介面中,Visit 方法以 Element 作為入參,上述例子中, Element 為 Table 物件,
- 為 Visitor 抽象介面定義具體的實作物件,上述例子為 FieldEqVisitor,
- 在訪問者的 Visit 方法中實作具體的業務邏輯,上述例子中 FieldEqVisitor.Visit(...) 實作了按列等值查詢邏輯,
- 在被訪問者 Element 中定義 Accept 方法,以訪問者 Visitor 作為入參,上述例子中為 Table.Accept(...) 方法,
擴展
Go 風格實作
上述實作是典型的面向物件風格,下面以 Go 風格重新實作訪問者模式:
// demo/db/table_visitor_func.go package db // 關鍵點1: 定義一個訪問者函式型別 type TableVisitorFunc func(table *Table) ([]interface{}, error) // 關鍵點2: 定義工廠方法,工廠方法回傳的是一個訪問者函式,實作了具體的訪問邏輯 func NewFieldEqVisitorFunc(field string, value interface{}) TableVisitorFunc { return func(table *Table) ([]interface{}, error) { result := make([]interface{}, 0) idx, ok := table.metadata[field] if !ok { return nil, ErrRecordNotFound } for _, r := range table.records { if reflect.DeepEqual(r.values[idx], value) { result = append(result, r) } } if len(result) == 0 { return nil, ErrRecordNotFound } return result, nil } } // 關鍵點3: 為Element定義Accept方法,入參為Visitor函式型別 func (t *Table) AcceptFunc(visitorFunc TableVisitorFunc) ([]interface{}, error) { return visitorFunc(t) }
客戶端可以這么使用:
func client() { table := NewTable("testRegion").WithType(reflect.TypeOf(new(testRegion))) table.Insert(1, &testRegion{Id: 1, Name: "beijing"}) table.Insert(2, &testRegion{Id: 2, Name: "beijing"}) table.Insert(3, &testRegion{Id: 3, Name: "guangdong"}) result, err := table.AcceptFunc(NewFieldEqVisitorFunc("name", "beijing")) if err != nil { t.Error(err) } if len(result) != 2 { t.Errorf("visit failed, want 2, got %d", len(result)) } }
Go 風格的實作,利用了函式閉包的特點,更加簡潔了,
總結幾個實作關鍵點:
- 定義一個訪問者函式型別,函式簽名以 Element 作為入參,上述例子為 TableVisitorFunc 型別,
- 定義一個工廠方法,工廠方法回傳的是具體的訪問訪問者函式,上述例子為 NewFieldEqVisitorFunc 方法,這里利用了函式閉包的特性,在訪問者函式中直接參考工廠方法的入參,與 FieldEqVisitor 中持有兩個成員屬性的效果一樣,
- 為 Element 定義 Accept 方法,入參為 Visitor 函式型別 ,上述例子是 Table.AcceptFunc(...) 方法,
與迭代器模式結合
訪問者模式經常與迭代器模式一起使用,比如上述例子中,如果你定義的 Visitor 實作不在 db 包內,那么就無法直接訪問 Table 的資料,這時就需要通過 Table 提供的迭代器來實作,
在 簡單的分布式應用系統(示例代碼工程)中,db 模塊存盤的服務注冊資訊如下:
// demo/service/registry/model/service_profile.go package model // ServiceProfileRecord 存盤在資料庫里的型別 type ServiceProfileRecord struct { Id string // 服務ID Type ServiceType // 服務型別 Status ServiceStatus // 服務狀態 Ip string // 服務IP Port int // 服務埠 RegionId string // 服務所屬regionId Priority int // 服務優先級,范圍0~100,值越低,優先級越高 Load int // 服務負載,負載越高表示服務處理的業務壓力越大 }
現在,我們要查詢符合指定 ServiceId 和 ServiceType 的服務記錄,可以這么實作一個 Visitor:
// demo/service/registry/model/service_profile.go package model type ServiceProfileVisitor struct { svcId string svcType ServiceType } func (s *ServiceProfileVisitor) Visit(table *db.Table) ([]interface{}, error) { var result []interface{} // 通過迭代器來遍歷Table的所有資料 iter := table.Iterator() for iter.HasNext() { profile := new(ServiceProfileRecord) if err := iter.Next(profile); err != nil { return nil, err } // 先匹配ServiceId,如果一致則無須匹配ServiceType if profile.Id != "" && profile.Id == s.svcId { result = append(result, profile) continue } // ServiceId匹配不上,再匹配ServiceType if profile.Type != "" && profile.Type == s.svcType { result = append(result, profile) } } return result, nil }
典型應用場景
- k8s 中,kubectl 通過訪問者模式來處理用戶定義的各類資源,
- 編譯器中,通常使用訪問者模式來實作對語法樹決議,比如 LLVM,
- 希望對一個復雜的資料結構執行某些操作,并支持后續擴展,
優缺點
優點
- 資料結構和操作演算法解耦,符合單一職責原則,
- 支持對資料結構擴展多種操作,具備較強的可擴展性,符合開閉原則,
缺點
- 訪問者模式某種程度上,要求資料結構必須對外暴露其內在實作,否則訪問者就無法遍歷其中資料(可以結合迭代器模式來解決該問題),
- 如果被訪問物件內的資料結構變更,可能要更新所有的訪問者實作,
與其他模式的關聯
- 訪問者模式 經常和迭代器模式一起使用,使得被訪問物件無須向外暴露內在資料結構,
- 也經常和組合模式一起使用,比如在語法樹決議中,遞回訪問和決議樹的每個節點(節點組合成樹),
文章配圖
可以在 用Keynote畫出手繪風格的配圖 中找到文章的繪圖方法,
參考
[1] 【Go實作】實踐GoF的23種設計模式:SOLID原則, 元閏子
[2] 【Go實作】實踐GoF的23種設計模式:迭代器模式, 元閏子
[3] Design Patterns, Chapter 5. Behavioral Patterns, GoF
[4] GO 編程模式:K8S VISITOR 模式, 酷殼
[5] 訪問者模式, refactoringguru.cn
點擊關注,第一時間了解華為云新鮮技術~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/511020.html
標籤:其他
下一篇:Python基礎語法:函式
