How can I implement a Singleton class that can be derived from in WPF?

Will

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; }
}

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

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?

christophano

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;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I implement a Singleton class that can be derived from in WPF?

From Dev

How can I implement @Singleton annotation?

From Dev

how can I obtain HWND of a class derived from QMainWindow

From Dev

How can I retrieve the derived class from a list of abstract classes?

From Dev

how can I obtain HWND of a class derived from QMainWindow

From Dev

How can I retrieve the derived class from a list of abstract classes?

From Dev

Can derived class choose how much properties from the base class to implement?

From Dev

Can derived class choose how much properties from the base class to implement?

From Dev

How can I set the visibility of an element from another class in WPF?

From Dev

How can I create an instance of a derived class from an instance of a base class and include private fields?

From Dev

How can I initialize an instance of a derived class from an instance of the base class?

From Dev

How can I friend a derived class function in the base class?

From Dev

How can I implement asyncio websockets in a class?

From Dev

Can I call a derived method from base class?

From Dev

How can I maintain a compile-time list of all types derived from a base class?

From Dev

How can I avoid dynamically generated classes derived from metaclass not to end up as the same class?

From Dev

How to implement a derived class and call it from a Button?

From Dev

How can I find out which method of a derived class is not implemented?

From Dev

How can I do constructor overloading in a derived class in TypeScript?

From Dev

How can I "un-JsonIgnore" an attribute in a derived class?

From Dev

How can I find out which method of a derived class is not implemented?

From Dev

C++ How can I return an unknown derived class?

From Dev

How can I access a method defined with new keyword in a derived class

From Dev

How can I call the constructor of the derived class when using CRTP?

From Dev

How can I implement a method from a class I extend from in Typescript?

From Dev

How can I combine templated derived class in CRTP with derived class expression templates?

From Dev

How can I make a class both implement an Interface and inherit from another class

From Dev

How can I use NSCoding with a singleton class in Swift

From Dev

How can i tackle State Pattern with Singleton pattern in one class

Related Related

  1. 1

    How can I implement a Singleton class that can be derived from in WPF?

  2. 2

    How can I implement @Singleton annotation?

  3. 3

    how can I obtain HWND of a class derived from QMainWindow

  4. 4

    How can I retrieve the derived class from a list of abstract classes?

  5. 5

    how can I obtain HWND of a class derived from QMainWindow

  6. 6

    How can I retrieve the derived class from a list of abstract classes?

  7. 7

    Can derived class choose how much properties from the base class to implement?

  8. 8

    Can derived class choose how much properties from the base class to implement?

  9. 9

    How can I set the visibility of an element from another class in WPF?

  10. 10

    How can I create an instance of a derived class from an instance of a base class and include private fields?

  11. 11

    How can I initialize an instance of a derived class from an instance of the base class?

  12. 12

    How can I friend a derived class function in the base class?

  13. 13

    How can I implement asyncio websockets in a class?

  14. 14

    Can I call a derived method from base class?

  15. 15

    How can I maintain a compile-time list of all types derived from a base class?

  16. 16

    How can I avoid dynamically generated classes derived from metaclass not to end up as the same class?

  17. 17

    How to implement a derived class and call it from a Button?

  18. 18

    How can I find out which method of a derived class is not implemented?

  19. 19

    How can I do constructor overloading in a derived class in TypeScript?

  20. 20

    How can I "un-JsonIgnore" an attribute in a derived class?

  21. 21

    How can I find out which method of a derived class is not implemented?

  22. 22

    C++ How can I return an unknown derived class?

  23. 23

    How can I access a method defined with new keyword in a derived class

  24. 24

    How can I call the constructor of the derived class when using CRTP?

  25. 25

    How can I implement a method from a class I extend from in Typescript?

  26. 26

    How can I combine templated derived class in CRTP with derived class expression templates?

  27. 27

    How can I make a class both implement an Interface and inherit from another class

  28. 28

    How can I use NSCoding with a singleton class in Swift

  29. 29

    How can i tackle State Pattern with Singleton pattern in one class

HotTag

Archive