3

I have a class which is responsible for sending and receiving messages. Messages are sent by using myInstance.send(message, channel) and messages are received by registering a MessageListener with a channel to listen on.

I would usually just call this something fairly generic such as MessageManager but I have recently read Naming Classes - How to avoid calling everything a "<WhatEver>Manager"? which made me try and find another name. The closest I could come to a good name was MessageDispatcher which doesn't really convey the fact that also receives messages. Are there any generally used names for a class of this nature?

Community
  • 1
  • 1
user2248702
  • 2,741
  • 7
  • 41
  • 69
  • It would help if you could name some other responsibility the instances of your class will have. Will they filter, validate, certify, translate, register, deflect, spy, wrap the messages they receive? There must be some central processing capability assigned to these objects that makes them useful in your system. All messages are meant to be sent and received, but maybe the purpose of your class adds something else to that. – Leandro Caniglia Dec 26 '14 at 02:39

1 Answers1

5

I tend to like the names Publisher and Subscriber when dealing with messages (see more info on publish/subscribe pattern here).

If your message handling is similar to that a neat and tidy naming strategy could be the to separate the whole thing into these interfaces.

// Publisher of messages
public interface MessagePublisher {
    void send(Message m, Channel c);
}

// Subscriber of messages
public interface MessageSubscriber {
    void messageReceived(Message r);
}

// Handles registration
public interface MessageSubscriberAware {
    void registerMessageSubscriber(MessageSubscriber s, Channel c);
}

// The "glue" - the concrete implementation
public class MessageDispatcher implements MessagePublisher, MessageSubscriberAware {
    // Impl
}

The concrete implementation of this that could be named MessageDispatcher, it is aware of the subscribers and can therefore distribute the published messages.

wassgren
  • 18,651
  • 6
  • 63
  • 77
  • As a complement to wassgren's answer, the Publish-Subscribe pattern reference is relevant in a messaging context. If your system uses publishing and dispatching for a more general purpose, then you should rather make a reference to the [observer pattern](http://en.wikipedia.org/wiki/Observer_pattern). – bdulac Dec 25 '14 at 10:07
  • FYI, a [transceiver](http://www.webopedia.com/TERM/T/transceiver.html) is an entity which both transmits and receives messages. However I completely agree with @wassgren, you'll be better off separating the concerns and have separate Publisher and Subscriber classes. – Ben Smith Dec 30 '14 at 22:34