JAVA - extends vs interface - strategy development template

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()
    {
         // draw specific to analog view
    }
}

public abstract class CDigitalView extends CView
{
    public void draw()
    {
        // draw specific to digital view
    }
}

public abstract class CCustomView extends CView
{
    public void draw()
    {
        // draw specific to custom view
    }
}

// concrete implementations
public class CAnalogTempView extends CAnalogView
{
    public void calculate()
    {
        // read and calculate sensor value here
    }
}

public class CDigitalTempView extends CDigitalView
{
    public void calculate()
    {
        // calculate here. same as CAnalogTempView::calculate()
    }
}

public class CCustomTempView extends CCustomView
{
    public void calculate()
    {
        // calculate here. same as CAnalogTempView::calculate()
    }
}
+4
source share
3 answers

. , , .

extends , .

:

calculate(), calculate() .

:

1) CalcInterface calculate().

2) CalcInterface : CAnalogTempCalc, CDigitalTempCalc CCustomTempCalc. calculate() .

3) composition. , Sensor ( )... CalcInterface. PS: display(), .

4) 3 , extends Sensor AnalogSensor, TempSensor CustomSensor...

5) (CAnalogTempCalc, CDigitalTempCalc CCustomTempCalc).

EDIT:

, ... , .

enter image description here

CustomCalcuations, , CalcInterface...

POWER , , .

+2

Sensor View. , . Sensor calculate(), ( double ):

public interface Sensor {
    double calculate();
}

public class TemperatureSensor implements Sensor {
    public double calculate() {
        // specific implementation for the temperature sensor
    }
}

CView, , calculate(), , .

public abstract class CView {
    private Sensor sensor;

    CView(Sensor sensor) {
        this.sensor = sensor;
    }

    public abstract void draw();

    public void calculate() {
        double result = this.sensor.calculate();
        // display the result
    }
}

public class CDigitalView extends CView {
    CDigitalView(Sensor sensor) {
        super(sensor);
    }

    public void draw() {
        // draw specific to digital view
    }
}

, , , , :

Sensor temperatureSensor = new TemperatureSensor();
CView digitalView = new CDigitalView(temperatureSensor);

, . n - , m - , n + m. , , , 6 .

, , , n * m . 9, 3 .

0

, , public abstract void calculate() CView. - ,

0

All Articles