如何根据传入的类型调用策略模式?

huangapple go评论98阅读模式
英文:

How to call Strategy pattern based on incoming type

问题

我有两种策略。根据请求的数据,我想调用我想要的策略并在一行中执行操作。我该如何实现这个?到目前为止,我的代码是这样的:

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[]进行了一次操作。我能够根据请求中的参数,通过将其与枚举匹配,创建相关的策略并执行操作。我现在记不清了,也找不到任何关于这方面的例子。你能帮帮我吗?

英文:

I have 2 strategies. According to the data from the request, I want to call the strategy I want and perform operations in a single line. How can I achieve this? So far my code is like this

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
}

MY service:

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
}

Years ago I made an operation using make[] . I was able to create the relevant strategy and perform the operations by matching it with an enum according to a parameter in the request. I can't remember it right now, I couldn't find any examples on the internet. Can you help me?

答案1

得分: 2

你可以将所有可用的策略放在一个映射中:

var strategies = map[int]func(repository.Repository) Strategy {
   enum.BRANCH: func(repo repository.Repository) Strategy {
      return NewDistributeBranchStrategy(repo)
   },
   ...
}

然后调用:

s, ok := strategies[x]
if !ok {
  // 错误处理
}
s(repo).Distribute(...)
英文:

You can put all available strategies in a map:

var strategies = map[int]func(repository.Repository) Strategy {
   enum.BRANCH: func(repo repository.Repository) Strategy {
      return NewDistributeBranchStrategy(repo)
   },
   ...
}

Then call:

s, ok: = strategies[x]
if !ok {
  // error
}
s(repo).Distribute(...)

huangapple
  • 本文由 发表于 2023年5月11日 01:17:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定