Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Is using a singleton to represent a Publish-subscribe message system a bad design choice?


Background

Im thinking about writing a pubsub implementation (using the singleton pattern) to allow for easy decoupling in my application that's using an MVCish architecture.

I have used the wxPython gui toolkit along with pyPubSub (which uses the singleton pattern to implement its Publisher -see here-) in the past and have found it a simple and effective way to decouple the model from the view. Im looking for a simple way to achieve the same kind of functionality in a java swing based application without the need for a large third party framework which I don't feel is necessary or merited.

I am aware of Java's observer interface and observable class but these are a little basic for my needs. I need to have the ability to send messages and data with different topics -different topics will be handled differently and therefore should have different handlers/callbacks.

I have read numerous threads and blog posts on the "evilness" of singletons, and have concluded that ideally we should not use them but practically some times we have to.

For this application I don't see it as a problem.. I don't see any issues with dependencies.

Is there cleaner way to design this ?


Pseudo Implementation

If I were to build a simple implementation based on pyPubSub, broadcasting a message from my model would look something like this..

//broadcast a messge.. i dont care if anyone is listening..
Publisher.getInstance().sendMessage("topicName", messageObject)

We then write an inner class in the controller to handle messages for which ever topicname its bound to using the Publishers subscribe method.

//inner class to handle message recieved for "topicname"
private Class myPubsubListener implements PubsubListener {
    public messageHandler(Message message) {
        //dostuff..
    }

}

Then we bind an instance of the myPubsubListener class to the "topicname" in the controller

//setting up a listener for a particular "topicname"
//bind the myPubsubListener to the "topicname" so that it will handle all
//all message sent with this topic name
Publisher.getInstance().subscribe(new myPubsubListener(), "topicname");

Notes:

  • Planning to use weak references for the PubsubListeners so as not distrupt garbage collection...

  • Also there will be no dependancy on topic names.. if topic name has
    not been subscribed to, simply no message will be sent

  • A topicname does not have to exist for it to be subscribed to

  • The number of listeners that can be subscribed to a topicname is
    unlimited

share|improve this question
    
Is there a reason why you can't use Java's built-in listeners? –  Eva Jan 16 '13 at 21:50

1 Answer 1

up vote 5 down vote accepted

I'd create a Publisher interface and move the implementation to a PublisherImpl implementation class. (You may can find a better name for the class.) Then, create only one Publisher instance in the production code and pass it to the objects which need it. You can use constructor parameters, dependency injection, setter methods etc. for this.

It would avoid the global variable (singleton), tight coupling and its drawbacks.

share|improve this answer
    
+1 Would I be correct in saying that idea for the Publisher interface is allow for dependency injection in the objects to which its passed? Either way I like this design and Ill probably go with this, but Ill wait a while longer before accepting your answer to see if there are any others.. –  volting Sep 23 '12 at 17:55
    
@volting: Um, I don't know that interfaces are strictly required by DI frameworks or not (I suppose it mainly depends on the framework) but it does not hurt and helps testing too. Although some mocking frameworks could mock classes, not just interfaces, I guess using interfaces is a cleaner solution for testing (since you don't need bytecode manipulations, if I'm right). Thanks for the upvote! –  palacsint Sep 23 '12 at 18:16
    
I wasn't referring to using a framework - from what I understand making parameters to the constructor interfaces is an inversion of control and allows for DI (instead of instantiating the objects that would passed in the constructor..). I just wan't to see what your rationale was - which is more or less what I thought even if you don't label it that way. Thanks again –  volting Sep 23 '12 at 18:47
1  
Good answer. It's very favorable to simply use dependency injection, through the methods you named, over a Singleton use. The key is to inject interfaces and not concrete classes. –  Neeko Sep 26 '12 at 13:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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