I have an Interface IGeneralEvents
and I have implemented three classes based on it Social
, Sport
and Traditional
. All three of them are using singletons to make sure they are only instantiated once.
The three classes have some additional methods in them which are unique for each one.
I have also created a class to manage these events named EventManager
. Depending on the date and time one of these three events will occur through the EventManager
. The EventManager
will instantiate one of them like below:
public class EventManager {
public EventManager() {
if (time_is_right()) {
Social social_event = Social.getInstance();
}
}
}
In another class I need to know which is the current running event. So my approach of a solution was to do the following:
public class EventManager {
public static IGeneralEvents current_event_instance = null;
public EventManager() {
if (time_is_right()) {
Social social_event = new Social.getInstance();
current_event_instance = social_event;
}
}
}
I created a static variable in the EventManager
of type IGeneralEvents
and tried to pass the instance of the event when it was decided in the constructor. When I do that and I call the current_event_instance
with a method that does not exist in IGeneralEvents
(e.g current_event_instance.chat()
) then I obviously get java: cannot find symbol
as the method is not available in the Interface.
What is the proper way to follow in order to be able to call something like current_event_instance.chat()
from other classes ?