首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >地图租赁成本策略

地图租赁成本策略
EN

Code Review用户
提问于 2016-05-19 08:24:10
回答 1查看 93关注 0票数 2

在这种情况下,数据包含一些字段,指示应该选择哪种类型的策略。

我的意思是,我经常忽略解决下面所示的问题,重复策略模式首先要解决的问题。在这种情况下,它是否有一个模式,或者根本不可行?

最近关于我如何解决这个问题的例子:

代码语言:javascript
复制
public class MapRentalCostStrategy : IMapper<EquipmentType, IRentalCostStrategy>
{
    private readonly Dictionary<EquipmentType, IRentalCostStrategy> _knownRentalCostStrategy;

    public MapRentalCostStrategy(IMutablePriceConfiguration mutablePriceConfiguration)
    {
        _knownRentalCostStrategy = new Dictionary<EquipmentType, IRentalCostStrategy>
        {
            { EquipmentType.Heavy, new HeavyRentalCostStrategy(mutablePriceConfiguration)},
            { EquipmentType.Specialized, new SpecializedRentalCostStrategy(mutablePriceConfiguration)},
            { EquipmentType.Regular, new RegularRentalCostStrategy(mutablePriceConfiguration)}
        };
    }

    public Func<EquipmentType, IRentalCostStrategy> Create => equipment =>
    {
        if (!_knownRentalCostStrategy.ContainsKey(equipment))
            throw new ArgumentException();

        return _knownRentalCostStrategy[equipment];
    };
}

其中EquipmentType是一个枚举。

也许我应该注入一个返回此字典的服务?无论如何,我似乎是在委派责任(在这种情况下,它的“对象创建”,不应该是映射者处理字典实例化的责任)。

也许我走错路了?有更好的办法吗?

EN

回答 1

Code Review用户

发布于 2016-05-19 16:49:21

我认为单例/枚举模式将是有益的。奇怪的是,IMutablePriceConfiguration对象似乎因情况而异。

IMutablePriceConfiguration作为成本策略对象的依赖项删除。

下一步:

代码语言:javascript
复制
public sealed class EquipmentType
{
    public static readonly Heavy = new EquipmentType(1, "Heavy", new HeavyRentalCostStrategy());
    public static readonly Specialized = new EquipmentType(2, "Specialized", new SpecializedRentalCostStrategy());
    public static readonly Regular = new EquipmentType(3, "Regular", new RegularRentalCostStrategy());

    public static readonly IEnumerable<EquipmentType> All = new EquipmentType[]
    {
        Heavy,
        Specialized,
        Regular
    };

    public static EquipmentType Find(int id)
    {
        return All.SingleOrDefault(e => e.Id == id);
    }

    public static EquipmentType Find(string name)
    {
        return All.SingleOrDefault(e => e.Name == name);
    }

    private IRentalCostStrategy CostStrategy { get; set; }
    public string Name { get; private set; }
    public int Id { get; private set; }

    private EquipmentType(int id, string name, IRentalCostStrategy costStrategy)
    {
        Id = id;
        Name = name;
        CostStrategy = costStrategy;
    }

    public double CalculateRentalCost(IMutablePriceConfiguration mutablePriceConfiguration)
    {
        return CostStrategy.CalculateRentalCost(mutablePriceConfiguration);
    }
}
  • IMutablePriceConfiguration成为EquipmentType类上的CalculateRenderCost方法的参数。
  • EquipmentType类有一个私有构造函数,因此保证只能使用这三个实例。没有人能够错误地(或恶意地)创建设备类型及其成本策略。
  • 您可以使用类似枚举的语法访问每种设备类型: EquipmentType.Regular。
  • 您可以通过id或名称找到: EquipmentType.Find(1);EquipmentType.Find(“专用”);
  • 您可以盲目地循环所有可用的设备类型: foreach (var equipmentType in EquipmentType.All) { // .}
  • 只要您有一个IMutablePriceConfiguration对象,就可以盲目地调用CalculateRentalCost方法并获得结果。不需要ifs或switchs到处都是你的代码。
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/128735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档