Search code examples
javadependencieslayerdecoupling

Reverse dependency between layers problem


I'm building a control system for a hardware installation (water circulation system). I've designed it in two layers: the hardware description layer and the control layer.

+----------------------------------------------+
|    Control (enables manipulation of devices) |
+----------------------------------------------+
        | uses (decorates)
        v
+---------------------------------------+  (de)serialize  +------------+
|            HW Description             |<--------------->| Files / DB |
| (stores physical layout, cabling etc) |                 +------------+
+---------------------------------------+

The hardware description layer contains a map of the hardware, describing how pipes are connected to heat exchangers and other equipment. The data for this layer is configurable per installation, and will be read in at runtime. Therefore all the classes in the hardware description layer should be serializable in one way or another (via Hibernate / serialization to XML or so).

The control layer will decorate the hardware description layer classes with intelligence, so the heat exchanger will for instance get a HeatExchangerController associated with it.

In this system, the entities in the control layer might need to seek out their neirest neighbours via the hardware description, so the heat exchange controller might do something like

HeatExchangerController neighbour = this.getHeatExchanger().getInputPipe().getOtherSide().getHeatExchanger().getController();
neighbour.notify(highLoadAlert);

The problem is that this makes the lower layer (the hardware layer) aware of the intelligence above it (its controller(s)). Not only is this breaking the one-way dependency rule between the layers, but since all hardware description classes should be serializable this complicates the code, since all references to the higher layers need to be optional, declared transient etc.

Now I'm a bit stuck trying to decide if it is a good or bad idea to make the control available through the HW description layer like this, or if there are any alternative approaches to take. I can only think of one method which would involve mirroring and delegating almost all of the HW description layer, which seems way to intrusive to be good.

So my question is if any of you recognize this situation and have any tips / experiences. Does it have a name? Are there are any good patterns /best practices for it, and are there are any other well-known libraries / frameworks which also have had and worked around this situation?

Thank you.


Solution

  • You haven't shown us what benefit you get from seperating the controller logic from the hardware description. Are you sure this separation is worth the effort?

    It sounds like you are building an Anemic Domain Model?