使用Singleton的动态长度数组

时间:2013-05-14 19:35:20

标签: c# arrays singleton

好吧,我在这里超出了我的舒适区,并且正在努力应对新的概念,但我希望我能让自己清楚。
据我了解,全局变量在C#中非常糟糕(并且一般都很危险),但我真的不想进入那场辩论。经过一些research我被引导相信Singletons可以提供帮助。如果我在下面描述的情况出错,请随时提供替代方案。

我要做的是创建一个包含数字数据的动态多维array。此矩阵的大小不同,必须在运行时创建(我通过GUI从日志记录设备中提取数据)。
我认为解决方案是创建一个class,其中包含一个变量,我可以getset,但动态大小。

public class mySingleton
{
    public static int dataSize { get; set; }
    public double[] dataSet = new double[dataSize] { get; set; }               
}

出现这种效果,但显然这是错误的,不起作用。我一直在尝试研究如何在运行时初始化数组,但无法弄清楚,但我也觉得我不知道要搜索哪些术语。有什么帮助吗?

4 个答案:

答案 0 :(得分:3)

您可能想要做的是使用显式(而非隐式)支持字段,以便您可以向getter和setter添加逻辑。像这样:

public class mySingleton
{
    private static int _dataSize;    // you might want to set this to some sensible default
    public static int DataSize 
    { 
        get { return _dataSize; }
        set 
        { 
            _dataSize = value;
            _dataSet = null;        // changing the size will implicitly clear the array - but you could write code to resize if you really wanted to
        }
    }
    private static double[] _dataSet;
    public static double[] DataSet 
    { 
        get 
        {
            if (_dataSet == null) 
            {
                _dataSet = new double[_dataSize];
            }
            return _dataSet;
        }
        // you can include a setter if you want to let the consumer set the dataset directly - in which case it should update the _dataSize field.
    }               
}

答案 1 :(得分:2)

您可能希望初始化数组以响应dataSize属性上的set方法。您将无法使用快速“自动填充”属性(“get; set;”),但这样您就可以在用户设置数据大小时立即初始化数据集。

这样的事情:

public class mySingleton
{
    private static int _dataSize;
    public static int dataSize { 
        get {return _dataSize;} 
        set {
            _dataSize = value;
            dataSet = new double[value];
        } 
    }
    public double[] dataSet { get; private set; }               
}

通常,要设置类的静态属性,可以使用静态构造函数(http://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.80).aspx)或以可以设置静态成员的方式控制对类/数据的访问流在其他人需要使用它们之前。

答案 2 :(得分:1)

您可以轻松创建动态大小的数组:

double[] array = new double[size];

size可以是int类型的任意表达式。所以你的代码看起来像这样:

class ArrayHolder { public static double[] Value; } //global state

//set the global state somewhere else in your code:
var size = DetermineSize();
double[] array = new double[size];
ArrayHolder.Value = array; //publish globally

初始化阵列后,它在整个程序中可用。数组是引用类型,所以这里没有不必要的数据复制。

旁注:为什么你更喜欢单例到静态变量?它们通常具有相同的优点和缺点(IOW没有任何有意义的差异)。在我的例子中,我只使用了一个静态变量。

答案 3 :(得分:0)

我不确定Singleton是否最适合您的方法,但无论如何,这是一个Singleton实现:

public class MatrixSingleton
{
    private static readonly MatrixSingleton instance = new MatrixSingleton();

    static MatrixSingleton() 
    {
    }

    private MatrixSingleton()
    {
        this.Data = new List<Tuple<double, double>>();
    }

    public static MatrixSingleton Instance
    {
        get { return instance; }

    }

    public List<Tuple<double, double>> Data;

}

及其使用

MatrixSingleton matrixSingleton = MatrixSingleton.Instance;
matrixSingleton.Data.Add(new Tuple<double, double>(1.1, 2.2));

有关Singleton模式的更多信息,这些链接可能有所帮助:

http://braindrivendevelopment.com/2013/05/04/simplify-singleton-pattern/

http://www.csharpindepth.com/Articles/General/Singleton.aspx

相关问题