![]() ![]() 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 |