We are developing a framework that has several layers and would be deployed in a multi-threaded environment. Each layer may have its own input/output data type. The top layer takes the input, performs some actions on the data and passes it to the next layer. Based on the output from next layer, the top layer would perform another set of operations on the data. Similar interactions happen between other layers.
There are two use cases that we did not incorporate in the original design:
Generating metrics over what operations each layer performs on the data.
The change in data in one layer affects the operations in another layer, the changes should be recorded and each layer should have capability to fetch the list of changes.
After some thinking we came up with two approaches to accommodate these requirements in the existing design:
Use observer pattern and let each layer report its changes. These changes can be then pulled by the layer that needs to use it.
Pros:
- Since we have already come up with a design for the framework and most of work is complete. This approach would not require significant changes to the existing design, if any.
Cons:
- It becomes difficult to manage(record and report back) changes corresponding to each layer.
- There is a central class that aggregates all the data for each request. This class acts like a global variable. It has to be initialized and deleted by the top layer. Adding another top layer could be error prone.
Extend the current data classes using an interface equivalent to EventContainer. This way all the layers would record their changes and spit them in their output.
Pros:
- This approach is much more cleaner and extendable than observer pattern.
Cons:
- It would require significant design changes.
- The concept of data having events as well merges two separate concerns into one and does not seem like a good idea.
We would like to know if there is a design pattern or any other solution that solves this problem?
Which of the above two solutions should be given higher preference considering that we would want a flexible, extendable and cleaner solution?