php小編柚子為您介紹如何根據傳入型別呼叫策略模式。策略模式是一種物件導向的設計模式,它允許根據不同的情況選擇不同的演算法或策略。在實際開發中,我們經常需要根據不同的類型來執行不同的操作。透過使用策略模式,我們可以將這些不同的操作封裝成不同的策略類,並根據傳入的類型來呼叫對應的策略,從而實現靈活的邏輯控制。接下來,我們將詳細介紹如何在PHP中使用策略模式來根據傳入類型呼叫對應的策略。
問題內容
我有兩個策略。根據請求的數據,我想呼叫我想要的策略並在一行中執行操作。我怎樣才能實現這個目標?到目前為止我的程式碼是這樣的
package strategy type strategy interface { distribute(request model.routerequest) (*model.distributeresponse, error) getstrategytype() int }
package strategy type strategy interface { distribute(request model.routerequest) (*model.distributeresponse, error) getstrategytype() int }
package strategies import ( "github.com/x/internal/enum" "github.com/x/internal/model" "github.com/x/internal/repository" ) type distributebranchstrategy struct { repo repository.repository } func newdistributebranchstrategy(repo repository.repository) *distributebranchstrategy { return &distributebranchstrategy{ repo: repo, } } func (d *distributebranchstrategy) distribute(request model.routerequest) (*model.distributeresponse, error) { return nil, nil } func (d *distributebranchstrategy) getstrategytype() int { return enum.branch }
package strategies import ( "github.com/x/internal/enum" "github.com/x/internal/model" "github.com/x/internal/repository" ) type distributetransfercenterstrategy struct { repo repository.repository } func newdistributetransfercenterstrategy(repo repository.repository) *distributetransfercenterstrategy { return &distributetransfercenterstrategy{ repo: repo, } } func (d *distributetransfercenterstrategy) distribute(request model.routerequest) (*model.distributeresponse, error) { return nil, nil } func (d *distributetransfercenterstrategy) getstrategytype() int { return enum.transfer_center }
我的服務:
package service import ( "github.com/x/internal/model" "github.com/x/internal/repository" "github.com/x/internal/strategy/strategies" ) type DistributeService struct { repo repository.Repository distributeBranchStrategy strategies.DistributeBranchStrategy } type Distribute interface { Distribute(vehicleNumberPlate string, request model.DistributeRequest) *model.DistributeResponse } func NewDistributeService(repo repository.Repository, strategy strategies.DistributeBranchStrategy) *DistributeService { return &DistributeService{ repo: repo, distributeBranchStrategy: strategy, } } func (d *DistributeService) Distribute(vehicleNumberPlate string, request model.DistributeRequest) *model.DistributeResponse { // TODO: Implement this method for _, x := range request.RouteRequest { d.distributeBranchStrategy.Distribute(x) } return nil }
幾年前,我使用 make[] 進行了一次操作。我能夠創建相關策略並根據請求中的參數將其與枚舉相匹配來執行操作。我現在不記得了,我在網路上找不到任何例子。你能幫我嗎?
解決方法
您可以將所有可用策略放入地圖中:
var strategies = map[int]func(repository.repository) strategy { enum.branch: func(repo repository.repository) strategy { return newdistributebranchstrategy(repo) }, ... }
然後呼叫:
s, ok: = strategies[x] if !ok { // error } s(repo).Distribute(...)
以上是如何根據傳入型別呼叫策略模式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Go語言的核心特性包括垃圾回收、靜態鏈接和並發支持。 1.Go語言的並發模型通過goroutine和channel實現高效並發編程。 2.接口和多態性通過實現接口方法,使得不同類型可以統一處理。 3.基本用法展示了函數定義和調用的高效性。 4.高級用法中,切片提供了動態調整大小的強大功能。 5.常見錯誤如競態條件可以通過gotest-race檢測並解決。 6.性能優化通過sync.Pool重用對象,減少垃圾回收壓力。

Go語言在構建高效且可擴展的系統中表現出色,其優勢包括:1.高性能:編譯成機器碼,運行速度快;2.並發編程:通過goroutines和channels簡化多任務處理;3.簡潔性:語法簡潔,降低學習和維護成本;4.跨平台:支持跨平台編譯,方便部署。

關於SQL查詢結果排序的疑惑學習SQL的過程中,常常會遇到一些令人困惑的問題。最近,筆者在閱讀《MICK-SQL基礎�...

golang ...

Go語言中如何對比並處理三個結構體在Go語言編程中,有時需要對比兩個結構體的差異,並將這些差異應用到第�...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Atom編輯器mac版下載
最受歡迎的的開源編輯器