mardi 7 mai 2019

Splitting Interfaces And implementations into separate Modules/JARs

I am building a component that does some tax calculations. I have created two Maven modules tax-utility-api and tax-utility-impl.

tax-utility-api contains interfaces for the component

public int getTaxableIncome(int income);

and tax-utility-impl contains the actual implementations

public int getTaxableIncome(int income){

// some code

}

The reason why I split the interfaces and implementations into different JARs was so that other components will depend only the api(contract) JAR to develop code at their end. And the impl JAR will be required only during runtime.

But as I tried to create a test component that depends on this component's api JAR, I realized that I am not able to use the functions as they are interfaces and not concrete methods. So instead depending on the impl I am able to use component's functions.

That has confused me now and I have two questions:

  1. Firstly, to make other components just depend on the interfaces in my api JAR do I need a DI framework like Spring?
  2. Did I misunderstood the real benefit/purpose of splitting the interfaces(contract) and implementation into different modules/JARs?

Thanks for reading.

Aucun commentaire:

Enregistrer un commentaire