Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

message.send(chat);

VS.

chat.sendMessage(message);

share|improve this question
1  
did you consider chat.sent(message) ? –  Yusubov Sep 6 '13 at 13:21
    
well yeah, that's basically variant #2 but I was being somewhat more verbose… –  wasabii 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. –  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... –  ioSamurai Sep 6 '13 at 17:34

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
    
+1 Most other suggestions would require the client or message objects containing connection/chat state information. –  Steve 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);
    ...
    ...
}
share|improve this answer

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

client.send(message)

or

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

share|improve this answer

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.