Design flaw in own IoT-Device message-handling architecture

xvzwx :

I am about to build a test environment for IoT devices that have different behaviors and communicate via various protocols. To achieve this and to stay open for extensions I decoupled the devices and their protocols from each other. The following diagram outlines the current structure of the application:

UML diagram of current structure

I think this kind of implementation is structurally correct, because a device has a configuration and a (replaceable) MessageHandler. Which means, the IOTDevice doesn't care how the message is send over the network.

The Problem: When I receive a message from the outside (i.e. a MessageHandler receives a message, the handle method is called), I want to be able to execute a pre-defined behaviour that is defined in one of the IOTDev_X classes (i.e. call a method doX(), doY() or doZ()). But the MessageHandler doesn't know anything about the IOTDevice. This way it's not possible to call a specific method when it receives an incoming message.

One solution I thought about would be that a MessageHandler contains the IOTDevice. But from my point of view, that feels like a bad design.

What would be a good method to overcome this problem (at best without discarding the complete design).

user43968 :

What about the observer pattern ?

enter image description here

Your IOTDevice can be turn as a MessageObserver using an interface and your MessageHandler can be turn as a MessageObservable via an interface.

Now Register your IOTDevice in the MessageHandler to listen for event. Register just fill a collections of MessageEventObserver Your MessageHandler can now notify with an event messageReceived your IOTDevice by just looping threw the registered MessageEventObserver and by Calling MessageIncoming

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=157398&siteId=1