0

This might be a little long question but i try to keep it as small as possible and try to put it in the best way i can.

I have reading about design patterns and found observer pattern very intresting. i searched for its practical application and found various answers here. one of those answer was:

Whenever a question is posted, all the subscribers following the topics of similar interest are notified.

i tried to model this system in python as below:

using Mongoengine ORM to model a User and define a function notify for the User class which can be used to notify a user:

from mongoengine import *

connect('tumblelog')


# User model
class User(Document):
        email = StringField()
        first_name = StringField()
        last_name = StringField()
        topic_subscribed = StringField()

        def notify(self):
                print "email sent to user :{} {} {}".format(self.first_name, self.last_name, self.email)
                print "user was subsacribed to: {}".format(self.topic_subscribed)






# simulate publishing of an article of a particular topic
while True:
        x = raw_input(">>>")
        print "question of {} topic published".format(x)

        # How to notify all users that are subscribed to the topic ?????
        # naive way :
        # def some_aynchronous_function():
        #       1) find all users from db that are subscribed to the topic
        #       2) notify each user by looping through all of them          

I know the trivial way it can be done but can I do something better by using observer pattern here?

NOTE: I am not trying to fit the problem to the design pattern as it is usually frowned upon. I am just trying to implement it for *learning purpose. I have tried searching for some implementations but unsuccessful so far

MY QUESTION : I presented the way I know I could have approached the problem(pseudocode above) but is there anything that can be done better using an observer pattern here?

anekix
  • 2,393
  • 2
  • 30
  • 57

1 Answers1

0

The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.

source: Wikipedia - Observer Pattern

Your Observer is your User but you still need to define your Subject which in your case would probably be called Topic. This topic would keep a list of all its subscribers in an internal list. Then when an update occurs the topic would notify all it's subscribers by looping through each one and calling some method of theirs. For example:

class Topic:
    subscribers = []

    def addSubscriber(self, subscriber):
        subscribers.append(subscriber)

    def notifySubscribers(self):
        for subscriber : subscribers:
            subscriber.notify(self)
gimg1
  • 1,121
  • 10
  • 24
  • thanks for the reply. now i can atleast see a bigger picture of how things would stick together, however, i have a small doubt .For any middle to large size aplication or a popular web service we might have lot of users say.500,000 as an example. now would it make sense to keep all theses users internally in a list in memory at all the times?what happens when new users are added to the database? – anekix Oct 07 '17 at 07:13
  • You're right, keeping them in memory would most likely be implausible. In that case you could instead keep a reference of their id's and only load them into memory when the topic needs to notify subscribers. Some `persistence` frameworks can handle this for you by `Lazy Loading` the objects into memory when called. See https://en.wikipedia.org/wiki/Lazy_loading – gimg1 Oct 08 '17 at 17:57