如何实现可以从WPF中派生的Singleton类? [英] How can I implement a Singleton class that can be derived from in WPF?

查看:167
本文介绍了如何实现可以从WPF中派生的Singleton类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间,我了解了Singleton实现,该实现只允许通过隐藏类初始化程序并在其内部使用对象的私有静态引用以及引用该私有引用的公共GETTER来允许类对象的单个实例。 / p>

Some time ago I learned of the Singleton implementation that only permits a single instance of a class object by hiding the class initializer and using a private static reference of the object within itself, and a public GETTER that references that private reference -

public class Foo : IDisposable{
    private static Foo _Instance;
    public static Foo Instance{ get{ return Foo._Instance ?? new Foo(); }
    private Foo(){ Foo._Instance = this; }
    public void Dispose(){ Foo._Instance = null; }
}

我非常喜欢-对于Windows我特别喜欢想要在整个应用程序中都可以访问。

I love this quite a lot - it is especially nice for windows that I want accessible application wide.

我真正想做的一件事是能够实现一种通用的Singleton Window类,可以在其上构建真实的窗口,然后这样访问-这可能吗?我的想法是-

One thing that I would really like is to be able to implement a generic sort of Singleton Window class upon which a real window could be built and then accessed like this - Is this possible? My thinking was something like -

public class SingletonWindow : Window {
    private static SingletonWindow _Instance;
    public static SingletonWindow Instance{ get{ return SingletonWindow._Instance ?? new SingletonWindow(); } }
    private SingletonWindow(){ SingletonWindow._Instance = this; }
    private sealed override void OnClosed(EventArgs E){ SingletonWindow._Instance = null; }
}

但是...我内心深处的声音无法告诉我我认为这绝对肯定会惨败。有人可以告诉我为什么这样做会失败(如果确实会失败),如果有可能实现我在这里试图实现的目标,以及如果可能的话我将如何去做?

But... something inside me that I can't quite voice tells me that this will absolutely positively fail miserably. Can someone tell me why this would fail (if, indeed it will fail), if it is possible to achieve what I am attempting to achieve here, and how I might go about doing so if it is possible?

推荐答案

我个人不喜欢单例。
也就是说,如果您想要一个泛型类,则使其成为泛型类。您必须在派生类上有一个静态构造函数,该静态构造函数将提供到私有构造函数到您的泛型类的路由,仅此而已。

Personally, I'm not a fan of singletons. That said, if you want a generic class, make it a generic class. You will have to have a static constructor on your derived class that will provide a route to the private constructor to your generic class, but that's about it.

public abstract class Singleton<T> where T : Window, Singleton<T>
{
    protected static Func<T> create;
    private static T instance;

    public static T Instance { get { return instance ?? (instance = create()); } }

    private sealed override void OnClosed(EventArgs e)
    { 
        instance = null;
    }
}

public class MyWindow : Singleton<MyWindow>
{
    static MyWindow()
    {
        create = () => new MyWindow();
    }

    private MyWindow() { }
}

然后,您可以像在普通类中那样访问派生类上的实例。

Then you can access the instance on your derived class as if it was a normal singleton.

var myWindow = MyWindow.Instance;

这篇关于如何实现可以从WPF中派生的Singleton类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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