I have a scenario in which several concrete classes are extendsseveral classes Abstract. I find it difficult to come up with a clean structure, reduce the number of files, and avoid repeating code.
The query is to display different sensor values in different ways based on some criteria. Sensor values, such as temperature, voltage, current, etc., may have anlaog widget, numeric label, or a combination of both. I have 3 Abstractclasses for three different species. These classes 3 Abstractimplement a method that defines how a view is displayed. Each type of sensor is of extendsclass 3 Abstractand implements sensor reading methods, performs some engineering conversion and displays the sensor value. The problem here is that the code implemented by specific classes is the same regardless of which class Abstractit extends. CAnalogTempView, CDigitalTempViewand CCustomTempViewall have the same implementation, but extend different classes.
It seems uncomfortable. The code is repeated, and the number of source files increases by 3 times. Did I miss something simple here? Is there a pattern for such a problem? Can I extendsview sensor classes at runtime? Actual code is more complicated. I simplified the statement of the problem for clarity.
EDIT: There are several sensor views that implement view classes Abstract. The method calculate()for each sensor is different. I just listed 3 specific classes for simplicity. In the same way you would like to have CAnalogVoltageView, CDigitalVoltageView, CCustomVoltageViewand CAnalogCurrentView, CDigitalCurrentView, CCustomCurrentViewand etc.
public abstract class CView
{
public abstract void draw();
public abstract void calculate();
}
public abstract class CAnalogView extends CView
{
public void draw()
{
}
}
public abstract class CDigitalView extends CView
{
public void draw()
{
}
}
public abstract class CCustomView extends CView
{
public void draw()
{
}
}
public class CAnalogTempView extends CAnalogView
{
public void calculate()
{
}
}
public class CDigitalTempView extends CDigitalView
{
public void calculate()
{
}
}
public class CCustomTempView extends CCustomView
{
public void calculate()
{
}
}
source
share