0

I have a singleton class that manages the user state, let's say it's called UserStateManager. There are methods that I want to call like login and logout. However, it seems like I always have to do something like this:

UserStateManager.getInstance().login(user);
UserStateManager.getInstance().logout();

But I would like to instead do something like this:

UserStateManager.login(user);
UserStateManager.logout();

And have a static login and logout methods deal with obtaining the singleton instance. But at the same time I want to have instance methods still available in case I am doing multiple things with the singleton:

UserStateManager usm = UserStateManager.getInstance();
usm.login(user);
usm.setLocation(location);
usm.setVerified(true);

However Java doesn't allow static and instance methods to exist with the same signature. It does seem to allow you to call static methods from instances, but then I would have to call getInstance() even if I'm calling the method from an instance, which seems silly, but maybe that's the best option? What do people do in this situation? Just put up with it and call getInstance() all over the place?

JZC
  • 470
  • 5
  • 12
  • If you're using a Singleton, then why do you care about instances? You can have static variables that can be modified. – childofsoong Mar 09 '15 at 23:27
  • Two things spring to mind. First, use the second approach where possible or use method chaining if possible `usm.login(user).setLocation(location).setVerified(true);` or even a builder pattern which you can then pass to the singlton – MadProgrammer Mar 09 '15 at 23:28
  • Don't use singletons and then your problem goes away. "Only create one." – Alan Stokes Mar 09 '15 at 23:28
  • @soong From my experience, a singleton should be used when the implementation is determined at runtime. Not sure if that makes a difference, but it would mean (for me) avoiding static fields ;) – MadProgrammer Mar 09 '15 at 23:29
  • Do you know about `import static`? Saving typing isn't a good reason to use a singleton. – user253751 Mar 09 '15 at 23:33
  • @soong Am I mistaken that a Singleton is a class that can be instantiated a maximum of one time? The getInstance method is a method that obtains the existing instance or creates the first one if it hasn't been instantiated yet. I was under the assumption that this was a fairly common pattern so I didn't go into the details of what getInstance does. If there is a better pattern, let me know. – JZC Mar 09 '15 at 23:43
  • 2
    @JZC unless you're doing inheritance stuff (see https://stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern), there's very little practical difference between doing a Singleton and having a class that has nothing but static methods/fields (the closest you can come to having a truly static class in Java). – childofsoong Mar 09 '15 at 23:45
  • @immibis The goal isn't to use a singleton to save typing. The reality is that I have to use a singleton, and I would like to save typing. If there is a better way of saving user state within an Android app, I'm open to suggestions. – JZC Mar 09 '15 at 23:47
  • @soong Good point. I was under the impression that an object would have to be instantiated in order to maintain state, but it sounds like you can achieve the same thing by having static fields. I'll have to take another look at how my particular class is being used. I may end up going with a class with static methods and fields. My original question still stands, though. – JZC Mar 09 '15 at 23:55
  • You can call getInstance() from your login & logout functions and can avoid writing extra few characters :P – SSC Mar 11 '15 at 00:57

1 Answers1

0

I know this post is old but what about something like having the extra member function:

static void login() { getInstance().login() }

That could be used as:

UserStateManager::login()

But would be a mess if have lots of functions that need to be wrapped...

Ashley Duncan
  • 825
  • 1
  • 8
  • 22