# Whiteboard Pattern

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 that supports service registry. The service registry allows applications or bundles to register services, which are simple Java interfaces implementing different functionalities. The dynamic nature of the service registry is useful for tracking services that can appear 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:
I) A TV Show is to be projected on LCD. Suppose a service named ‘Display’ service will project the contents that cycles over many bundles, which contains different contents. Suppose this content is provided by some ‘Providers’. An interface can be implemented by any bundles whose contents want to be displayed, where ‘Providers’ are catechized for getting the content. A ‘DisplayInstructor’ is responsible to cycle through all the screens in the service registry and display the final content at that particular point of time, which is managed by ‘TimeManager’ which implements ‘Providers’.
This shows that the dependency between ‘DisplayInstructor’ and ‘TimeManager’ is handled. And both of them can work simultaneously or in any order at any time.
II) A group of friends uses a web based UI portal to store an article. Friends are working on article's sections. A section contains the contents which is added by some members of the group. Using whiteboard pattern, the dependencies for adding the content and displaying it on the web portal is handled. And both of these tasks can be done simultaneously or in any order at any moment.
An Example for Whiteboard Pattern Handler

## Conclusion

The Whiteboard pattern implementation is smaller and simpler, and thus less prone to programming errors. As per the code size, it is significant that listener pattern can have more deadlock possibilities than the whiteboard pattern.
The OSGi environment doesn't entirely follow basic programming rules with Java, rather it shows some features of its own over Java. The Listener Pattern puts such programming rules over OSGi environment, which can be considered first drawback. Apart from that, Listener Pattern doesn't suit for dynamic changes, which are expected in the OSGi environment.
The Listener pattern, a programmer of the event listener is full in-charge of the program. He decides event sources that are registered to the listener, and all the dependencies between them.
A Bundle is not an application running autonomously from other applications. It is a component running to bind with the other bundles in the environment. Bundles have to be configured and managed properly. Bundles should be written in such format that they do not show direct control, rather provide a service that can be connected and configured with other bundles. The Whiteboard pattern supports such functionality of bundles and show significant improvement over listener pattern.
These reasons are intimidating factors to apply whiteboard pattern, where managing the inter-bundle dependencies is required.