我可以做一个"全球"对象存储变量的多个对象? [英] Can I make a "global" object to store variables for multiple objects?

查看:143
本文介绍了我可以做一个"全球"对象存储变量的多个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个图形应用程序,它会显示多个图表。该图将需要访问一些全局数据和一些特定的图形数据。例如,我要的颜色一致,因此这将是全球性的,但具体的图表可以有不同的栅格间距(每图)。



我创建了一个主对象用默认设置,并与每个图形配置选项



派生类对象

 类GraphMasterObject {
众彩gridcolor =颜色。红;
}

类GraphObject:GraphMasterObject {
公众诠释gridSpacing = 10;
}

现在,从我的理解,我应该能够做到这一点。



  GraphObject去=新GraphObject(); 
颜色C = go.gridColor;



我怎样才能让这个如果我改变go.gridColor,它会在所有的对象更改从GraphMasterObject继承?这甚至可能?如果没有,还有什么其他的解决方案是可能的吗?类似

  GraphMasterObject GMO =新GraphMasterObject(); 
gmo.gridColor = Color.Blue;

  GraphObject去=新GraphObject(); 
go.gridColor = Color.Blue;


解决方案

一种常见的方法有共享一个对象实例。许多对象是Singleton模式



的http:/ /en.wikipedia.org/wiki/Singleton_pattern



使用Singleton模式,任何对象都可以请求单对象,将返回相同的单对象实例作为请求singleton对象的任何其他对象。



这似乎是在您的情况细解。



在C#中的一种方式来实现这种模式是通过使用的静态的属性,例如:

 公共类MySingleton 
{
静态专用MySingleton单身= NULL;

私人静态只读对象挂锁=新的对象();

静态公共MySingleton找回
{
得到
{
锁(锁)
{
如果(单== NULL )单=新MySingleton(); //初始化需要
}
返回单;
}
}
}



用法

  MySingleton单身= MySingleton.Retrieve; 



更新



下面是优于之一以上,由@Marksl

 公共密封类辛格尔顿$ b提供的文章的实现$ b {
私人静态只读懒<辛格尔顿>懒惰=
新懒人<辛格尔顿>(()=>新建辛格尔顿());

公共静态Singleton实例{{返回lazy.Value; }}

私人辛格尔顿()
{
}
}


I am creating a graphing application that will display several graphs. The graphs will need access to some global data and some graph-specific data. For example, I want the colors consistent, so that would be global, but the specific graphs can have different grid spacing (per graph).

I created a "master object" with set defaults and a derived object with per graph configuration options

class GraphMasterObject {
  public Color gridcolor = Color.Red;
}

class GraphObject : GraphMasterObject {
  public int gridSpacing = 10;
}

Now, from my understanding, I should be able to do this

GraphObject go = new GraphObject();
Color c = go.gridColor;

How can I make it so that if I change go.gridColor, it will change across all objects that inherit from GraphMasterObject? Is this even possible? If not, what other solutions are possible? Something like

GraphMasterObject gmo = new GraphMasterObject();
gmo.gridColor = Color.Blue;

or

GraphObject go = new GraphObject();
go.gridColor = Color.Blue;

解决方案

One common approach to have a single object instance shared among many objects is the Singleton Pattern.

http://en.wikipedia.org/wiki/Singleton_pattern

With the Singleton Pattern, any object can request the singleton object and will be returned the same singleton object instance as any other object that requests the singleton object.

That seems like a fine solution in your situation.

One way in C# to implement this pattern is by using a static property, e.g.:

public class MySingleton
{
    static private MySingleton singleton = null;

    private static readonly object padlock = new object();

    static public MySingleton Retrieve
    {
        get
        {
            lock (padlock)
            {
                if (singleton == null) singleton = new MySingleton(); // Initialize as needed
            }
            return singleton;
        }
    }
}

Usage

MySingleton singleton = MySingleton.Retrieve;

UPDATE

Here's an implementation that is superior to the one above, from the article provided by @Marksl

public sealed class Singleton
{
    private static readonly Lazy<Singleton> lazy =
        new Lazy<Singleton>(() => new Singleton());

    public static Singleton Instance { get { return lazy.Value; } }

    private Singleton()
    {
    }
}

这篇关于我可以做一个&QUOT;全球&QUOT;对象存储变量的多个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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