简体   繁体   中英

Design the interaction between classes - regarding the Solution 8.7 of Cracking the Coding Interview

I have learning the book Cracking the Coding Interview, and having some questions about the Chapter 8: Object-Oriented Design. Take the question 8.7 as an example:

8.7 Explain how you would design a chat server. In particular, provide details about the various backend components, classes, and methods. What would be the hardest problems to solve?

The solution can be found this github repo .

My general question is: how to assign different methods to different classes in order to complete a functionality? Is there some principles or common ideas of making these interactions? Take one specific functionality - User A adds User B - as an example:

The following is partial code of UserManager :

public class UserManager {
    ...
    public void addUser(User fromUser, String toAccountName) {
        User toUser = usersByAccountName.get(toAccountName);
        AddRequest req = new AddRequest(fromUser, toUser, new Date());
        toUser.receivedAddRequest(req);
        fromUser.sentAddRequest(req);
    }

    public void approveAddRequest(AddRequest req) {
        req.status = RequestStatus.Accepted;
        User from = req.getFromUser();
        User to = req.getToUser();
        from.addContact(to);
        to.addContact(from);
    }

    public void rejectAddRequest(AddRequest req) {
        req.status = RequestStatus.Rejected;
        User from = req.getFromUser();
        User to = req.getToUser();
        from.removeAddRequest(req);
        to.removeAddRequest(req);       
    }
    ...
}

The following is partial code of User :

public class User {
    ... 
    public boolean addContact(User user) {
        if (contacts.containsKey(user.getId())) {
            return false;
        } else {
            contacts.put(user.getId(), user);
            return true;
        }
    }

    public void receivedAddRequest(AddRequest req) {
        int senderId = req.getFromUser().getId();
        if (!receivedAddRequests.containsKey(senderId)) {
            receivedAddRequests.put(senderId, req);
        }       
    }

    public void sentAddRequest(AddRequest req) {
        int receiverId = req.getFromUser().getId();
        if (!sentAddRequests.containsKey(receiverId)) {
            sentAddRequests.put(receiverId, req);
        }       
    }

    public void removeAddRequest(AddRequest req) {
        if (req.getToUser() == this) {
            receivedAddRequests.remove(req);
        } else if (req.getFromUser() == this) {
            sentAddRequests.remove(req);
        }
    }

    public void requestAddUser(String accountName) {
        UserManager.getInstance().addUser(this, accountName);
    }
    ...
}

I am just wondering where approveAddRequest(AddRequest req) and rejectAddRequest(AddRequest req) are called. After AddRequest is sent or received, no further behaviors will be done for really process this request.

And also, I am wondering: should every interaction between class be like this (ie, lots of methods from different classes are calling and called many times)?

What you're looking for is "Observer" Pattern.

It's best-practice introduced by GoF for event handling.

What you've presented here has nothing to do with chat. It's just utility for dealing with user interaction.

As for you last question: you're not seeing full code, so you can't know where it's called from. You should find full source code first. And you're right, with presented code no behaviour would be done, but for real chat there should be. This whole things is a mock.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM