简体   繁体   English

强制继承对象之间的平等和唯一性

[英]Enforcing Equality and Uniqueness Among Inherited Objects

I have a general question about the structure of my object model. 我对我的对象模型的结构有一个普遍的疑问。 Perhaps I am approaching this with tunnel vision from the wrong direction. 也许我是从错误的方向来解决这个问题的。 I have two classes, Item and SerializedItem. 我有两个类,Item和SerializedItem。 I have defined them as such: 我将它们定义为:

public class Item
{
    public string ItemNumber { get; set; }
    public string Description { get; set; }
    public double Cost { get; set; }
}

public class SerializedItem : Item
{
    public string SerialNumber { get; set; }
    public MyObject Location { get; set; }
}

An Item is a generic definition of an item, and contains information common to that product. 物料是物料的通用定义,包含该产品共有的信息。 SerializedItem is a representation of a specific, physical item. SerializedItem是特定物理项目的表示。 My difficulty lies in the fact that only one Item with a particular ItemNumber should exist in memory at anytime, and I am not sure the best pattern to use to enforce that constraint while allowing a SerializedItem to act as its base type. 我的困难在于,任何时候都只有一个具有特定ItemNumber的Item会存在于内存中,我不确定在允许SerializedItem充当其基本类型的同时,用于执行该约束的最佳模式。

Maybe this is a more appropriate approach? 也许这是一个更合适的方法? I don't have a lot of experience using the 'New' keyword, and I've shied away from using it in the past in favor of an inheritance structure that didn't require its use. 我没有太多使用'New'关键字的经验,并且过去我不使用它,而是使用不需要使用它的继承结构。

public class Item
{
    public string ItemNumber { get; set; }
    public string Description { get; set; }
    public double Cost { get; set; }
}

public class SerializedItem : Item
{
    private Items _item;
    public SerializedItemz(Item item)
    {
        _item = item;
    }

    public new string ItemNumber
    {
        get { return _item.ItemNumber; }
        set { _item.ItemNumber = value; }
    }

    public new string Description
    {
        get { return _item.Description; }
        set { _item.Description = value; }
    }

    public new double Cost
    {
        get { return _item.Cost; }
        set { _item.Cost = value; }
    }

    public string SerialNumber { get; set; }
}

I would appreciate any guidance on how to approach this. 我将对如何处理此问题提供任何指导。 I'm not tied to any particular solution. 我不受任何特定解决方案的束缚。

To provide some clarity: 为了提供一些清晰度:

The Item class is a representation of a particular product, 'Widget A.' Item类是特定产品“小部件A”的表示。 It has information about the Widget A's cost, weight, dimensions, etc. No matter how many Widget As are produced, they all share this information. 它具有有关小部件A的成本,重量,尺寸等的信息。无论生产多少个小部件,它们都共享此信息。

The SerializedItem class is a representation of an actual item in that product line, 'Widget A 001.' SerializedItem类是该产品系列“ Widget A 001”中实际项目的表示。 It contains information about the physical location of that item and it's production and sales history. 它包含有关该项目的实际位置及其生产和销售历史的信息。

If the Item object is updated, all SerializedItems should reflect that change. 如果Item对象已更新,则所有Seri​​alizedItems都应反映该更改。

I am not sure the best pattern to use to enforce that constraint while allowing a SerializedItem to act as its base type 我不确定在允许SerializedItem充当其基本类型的同时用于实施该约束的最佳模式

At first glance a flyweight factory pattern would seem appropriate. 乍一看, 轻量级工厂模式似乎是合适的。 Create a class whose responsibility is to create Item s, keep track of which ones have already been created, and ensure that only one item with a given key is created. 创建一个类,其职责是创建Item ,跟踪已创建的Item ,并确保仅创建具有给定键的一个Item。

You can also build logic into the factory to create different subtypes like SerializedItem - you'd just need to provide the appropriate SPI to determine what type is necessary and collect the necessary inputs. 您还可以将逻辑内置到工厂中以创建不同的子类型,例如SerializedItem您只需要提供适当的SPI即可确定需要哪种类型并收集必要的输入。

A basic implementation would look something like: 基本实现如下所示:

public static class ItemFactory
{
    public static Dictionary<string, Item> _Items = new Dictionary<string, Item>;

    public static Item GetItem(string itemNumber)
    {
        if(!_Items.ContainsKey(itemNumber))
        {
            _Items[itemNumber] = new Item(itemNumber);
            // Initialize item if necessary
        }

        return _Items[itemNumber];
    }
}

The SerializedItem class is a representation of an actual item in that product line SerializedItem类表示该产品线中的实际项目

Than an appropriate design is to make Item an ItemType and use composition instead of inheritance . 比一个合适的设计是使ItemItemType并使用合成代替继承 So your second approach (with the change that SerializedItem does NOT inherit from Item ) looks valid. 因此,您的第二种方法(带有SerializedItem不继承自Item的更改)看起来是有效的。

If Item is truly a non-instantiated base class then mark it as abstract and work through your concrete SerializedItem class ( and any other derived classes you may have ). 如果Item确实是未实例化的基类,则将其标记为抽象,并遍历您的具体SerializedItem类(以及您可能拥有的任何其他派生类)。 If you only want a single Item in memory with a given item number then you might consider a Dictionary type collection indexed on the item number. 如果您只想在内存中使用给定的项目编号的单个项目,则可以考虑在项目编号上建立索引的Dictionary类型集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM