I am trying to create a singleton class for one of my apex classes and i am running into challenges. I created some static variables to control the instances but still i am running into multiple instances as static works within the execution context.
See the static boolean variable i use to control the instance. But when i call getInstance, i always end up creating a new instance each time. Can anybody tell me what i am doing wrong and how to create a guaranteed singleton class?
public with sharing class Helper_PK_ScreenDriver {
private static Helper_PK_ScreenDriver instance ;
private static boolean isHelperInstantiated = false;
}
public static Helper_PK_ScreenDriver getInstance(String module,String entityId)
{
System.debug('Helper pk screen driver instance'+isHelperInstantiated + 'instance' +instance);
if(isHelperInstantiated)
{
return instance;
}
else
{
//This part gets executed everytime i call this from outside..
instance = new Helper_PK_ScreenDriver(module,entityId);
isHelperInstantiated = true;
return instance;
}
}