When modeling a chat, should a chat object receive messages or a message be sent to a chat object? I'm not sure whether there is a definitive answer but maybe someone can point out benefits of one over the other.




  • 1
    did you consider chat.sent(message) ? – EL Yusubov Sep 6 '13 at 13:21
  • well yeah, that's basically variant #2 but I was being somewhat more verbose… – pkluz Sep 6 '13 at 13:23
  • keep it clean and simple, that is the best approach and called YAGNI and KISS principles of lean development. – EL Yusubov Sep 6 '13 at 13:24
  • Message shouldn't have to know about where it is sent. – Marjan Venema Sep 6 '13 at 14:32
  • @MarjanVenema it depends, a message could contain the sender and receiver then the system could parse it accordingly, really there isn't enough information about the implementation to properly answer the question... – MetaGuru Sep 6 '13 at 17:34

For the model, it doesn't really matter. However, the implementation is almost always easier one way compared to the other. If you can't tell, try both ways and pick the one that's cleaner. I suspect in this case the cleaner implementation would be chat.sendMessage, because you're mostly using the chat resources to perform the action, of which the message is just one more dependency, but there's really no way to know without seeing the rest of the code.

  • +1 Most other suggestions would require the client or message objects containing connection/chat state information. – Steven Evers Sep 6 '13 at 18:08

I like the 2nd option best.

I'd do something like this:

public interface ChatRoom {
    public void putMessage(ChatMessage m);
    public void putMessageToParticularUser(ChatMessage m, int userID);
    public void addUser(ChatUser cu);

What is triggering the message to be sent? If it's a specific client then why not:



client.send(message, otherClient) for whisper

where client would be whatever object is created when a request is received, could be a message context or could be a user object that would identify the user, etc

if you base the objects around the events it should become clearer of what things you want to be 'controller' objects and what things you want to be data objects

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.