From 624e0c2ffe1eab6cb525bf6be1166e0cfcbc004f Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Tue, 7 Mar 2017 21:37:11 -0800 Subject: [PATCH] Simplify online chat solution --- .../online_chat/online_chat.ipynb | 13 +------------ .../online_chat/online_chat.py | 15 ++------------- 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/solutions/object_oriented_design/online_chat/online_chat.ipynb b/solutions/object_oriented_design/online_chat/online_chat.ipynb index bcc10f2..3e42f88 100644 --- a/solutions/object_oriented_design/online_chat/online_chat.ipynb +++ b/solutions/object_oriented_design/online_chat/online_chat.ipynb @@ -69,8 +69,6 @@ "\n", "class UserService(object):\n", "\n", - " __metaclass__ = Singleton\n", - "\n", " def __init__(self):\n", " self.users_by_id = {} # key: user id, value: User\n", "\n", @@ -145,16 +143,7 @@ " UNREAD = 0\n", " READ = 1\n", " ACCEPTED = 2\n", - " REJECTED = 3\n", - "\n", - "\n", - "class Singleton(type):\n", - "\n", - " _instances = {}\n", - " def __call__(cls, *args, **kwargs):\n", - " if cls not in cls._instances:\n", - " cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)\n", - " return cls._instances[cls]" + " REJECTED = 3" ] } ], diff --git a/solutions/object_oriented_design/online_chat/online_chat.py b/solutions/object_oriented_design/online_chat/online_chat.py index 266af29..d742629 100644 --- a/solutions/object_oriented_design/online_chat/online_chat.py +++ b/solutions/object_oriented_design/online_chat/online_chat.py @@ -3,8 +3,6 @@ from abc import ABCMeta class UserService(object): - __metaclass__ = Singleton - def __init__(self): self.users_by_id = {} # key: user id, value: User @@ -38,8 +36,8 @@ class User(object): class Chat(metaclass=ABCMeta): def __init__(self, chat_id): - self.users = [] self.chat_id = chat_id + self.users = [] self.messages = [] @@ -79,13 +77,4 @@ class RequestStatus(Enum): UNREAD = 0 READ = 1 ACCEPTED = 2 - REJECTED = 3 - - -class Singleton(type): - - _instances = {} - def __call__(cls, *args, **kwargs): - if cls not in cls._instances: - cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) - return cls._instances[cls] \ No newline at end of file + REJECTED = 3 \ No newline at end of file