I have a very specific case. I need to develop a public API for my software, and I have a class, let's say Base
, which extends the Listener
class (that comes from an external API). Now, any specific class users will create will extend Base
, so it will also extend Listener
.
I also want that the user can't instantiate multiple times the same class if it extends Base
- a Listener is used in a static context by other classes and instantiating it multiple times is useless, can cause memory leaks in the software itself and even duplicate events called. I myself don't instantiate Listener
(s) multiple times, but external users can do it and it would be hard to tell them to just stop because my software works by thinking that their class behaves like a Singleton.
Is there a way to say that any class that extends Base
should be a Singleton?
Making the Base
class a Singleton itself is useless, because Base
is extended from multiple classes and Base
itself is never going to be instantiated anyways. I just need a way to make that all classes that extends Base
should be forced to be Singleton or in any way only have a single instance.
I could also make the Base
class an abstract one, but how would I impose the fact that there should be only a single instance for each class which extends Base
at a time?
e.g. Three classes extends Base
: Child1
, Child2
, Child3
. All three should be Singletons and be instanced only a single time.
Aucun commentaire:
Enregistrer un commentaire