A simple example of the observer mode in the Python design mode.

Source: Internet
Author: User

A simple example of the observer mode in the Python design mode.

This article describes the observer mode of the Python design mode. We will share this with you for your reference. The details are as follows:

The observer mode is a software design mode. A Topic object contains a series of observer dependent on it, which automatically notifies the observer of the change of the topic object and usually calls a method of each observer. This design mode is verySuitable for Distributed Event Processing Systems.

Typical observer mode:

1. The publisher class should include the following methods:

Register the objects that can receive notifications
Notifies you of any changes from the primary object to the registered object
Unregistered objects cannot receive any notification information

2. The Subscriber category should include the following:

The publisher will call the method provided by a subscriber to notify the subscriber of any changes.

3. When an event triggers a state change, the sender calls the notification method.

Summary:A subscriber can register or not register in a publish object. No matter what event occurs, the publisher is triggered to notify the subscriber by calling the notification method. This notification will only notify registered buyers when an event occurs.

A simple python implementation:

Let's implement an example of how different users publish technical emails on TechForum. When any user publishes a new email, other users will receive a new email notification. From the perspective of the object, we should have a TechForum object. We need another user object to be registered on TechForum. When a new email notification is sent, we should send the mail title.

A simple example of the analysis will be associated with the relationship between the intermediary and the employer. This is an extension of the relationship between recruiters and candidates. Different types of work information will be published through a work agency. Candidates will search for relevant work information, and recruiters will also look for candidates registered in the agency.

The Code is as follows:

class Publisher:  def __init__(self):    pass  def register(self):    pass  def unregister(self):    pass  def notifyAll(self):    passclass TechForum(Publisher):  def __init__(self):    self._listOfUsers = []    self.postname = None  def register(self, userObj):    if userObj not in self._listOfUsers:      self._listOfUsers.append(userObj)  def unregister(self, userObj):    self._listOfUsers.remove(userObj)  def notifyAll(self):    for objects in self._listOfUsers:      objects.notify(self.postname)  def writeNewPost(self , postname):    self.postname = postname    self.notifyAll()class Subscriber:  def __init__(self):    pass  def notify(self):    passclass User1(Subscriber):  def notify(self, postname):    print "User1 notified of a new post %s" % postnameclass User2(Subscriber):  def notify(self, postname):    print "User2 notified of a new post %s" % postnameclass SisterSites(Subscriber):  def __init__(self):    self._sisterWebsites = ["Site1" , "Site2", "Site3"]  def notify(self, postname):    for site in self._sisterWebsites:        print "Send nofication to site:%s " % siteif __name__ == "__main__":  techForum = TechForum()  user1 = User1()  user2 = User2()  sites = SisterSites()  techForum.register(user1)  techForum.register(user2)  techForum.register(sites)  techForum.writeNewPost("Observe Pattern in Python")  techForum.unregister(user2)  techForum.writeNewPost("MVC Pattern in Python")

Running result:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.