首頁  >  文章  >  後端開發  >  如何根據傳入型別呼叫策略模式

如何根據傳入型別呼叫策略模式

王林
王林轉載
2024-02-08 21:12:26400瀏覽

如何根據傳入型別呼叫策略模式

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中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除