You can find a lot of descriptions of the common patterns like MVC, strategy, factory, adapter etc. on the internet. The blackboard pattern is one of the not so common patterns. This is why I want to take a closer look on it. It can be categorized as a behavioral pattern. The blackboard pattern describes a process of solving a set of problems by applying a set of known solutions.
The pattern has three components playing together:
- the blackboard – like in school (well, at least when I went to school), where you put all information you have on a blackboard (the problem to solve, the variables you know, the limitations given and the algorithms you’re allowed to use etc.).
- the so called “knowledge sources” – this is a set algorithms or modules, that can solve a specific problem or transform it into another set of equivalent (and possibly smaller) problems.
- a controller, which knows how to execute the knowledge sources.
The pattern is very simple: the blackboard keeps track of the current state of the problem you want to solve. Depending on the status it decides which “knowledge source” to execute. The controller read all information needed from the blackboard, configures the knowledge source and executes the knowledge source. The result of the execution is written back onto the blackboard. Then the process gets repeated until the initial problem is solved.
The blackboard pattern is used mainly in dynamic environments, when the execution is depending on the input/output of the processed data. For example, many AI and machine learning problems benefit from the blackboard pattern.
An example implementation of the blackboard pattern can be found on my Github repo Patterns, where this pattern is used to implement a MergeSort algorithm. Here the “knowledge sources” are: “Split an array”, “Merge two arrays” and “Sort two elements”. The controller checks, if it any of the “knowledge sources” knows how to sort the input array (which is not the case for arrays with more than 2 elements). As no “source” knows how to do this, the controller asks, if a source knows, how to split the array. The source “Split an array” feels responsible and produces more smaller arrays to sort. This is repeated until all arrays are so small, that the source “Sort two elements” can handle these arrays. Finally, “Merge two arrays” builds up the sorted result.
See also: Blackboard-Pattern – Wikipedia