![]() In most cases, it consists of a single update method. The Subscriber interface declares the notification interface. When a new event happens, the publisher goes over the subscription list and calls the notification method declared in the subscriber interface on each subscriber object. Publishers contain a subscription infrastructure that lets new subscribers join and current subscribers leave the list. These events occur when the publisher changes its state or executes some behaviors. The Publisher issues events of interest to other objects. The interface would allow subscribers to observe publishers’ states without coupling to their concrete classes. This interface would only need to describe a few subscription methods. If your app has several different types of publishers and you want to make your subscribers compatible with all of them, you can go even further and make all publishers follow the same interface. ![]() ![]() Publisher notifies subscribers by calling the specific notification method on their objects. This interface should declare the notification method along with a set of parameters that the publisher can use to pass some contextual data along with the notification. That’s why it’s crucial that all subscribers implement the same interface and that the publisher communicates with them only via that interface. Besides, you might not even know about some of them beforehand if your publisher class is supposed to be used by other people. You wouldn’t want to couple the publisher to all of those classes. Real apps might have dozens of different subscriber classes that are interested in tracking events of the same publisher class. Now, whenever an important event happens to the publisher, it goes over its subscribers and calls the specific notification method on their objects. In reality, this mechanism consists of 1) an array field for storing a list of references to subscriber objects and 2) several public methods which allow adding subscribers to and removing them from that list.Ī subscription mechanism lets individual objects subscribe to event notifications. Fear not! Everything isn’t as complicated as it sounds. The Observer pattern suggests that you add a subscription mechanism to the publisher class so individual objects can subscribe to or unsubscribe from a stream of events coming from that publisher. All other objects that want to track changes to the publisher’s state are called subscribers. The object that has some interesting state is often called subject, but since it’s also going to notify other objects about the changes to its state, we’ll call it publisher.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |