1

In the book Effective Java he give the best Singleton pattern implementation in his, that is implement by a Enum. I have doubt to how to implement this pattern with a third party API.

I'm using an third party API that provides an interface IRCApi that has a lot of methods like:

public interface IRCApi{
    void connect();
    void disconnect();          
    void disconnect(String aQuitMessage);
    void joinChannel(String aChannelName); 
    //...
}

My question: Is the code snippet below a valid implementation of that design pattern with all benefits cited by Joshua Bloch (multi thread support, facility to change, etc):

public enum IRCApiSingleton {
    INSTANCE;

    private final IRCApi ircApi = new IRCApiImpl(false);

    public final IRCApi get() {
        return ircApi;
    }
}

using this way: IRCApiSingleton.get().connect();

Or do I have to make my enum implement IRCApi and encapsulate all methods of an IRCApi implementation (there are about 40 methods), like:

public enum IRCApiSingleton implements IRCApi {
    INSTANCE {
        private IRCApi impl = new IRCApiImpl(true);    
        @Override
        public void connect() {
            impl.connect();

        }

        @Override
        public void disconnect() {
            impl.disconnect();
        }
        //...
    };
}

and use the INSTANCE directly?

1

Well, as I see it, IRCApiImpl has a public constructor making it difficult to call either way a "classical singleton".

Still, I'd say that if you follow the guideline of not creating another instance of IRCApiImpl at any other point or even better, making the class inaccessable, you can use either implementation.

From a strict Software-Engineering point of view, the first implementation would be preferable, because It would allow less coupling and would not need to be changed when the API of IRCApiImpl changed.

Personally though I'd prefer the second implementation, which saves you the .get() on every call.

2

I'd take a step back and ask yourself why you are trying to create a singleton in the first place. Don't get me wrong, I know there's a lot of people hating on singleton, and I think a lot of that hate is unwarranted. That said, it doesn't seem like your singleton is providing any functionality except acting as a global constant to IRCApiImpl.

If you had declared IRCApiImpl as an enum (or equivalently, taken all the code in IRCApiImpl and placed it inside of IRCApiSingleton), then I'd think you'd be applying Joshua Bloch's pattern correctly.

But given that not only have you not done that, but IRCApiImpl seems to have a constructor taking a parameter (implying that there could be multiple instances of it, each one taking on a different set of parameters when they got constructed), it sounds like what you're working with isn't a conceptually singleton at all.

If you're trying to use singleton because you want easy access to an object, I'd recommend you look at dependency injection instead.

  • Thanks for your reply. My thought about using singleton is because I created 3 other services that use this IRCApiImpl. I have to use the same instance of it in this services. As you say it would be better to inject it, but I don't use a DI framework (it is an Android App). I simply don't know where to instanciate it at first time. In Android when you need it usually you extend the Application class and put global scope stuff there. But I think it is worse solution. – alexpfx Jul 3 '15 at 13:57
  • You may be interested in blog.andresteingress.com/2014/08/31/… – Nebu Pookins Jul 3 '15 at 18:18
  • In this project I use butterknife. I already try to use Dagger, but I found to difficult to setup comparing with simplicity of my projects. I think it will be more pain to configure Dagger properly. – alexpfx Jul 3 '15 at 18:23

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.