For the interview of an oo design question: design message system, I am having trouble understanding what are some uses of public
and private
members/method for each class.
Long story short. Say we define the user class as the follows.
class user {
public:
string account_name;
string info;
vector<User> friend_list;
vector<Chat> chat_list;
void friend_request(User friend_target);
private:
string system_user_id;
}
I am wondering, should there be any private
member in the first place?
Here, I defined system_user_id
to be private because it shouldn't be exposed to the real user of the system. What do you guys think?
Another thing that I find helpful is considering encapsulation as it applies to clients of the user
class, not just the external user.
A client could be an internal user. Imagine that in a couple months your user is the most popular thing on the internet and you have a team of developers working on your system.
They see a user class with a public account_name
. They would like to change the name of the account so they update it directly. But what if a valid update requires synchronization with a datastore or something? The user class design has allowed the client (internal) the ability to create incorrect code!!
The same could go for friend_list, chat_list, if you're user is being used in a concurrent environment, you might need some sort of locking, if you expose the lists directly it allows your internal clients the option of creating race conditions, while if they were private and encapsulated you could better protect your internal clients.