这种在生产计划示例中使用Dictionary <enum,object>的方法是否正确?

der*_*rdo 2 c# oop enums dictionary

考虑具有许多产品的生产计划应用程序.每个产品都有一个InventoryControlType上键入的InventoryControl对象列表.根据我们为生产计划运行的算法,我们需要访问给定产品的不同类型的InventoryControl对象.这很好用.但是,今天我需要在InventoryControl中引入一个包含InventoryControlType的字段,因为我们的算法深入了解InventoryControlType.

但是,我觉得我觉得我做错了,因为看起来我正在重复数据.

这个设计看起来不错吗?有什么改进的想法吗?

class Product{
    Dictionary<InventoryControlType, InventoryControl> InventoryControls;
    GetInventoryControl(InventoryControlType type){
        return InventoryControls[type];
    }
}

class InventoryControl{
    InventoryControlType controlType;
    float limit;
    float cost; 
    ...
    CalculateCost(){...}
    GetConstraint(){...}
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*Tao 6

我觉得你很好.使用对象的唯一属性作为键是完全正常的(至少在我的经验中) - 无论是在a Dictionary,a DataTable还是在你身上.

例如,在我自己的工作中,我们的主要项目具有一个名为Product带有被调用属性的类Symbol,并且应用程序维护一个Dictionary被调用Products,每个Product对象的Symbol属性都作为其键.

可以这样想:如果你有一个带有两个表的数据库,并且一个表通过键引用另一个表中的行,那么你可能觉得你是"复制"数据,因为你有相同的数字(密钥)两个地方.但这不是重复; 这是一个参考.这同样适用于您的方案.