Thursday, September 13, 2007

Publish/Subscribe



NotificationService {
publish(topic, payload);
subscribe(topic, listener);
}

Internally, each topic maintain a set of listener; Alternatively, topic is not used, uses more specific method such as addMouseClickListener(...) to specify it implicitly.

Callee subscribe/unsubscribe the listeners to interesting topic; When something happend (e.g, UI interaction, object added, removed or modified), event is fired, (The event carries all the context information such as source object, action, context etc that listener need to know). publish() is invoked to notify listener.

Listener {
ObjectChanged(event);
}

Publish/Subscribe also known as Observer pattern. The Observer pattern defines an one-to-many dependency between a subject object and any number of observer objects so that when the subject object changes state, all its observer objects are notified and updated automatically.

In the diagram, we can see it doesn't pass an event/data/msg object though, one static reference to subject object is kept in concreteObserver to get the latest state information.

No comments: