Skip to content

Commit 624e0c2

Browse files
committed
Simplify online chat solution
1 parent 992cd5b commit 624e0c2

File tree

2 files changed

+3
-25
lines changed

2 files changed

+3
-25
lines changed

solutions/object_oriented_design/online_chat/online_chat.ipynb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@
6969
"\n",
7070
"class UserService(object):\n",
7171
"\n",
72-
" __metaclass__ = Singleton\n",
73-
"\n",
7472
" def __init__(self):\n",
7573
" self.users_by_id = {} # key: user id, value: User\n",
7674
"\n",
@@ -145,16 +143,7 @@
145143
" UNREAD = 0\n",
146144
" READ = 1\n",
147145
" ACCEPTED = 2\n",
148-
" REJECTED = 3\n",
149-
"\n",
150-
"\n",
151-
"class Singleton(type):\n",
152-
"\n",
153-
" _instances = {}\n",
154-
" def __call__(cls, *args, **kwargs):\n",
155-
" if cls not in cls._instances:\n",
156-
" cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)\n",
157-
" return cls._instances[cls]"
146+
" REJECTED = 3"
158147
]
159148
}
160149
],

solutions/object_oriented_design/online_chat/online_chat.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
class UserService(object):
55

6-
__metaclass__ = Singleton
7-
86
def __init__(self):
97
self.users_by_id = {} # key: user id, value: User
108

@@ -38,8 +36,8 @@ def reject_friend_request(self, friend_id): # ...
3836
class Chat(metaclass=ABCMeta):
3937

4038
def __init__(self, chat_id):
41-
self.users = []
4239
self.chat_id = chat_id
40+
self.users = []
4341
self.messages = []
4442

4543

@@ -79,13 +77,4 @@ class RequestStatus(Enum):
7977
UNREAD = 0
8078
READ = 1
8179
ACCEPTED = 2
82-
REJECTED = 3
83-
84-
85-
class Singleton(type):
86-
87-
_instances = {}
88-
def __call__(cls, *args, **kwargs):
89-
if cls not in cls._instances:
90-
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
91-
return cls._instances[cls]
80+
REJECTED = 3

0 commit comments

Comments
 (0)