let's say that I am working with an IoT device. The device can have many sensors, each of them has an address, i/o direction and a reading method.
temperature=d2,input,digital
humidity=d3,input,digital
lcd=a1,output,analog
From my program, I should read this configuration file and start getting data for example from the temperature sensor, which I know is at address d2 and provides digital input. How can I abstract this? I mean, I can't have something like
if(direction == input) {
if(type == digital) {
readDigital(d2)
} else {
readAnalog(d2)
}
} else {
if(type == digital) {
writeDigital(d2)
} else {
writeAanalog(d2)
}
}
This is absolutely not abstract stuff, and it kinda sucks. How can I connect my configuration to my code?
-
2Why can't you have something like that? Depending on what you need to do with sensors, the code you wrote could be the perfect solution. Keep in mind that your code doesn't necessarily need to reflect the relationship between analog inputs, digital inputs, analog outputs and digital outputs; the best solution may be to treat those four concepts as though they were completely unrelated.Sophie Swett– Sophie Swett2017年06月07日 23:14:25 +00:00Commented Jun 7, 2017 at 23:14
2 Answers 2
This looks like a job for polymorphism!
Stream temperature = new Input( new DigitalProtocol( new Address(d2) ) );
Stream humidity = new Input( new DigitalProtocol( new Address(d3) ) );
Stream lcd = new Output( new AnalogProtocol( new Address(a1) ) );
Create whatever interfaces you need to make that legal and useful and you're on your way. Notice how adding Bidirectional
is no longer traumatic?
-
Polymorphism is great! Good suugestionRhys Johns– Rhys Johns2017年06月07日 22:57:43 +00:00Commented Jun 7, 2017 at 22:57
-
What if I need to instantiate these streams dinamically? In the example I have those sensors in the configuration, but I could have more and not know which...user274721– user2747212017年06月08日 07:48:12 +00:00Commented Jun 8, 2017 at 7:48
-
The code I offered is dynamic. Stick it in any creational pattern you like. If you mean you have your heart set on using a configuration file you just need a parser that knows how to build these streams. Heck you can use some dependency injection framework to do it if you're really hell bent on using a configuration file. It's the main thing they offer anyway: construction code moved into another language like xml or json.candied_orange– candied_orange2017年06月08日 08:00:09 +00:00Commented Jun 8, 2017 at 8:00
I would suggest an abstraction like so: enter image description here
Inspiration taken from Java's InputStream
and OutputStream
.
You can tailor InputSensor
and OutputSensor
to your needs. I added Stream
because from your example, you're treating input and output streams the same, but you may not need it.
On the left side of the diagram is the method that the sensors will be constructed from configuration using the abstract factory pattern. DefaultSensorFactory
would take the configuration, and construct AddressedInputSensor
and AddressedOutputSensor
form the description in your question.
The benefits of this design are that you can test each part independently of each other by mocking each interface, and you can use sensors in an abstract way.
-
1Do you think it could be a little more complex?Frank Hileman– Frank Hileman2017年06月07日 21:52:45 +00:00Commented Jun 7, 2017 at 21:52
-
Which part is complex Frank? I would be happy to explain it more.Samuel– Samuel2017年06月07日 21:55:36 +00:00Commented Jun 7, 2017 at 21:55