约束和限制
- 假设我们聚焦在以下的工作流
-
只是输入Text进行对话
-
Users
- Add a User
- Remove a User
- Update a user
- Add to a user’s friends list
- Add friend request
- Approve friend request
- Reject friend request
- Remove from a user’s friends list
- Add friend request
-
Create a group chat
- 邀请朋友到一个 group chat
- 发送一个消息到一个 group chat
-
私人 1-1 chat
- 邀请一个朋友到一个 Private Chat
- 发送一个消息到一个 Private Chat
-
不需要担心初始化的扩展问题
-
编码实现
from abc import ABCMetaclass UserService(object):def __init__(self):self.users_by_id = {}def add_user(self, user_id, name, pass_hash): #...def remove_user(self, user_id): #...def add_friend_request(self, from_user_id, to_user_id): #...def approve_friend_request(self, from_user_id, to_user_id): #...def reject_friend_request(self, from_user_id, to_user_id): #...class User(object):def __init__(self, user_id, name, pass_hash):self.user_id = user_idself.name = nameself.pass_hash = pass_hashself.friends_by_id = {}self.friend_ids_to_private_chats = {}self.group_chats_by_id = {}self.received_friend_requests_by_friend_id = {}self.sent_friend_requests_by_friend_id = {}def message_user(self, friend_id, message): #...def message_group(self, group_id, message): #...def send_friend_request(self, friend_id): #...def receive_friend_request(self, friend_id): #...def approve_friend_request(self, friend_id): #...def reject_friend_request(self, friend_id): #...class Chat(metaclass=ABCMeta):def __init__(self, chat_id):self.chat_id = chat_idself.users = []self.message = []class PrivateChat(Chat):def __init__(self, first_user, second_user):super(PrivateChat, self).__init__()self.users.append(first_user)self.users.append(second_user)class GroupChat(Chat):def add_user(self, user): #...def remove_user(self, user): #...class Message(object):def __init__(self, message_id, message, timestamp):self.message_id = message_idself.message = messageself.timestamp = timestampclass AddRequest(object):def __init__(self, message_id, message, timestamp):self.message_id = message_idself.message = messageself.timestamp = timestampclass RequestStatus(Enum):UNREAD = 0READ = 1ACCEPTED = 2REJETED = 3