How do I create an abstract class that will force each derivative class on being single? I use C #.
This is not possible when you want to compile time checks. You can do this with runtime checking. It's not beautiful, but it's possible. Here's an example:
Public abstract class singleton {Private stationary redone object locker = new object (); Private stationary hashets and objects & gt; Registered Type = New Hashet & lt; Object & gt; (); Protected Singleton () {Lock (locker) {If registered type (this. Gate type ()) (New invalid operation expansion ("Only one instance can be registered.");} Add registered type (this GETType ());}}} Public square repository: Singleton {Repository instance for public static reading = new repository (); Private repository () {}}
Comments
Post a Comment