Whiteboard Design Pattern is an OSGi service model, which influences the OSGi framework's service registry. Whiteboard pattern came into existence because of complicated and error prone nature of traditional model: The Listener Pattern (aka Observer Pattern).
OSGi service model is a collaboration model which supports service registry. The service registry allows applications or bundles to register services, which are simple Java interfaces to implement different functionalities. The dynamic nature of the service registry is useful to track the services that can show up or leave at any time. Whiteboard Pattern is created to support such OSGi service model, which was not supported by Listener Pattern.
Introduction
The Listener Pattern
The Listener Pattern is typically known as Observer Pattern. It is a Behavioral Pattern (aka Publish-Subscribe), which deals with dynamic changes in the state of different objects.
Listener Pattern follows a structure where an event listener is registered to event source. Now whenever an event source changes its state, all its event listeners get notified about the change through event object. In this pattern, everything is controlled by event source.
Implementation of Listener pattern is very complicated. Listener pattern supports many event listeners, so all listeners are registered to an event source. No event listener has access control over an event source, so for every service a new file is to be created. This creates an overhead of class file and affects the program running time and memory usage. Also, when an event listener is exiting, its registration is to be cancelled from the event source. Vice versa, when an event source is exiting, it makes sure that all its event listeners' references are removed. Here it is assumed that clean up is done automatically, but some embedded applications which are running continuously and are highly dynamic, this assumption cannot be verified and creates a significant issue.
Whiteboard Pattern
Whiteboard Pattern can be suggested by a role, where an event listener is looking for a special service. Another role could be when an event listener is looking for a looked service.
Whiteboard Pattern follows a structure where an event listener is registered to event source. This service registration is maintained by service registry, which is application programming interface for managing services. Bundle implements this interface and registers the service in service registry. Bundles are typically a group of Java classes, furnished with well-written manifest file, which help to bind with many other services. Now, an event listener can find the service in the service registry, or react to it when the service is executed. In this pattern, control is given to an event listener.
Implementation of Whiteboard Pattern is complex, because it has to handle all the services dynamically. To handle this dynamic nature Whiteboard Pattern is implemented using application bundles and server bundles. As the framework registry can be reused dynamically, the implementation becomes easier. Both application bundles and server bundles are responsible for managing the services and their inter-dependencies to the framework.
Advantages and Disadvantages: Over Listener Pattern
Advantages
Adding new bundles is easy. Contributed and configured bundles can be consumed by any number of subsystems in any way. Thus, registry supports configuration management. Testing for bundles and their connection with each other is easy. Implementation and debugging is easy because it reuse the framework registry. Implementing the same service using Listener Pattern is tedious work and cannot be reused. It is secured. As event listeners have access controls over event source and for every status change event listeners get notified, whereas there is no access to event listeners in Listener Pattern.
Disadvantages
It is the event source, and the event listeners, that determine the relative positioning of the contents, whereas in Listener Pattern every content comes from event source. The service registry's scope is undefined. So, it cannot support multiple event listeners requesting on one framework, which has different set of bundles.
Usage and Example
Typically OSGi model for Whiteboard Pattern handles inter-dependencies with many bundles. Some usages are stated using following examples: