I'm using a Singleton-like class to operate session and make the classes to be referenced easily. I have two classes LockPage and HomePage which are lazily initialized. My Singleton-like class is here:
public class Session{
private static LockPage lock;
private static HomePage homePage;
private Session() {
}
public static HomePage getHomePage() {
if(homePage==null) {
homePage = new HomePage();
}
return homePage;
}
public static LockPage getLockPage() {
if(lock==null) {
lock = new LockPage();
}
return lock;
}
public static void resetEverything() {
lock=null;
homePage = null;
}
}
LockPage gets instantiated first from main() and after a successul login, HomePage gets initialized.
On HomePage class if the user clicks logout, this gets called:
public void logOUt(){
Session.getHomePage().disposeScreen();
Session.resetEverything();
Session.getLockPage();
}
I need to use HomePage class in several other classes so I thought referencing like this might be better. Please let me know if this is a GOOD APPROACH or if there is a much better way.
P.S: The Session class is fundamentally not a Singleton