A design characteristic of Parham is efficiency. In this example it is shown that Parham composition model is very efficient.
Combining Queue and Tree components
Assume that we need a queue with an extra feature: we need to be able to remove an element from the queue by providing its key.
It means that we need the following operations.
All of the above methods except removedBykey are already provided by the Queue component.
To provide removeByKey a simple solution is to iterate over the elements of the queue and remove the element that matches the key. The cost of this approach is O(n) which is not acceptable.
A better solution is to augment Queue by a binary tree (the Tree component) so that an element that matches a key can be found in O(log n).
The combination of them forms a new component named QT. The cost of operations is QT are as follows.
The interesting point is that doing the same thing in Java is not possible.
In other words, augmenting a queue (LinkedList class of Java) with a binary tree does not yield any improvement.
The example is implemented in four files.
The implementation of QT component is provided in the front panel.
After the declaration of QT, there are two instances declarations: Queue and Tree.
After them, the implementation of operations are provided. They are straightforward.
The complete code of the sample can be downloaded from the link provided at the beginning of the discussion.
Queue[Root queue, Node];
Tree[Root tree, Node];
public void Root.enqueue (Node n)
public Node Root.deque ()
ret = queue.deque ();
public void Root.remove (Node n)
public Node Root.removeByKey (Node n)
ret = tree.removeByKey (n);