publish-subscribe pattern class publisher(object)def __init__(self)self subscribers set(def subscribe(selfsubscriber)self subscribers add(subscriberdef unsubscribe(selfsubscriber)self subscribers discard(subscriberdef unsubscribe_all(self)self subscribers set(def publish(selfmessage)for subscriber in self subscriberssubscriber process(messagenextwe remove the final link between the publisher and the subscriber by adding single dispatcher class through which all messages will pass the aim is to have single location to which publishers send messages the same location keeps an index of all the subscribers the result is that the number of publishers and subscribers can vary without having any impact on the rest of the system this gives us very clean and decoupled architecture to work with class message(object)def __init__(self)self payload none class subscriber(object)def __init__(selfdispatcher)dispatcher subscribe(selfdef process(selfmessage)print("message{}format(message payload)class publisher(object)def __init__(selfdispatcher)self dispatcher dispatcher