1

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;
    }   
}
  • 3
    Take a look here for a nice explanation: tgerm.com/2010/03/apex-implementing-singleton-design.html – BritishBoyinDC Apr 1 '13 at 19:58
  • 3
    @BritishBoyinDC this is an appropriate anser to the question in my view you should add it as such, I'll up vote you for one, nice find! – Andrew Fawcett Apr 1 '13 at 20:13
  • 1
    Although that blog post covers how to do a pseudo-singleton in Apex, because Apex is request-scoped there is almost never a justification for doing what he outlines. Static initializers in Apex also tend to execute much more frequently than the developer expects them to. Let us know the reason you think you need a singleton and we can show you the best way how to solve the problem, it's almost certainly with something other than a pseudo-singleton. – jkraybill Apr 2 '13 at 2:17
  • Agree 'static initializers' are not good, didn't spot those being used in the article linked above. Looking again, i would say i typically use the second method referred to as 'ApexAdvancedSingleton'. It has less side effects due to class references. – Andrew Fawcett Apr 2 '13 at 9:13
  • @BritishBoyinDC The post is real good one and i used the apex advanced singleton. But in my use case, i have a 60 step form where i need a singleton class to read an xml file one time and load all the screens. The only question is how to i instantiate my singleton class with class variables like module name for every user who starts the form? I need my instance to stay active till the user completes 60th step and if he starts a new form, i want to create a new instance. How do i do it? – buyan kumar Apr 2 '13 at 13:23
5

If you need a static, stateful singleton, there is no such thing in Apex. That's because static in Apex does not mean the same thing as static in Java. In Apex, none of your Apex can remain in memory after a request, and none of your Apex can share memory. Static is request-scoped.

If you can provide some more detail on what you're trying to achieve, we can try to help you solve your problem; in my experience, genuinely stateful singletons are a rare requirement that almost always have some alternative.

The general solution I'd recommend that most closely resembles a stateful singleton, and allows you to pass information between requests, is some combination of an abstract utility class with static methods and persisting state via either a custom object instance or a custom setting. Locking could be an issue depending on how fancy your state needs to be.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.