Solution:
Apparently, I am too naive and I think too few. Here‘s the detailed solution.
What is our chat server?
This is something you should discuss with your interviewer, but let’s make a couple of as- sumptions: imagine we’re designing a basic chat server that needs to support a small number of users. People have a contact list, they see who is online vs offline, and they can send text-based messages to them. We will not worry about supporting group chat, voice chat, etc. We will also assume that contact lists are mutual: I can only talk to you if you can talk to me. Let’s keep it simple.
What specific actions does it need to support?
What can we learn about these requirements?
We must have a concept of users, add request status, online status, and messages.
What are the core components?
We’ll need a database to store items and an "always online" application as the server. We might recommend using XML for the communication between the chat server and the clients, as it’s easy for a person and a machine to read.
What are the key objects and methods?
We have listed the key objects and methods below. Note that we have hidden many of the details, such as how to actually push the data out to a client.
enum StatusType { online, offline, away; } class Status { StatusType status_type; String status_message; } class User { String username; String display_name; User[] contact_list; AddRequest[] requests; boolean updateStatus(StatusType stype, String message) { }; boolean addUserWithUsername(String name); boolean approveRequest(String username); boolean denyRequest(String username); boolean removeContact(String username); boolean sendMessage(String username, String message); } // Holds data that from_user would like to add to_user class AddRequest { User from_user; User to_user; } class Server { User getUserByUsername(String username); }
What problems would be the hardest to solve (or the most interesting)?
amazon 设计 9 chat server,布布扣,bubuko.com
原文:http://www.cnblogs.com/leetcode/p/3906253.html