3

I have 3 Different Classes Which are representing different types of Sensors, for example:

  • WaterLevelSensor
  • DoorSensor
  • TemperatureSensor

Each Class has different functionality.

Following the OOD Principles:

  1. I have decided to make 1 parent class called Sensor.
  2. Make the 3 different types of sensors inherit from the parent class Sensor.

Main Questions:

  1. Is the decision to do it as inheritance is a good design? Or maybe it could be an Interface which the 3 other classes should implement?

  2. In case of a parent class or an Interface, could it be empty? See Image below for clarification.

enter image description here

4
  • 1
    If the parent class doesn't define any methods or attributes, what's the point of it? Commented Mar 25, 2017 at 5:43
  • Being a parent, @jonrsharpe . It allows to treat different children through the same mechanisms. Commented Mar 25, 2017 at 19:00
  • 1
    @Aganju but what "mechanisms"? If there's no interface, what actions can you take by virtue of the hierarchy? Commented Mar 25, 2017 at 19:01
  • You can make arrays of their addresses. But - after thinking a bit - I get your point; you cannot use them for anything if they don't have a common interface. So yes, with no interface, they are useless. Commented Mar 25, 2017 at 19:27

3 Answers 3

2

Do the 3 classes share an interface? That is, do they provide some consistent way to query them? I don't work much with sensors, but it strikes me as odd that a water level sensor and a door sensor would have much in common. In your diagram above, they have 3 different methods they implement (MethodX(), MethodY(), and MethodZ()), but nothing obvious in common.

If each one is able to output, say, a floating point value that represents the voltage coming from the sensor, then it might make more sense to have 3 different classes which each contain a Sensor, but don't inherit from any base class. This is called object composition rather than inheritance.

To answer your other question - it's fine to have a base class that's "empty" in that it has no member variables, and all of its methods are abstract. This would essentially be an interface. Depending on the language you use to implement it, you would either make it an interface (in something like Java or Objective-C), or an abstract base class (in C++).

In my mind what it comes down to is this: will you ever have a need to replace one with any of the others? Given what they do, it doesn't seem likely unless you're the manufacturer of the sensors and writing a test harness to test them before shipping them to customers. In that case, you might want a method like getVoltage() on all of them. But for end users of these classes, you'd probably want something more specific like getWaterLevel(), getDoorPosition(), and getTemperature(). Internally, they could query a Sensor object for its voltage (or whatever would be appropriate). But the interfaces probably wouldn't need to be very similar since they'd be used in such different scenarios.

2
  • Well, WaterLevelSensor for example has a function that returns a float value which is the current level of the water, 'DoorSensor' has a function that returns either the door is Opened or Closed, TemperatureSensor has a function that returns the current temperature of the water. So basically they are 3 different functions with 3 different return types. Based on your answer this means that it is better to use Composition or Aggregation rather than making a base class or an Interface, Right? Commented Mar 25, 2017 at 5:39
  • That's my feeling, yes. Commented Mar 25, 2017 at 5:40
4

If they truly share no behavior then they shouldn't share a base class.

The real question is, do they share behavior and I think the answer is "yes" if your language supports generics or class templates. Even then I would create an interface.

public interface ISensor<T>
{
    T Read();
}

public class WaterSensor : ISensor<float>
{
    public float Read() { ... }
}

Every sensor has to be read in order to be useful. Other than that I don't see any reason these classes should be related.

1
  • Great solution! After making an Interface, The Composition relationship will be between the interface and the Device class, not with each Sensor type, right? Commented Mar 25, 2017 at 18:24
0

I see there could be value in a parent class Sensor because I assume that there is or will be common functionality. For example, each sensor will have some set of attributes that are measured and some criteria which define whether the sensor is considered tripped -- or maybe three levels normal, warning, and critical. So I see a parent class with an interface so that the descendants must implement the common functionality.

The rest of the application handles the generic "Sensor" objects and really doesn't and shouldn't know the inner workings of each sensor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.