New answers tagged

1

I really think you are missing a ChatMessage type here, which would have let's say a Sender (that would be a user), a Channel (abstraction for chatroom, unless you want to use that term even for 1v1 conversations), and Content. Then you also need some object to orchestrate everything, e.g. when you create a new chatroom or a new user registers, who is the ...


20

In your case, you have users vs. Chatrooms. If you have methods that concern both, you could put them into either class, not much difference. However: You will have not only users vs. Chatrooms, you will have Users vs. bills, users vs. Support requests, users vs. 100 other things. If you do things consistently by putting methods into the user class, it will ...


40

A user is someone who is registered and able to use the system. A chat room is a place people can chat. What happens when a user joins a chat room? What is that thing that represents a user who has joined a chat room? That is the abstraction you are missing. Other answers are hinting at this. You could say a user participates in a chat. You need a class that ...


0

When struggling to find where code goes, consider whether there might be missing abstraction(s), which would be some other entity or object or actor. Consider the consuming client code, whose usage model should be as simple as possible — dealing with few objects, rather than unnecessarily dealing with object pairs, another sign of missing abstraction (and ...


8

By the looks of it you are missing a controller, an object that is calling the shots, that moves data from one object to another. User.JoinChatRoom() The user does not have to be an active thing that makes its own decisions. You have a UI. Someone double-clicks a room and you want the user to enter the room. You could then have the code behind the UI add ...


0

Sometimes you might want to consider conflicting forces: for example, in a chat program, the UI might want to talk to the User object only, because it shouldn't be concerned with the internal implementation, while the User object might delegate the function to the current ChatRoom object, which would be the right object to do the actual work. This is ...


2

It’s about flexibility. If I tell you that methods should be located with the data they act on you’d be right to complain because the whole point of your question is what to do when the needed data isn’t all in one place. So what should you do when the data doesn’t make it obvious? Create objects who’s methods will be stable. OOP is good at a lot of things ...


24

Rule of thumb: Your methods are probably in the right place if they don't need to pull data out of other objects. This is often called "envy", as in "feature envy". If your method wants data that does not belong to it, it is basically envious of the features of some other object. Don't envy, cooperate! Thinking about who do you want to ...


Top 50 recent answers are included