Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Observer pattern solution in Clear category for Party Invitations by martin.pilka
class Friend:
def __init__(self, name):
self._name = name
self._invitation = 'No party...'
def invite(self, invitation):
self._invitation = invitation
def show_invite(self):
"""
Class Friend should have the show_invite() method which returns the string with the last invite that the
person has received with the right place, day and time. The right place - is the 'place' which is given
to the Party instance in the moment of creation. If the person didn't get any invites, this method should
return - "No party..."
In this mission you could use the Observer design pattern.
"""
return self._invitation
class Party:
def __init__(self, place):
self._place = place
self._observers = []
def add_friend(self, observer: Friend):
"""
Add friend 'name' to the list of the 'observers' (people, which will get the invitations, when the new party is
scheduled)
"""
self._observers.append(observer)
def del_friend(self, observer: Friend):
"""
Remove 'friend' from the 'observers' list
"""
self._observers.remove(observer)
def send_invites(self, time):
"""
Send the invites with the right day and time to the each person on the list of 'observers'
"""
for observer in self._observers:
observer.invite(f'{self._place}: {time}')
Feb. 6, 2019