私有静态列表是限制类实例集的适当方法吗? [英] Is a private static list an appropriate way of limiting the set of instances of a class

查看:88
本文介绍了私有静态列表是限制类实例集的适当方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图避免使用相同的内部数据创建一个类的多个实例.我尝试了使用单独的类来构建MCode的实现,但是尝试保护MCode构造函数却无法正常工作,因此我回到了该实现.我想知道这是一个好的设计还是有什么更好的解决方案?

I am trying to avoid multiple instances of a class being created with the same internal data. I tried an implementation with a separate class for building the MCode but trying to protect the MCode constructor did not work so I have come back to this implementation. I wonder if this is good design or what better solution there may be?

public class MCode : IEquatable<MCode>
{

    private readonly static List<MCode> Instances;
    public AEnum AE { get; }
    public byte C { get; }
    public BEnum BE { get; }

    public static MCode GetMCode(AEnum ae, BEnum be, byte c)
    {
            if (Instances==null)
            {
                Instances = new List<MCode>();
                var newmc = new MCode(ae, be, c);
                Instances.Add(newmc);
                return newmc;
            }

            var mc = Instances.Find(x => x.Equals(ae, be, c));

            if (mc == null)
            {
                var newmc = new MCode(ae, be, c);
                Instances.Add(newmc);
                return newmc;
            }
            return mc;
    }

    protected MCode(AEnum ae, BEnum be, byte c)
    {
        AE = ae;
        BE = be;
        C = c;
    }

    public new bool Equals(MCode mc)
    {
        return (GetHashCode() == mc.GetHashCode());
    }

    public new bool Equals(AEnum ae, BEnum be, byte c)
    {
        return (GetHashCode() == GetHashCode(ae, be, c));
    }

    public new int GetHashCode()
    {
        return ((byte)AE * 256 * 256 + (byte)BE * 256 * C);
    }

    public static int GetHashCode(AEnum ae, BEnum be, byte c)
    {
        return ((byte)ae * 256 * 256 + (byte)be * 256 * c);
    }
}

这样做的动机是我有多个类实例,这些实例包含相同的MCode属性,并且我希望它们都使用相同的只读MCode实例.

The motivations for this are that I have multiple instances of classes which contain the same MCode property and I'd like them all to be using the same read-only MCode instance.

推荐答案

您所描述的内容似乎是轻量级工厂模式. Flyweight是相对较小的类,并且有数量有限的唯一"对象,因此维护唯一实例的目录可以帮助减少内存中不必要的重复数据.

What you are describing looks to be a Flyweight Factory pattern. Flyweights are classes that are relatively small, and there are a finite number of "unique" objects, so maintaining a catalog of unique instances can help reduce unnecessarily duplicated data in memory.

一个例子是US State.只有50个唯一状态,因此将50个状态集合为一组唯一实例可能会对系统有所不同,例如,每个用户记录都需要一个状态.

One example is US State. There are only 50 unique states, so keeping a collection of 50 states as a set of unique instances might make a difference in a system that, say, needs a state for every user record.

我也将班级与工厂分开.将工厂设为一个单独的类,并为MCode internal(而不是protected.

I would also separate the class from the factory. Make the factory a separate class, and make the constructor for MCode internal (instead of protected.

我也会注意您的Equals实施.仅仅因为两个对象具有相同的哈希码并不意味着它们相等.在您的情况下可能是正确的,因为int空间可以覆盖的对象数量有限,但是看起来很奇怪.实现 actual Equals逻辑(您已经在列表查找中拥有该逻辑)也将不需要重复的GetHashCode方法.

I would also be careful of your Equals implementation. Just because two objects have the same hash code does not mean that they are equal. It may be true in your case since you have a finite number of objects that can be covered by the int space, but it looks weird. Implementing the actual Equals logic (which you already have in the list lookup) would also negate the need for the duplicate GetHashCode method.

这篇关于私有静态列表是限制类实例集的适当方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆