lundi 7 août 2023

Is there a benefit to having an Interface for the individual Executors?

I'm trying to work on implementing a couple of APIs for a project and have this confusion around the use of Interfaces.

Let's suppose I have three Executor classes and an Activity class as follows:

//A main RandomActivity class as follows:

@AllArgsContructor(onConstructor = @__({ @Inject }))
public class RandomActivity extends Activity {

    private final ExecutorClassA executorClassA;
    private final ExecutorClassB executorClassB;
    private final ExecutorClassC executorClassC;

    public SampleResponse randomExecuteForA(ParameterForClassA parameterForClassA) {
        executorClassA.executeSomeFunctionA(parameterForClassA);
    }

    public SampleResponse randomExecuteForB(ParameterForClassB parameterForClassB) {
        executorClassB.executeSomeFunctionB(parameterForClassB);
    }

    public SampleResponse randomExecuteForC(ParameterForClassC parameterForClassC) {
        executorClassC.executeSomeFunctionC(parameterForClassC);
    }
}

class ExecutorClassA {

    public SampleResponse executeSomeFunctionA(ParameterForClassA parameterForClassA) {
        //do something
    }
}

class ExecutorClassB {

    public SampleResponse executeSomeFunctionB(ParameterForClassB parameterForClassB) {
        //do something
    }
}

class ExecutorClassC {

    public SampleResponse executeSomeFunctionC(ParameterForClassC parameterForClassC) {
        //do something
    }
}

Now I can have a setup like above, where the different classes take in different parameters and execute some logic (which is the "do something" section).

But I can also have an implementation like below, where I can have an Interface for these Executor classes.

interface MainExecutor {

    public SampleResponse execute(Object t);
}


//And then for each of the individual classes

class ExecutorClassA implements MainExecutor {

    public SampleResponse execute(ParameterForClassA parameterForClassA) {
        //do something
    }
}

class ExecutorClassB implements MainExecutor {

    public SampleResponse execute(ParameterForClassB parameterForClassB) {
        //do something
    }
} 

class ExecutorClassC implements MainExecutor {

    public SampleResponse execute(ParameterForClassC parameterForClassC) {
        //do something
    }
}

And then the RandomActivity.class does the same thing as above. 

Is there any added benefit of designing an Interface for this separately and having the Executors implement it? I understand that an Interface enforces a contract, so by having that, the execute function is ensured, but we will be calling the execute function (or a variation of that name) in the RandomActivity class regardless, so is the Interface really required?

Aucun commentaire:

Enregistrer un commentaire