How do you imagine sensors and actuators that both can be digital and analog?

I am working on a project related to hardware, especially sensors and actuators. Both sensors and actuators can be digital or analog. An example of all four types:

  • digital sensor: returns one of several possible states (ie "presence" / "absence" / "impossible to know")
  • analog sensor : returns any value in the range, but results are rounded based on the scaling interval (i.e. temperature sensor returns any value between 0 and 60 degrees Celsius with one digit precision, for example 18.5 C)
  • digital drive: can get one of several possible states (that is, the motor that opens the window can set the window "open" / "half open" / "closed")
  • analog drive: can transmit any value from the range, and the value is also rounded according to the scaling interval (i.e. fan rotations per second can be from 0 to 10, for example 9.5).

The question is , how can I represent these classes without violating the principles of OOP, at least to make the design clear and logical.

One of my solutions in the picture:

uml_diagram_solution_1

But I think this solution is "ugly"

The next solution is here:

uml_diagram_solution_2

This solution is also bad because of the redundant attributes and non-standard method names.

What do you suggest? Maybe there is a design pattern that solves my problem? Thanks for the help!

+3


source to share


2 answers


Finally I came to a solution. In my particular case, the Sensor and Actuator classes are just an abstraction for the current value and port number data. So I decided to get rid of the difference and make one class for both sensors and actuators. And discrete and continuous devices have their own classes.



solution

+1


source


I would solve this by using two different operations to return analog or digital values. The reason is that you will not mix them. If you read the temperature, you get readAnalog()

to get the value, and for the switch you would call readBinary()

. This way you can implement that reading float from binary will throw an exception.



Also, I wouldn't mix this in Device

. Actuators and gauges are very different and the only thing they can inherit is a plastic case (and even that seems strange).

0


source







All Articles