Friday, 13 May 2016

java - Name for sender/receiver of messages



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 "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?


Answer




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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...