Separate API and impl for important layers

Another best practice to ensure loosely coupled application layers is to have separate API and implementation modules in each layer. The following screenshot shows the data layer with two submodules--API and impl:

The data pom.xml defines two child modules:

    <modules>
<module>api</module>
<module>impl</module>
</modules>

The api module is used to define the interface that the data layer offers. The impl module is used to create the implementation.

The business layer should be built using the API from the data layer. The business layer should not depend on the implementation (the impl module) of the data layer. This helps in creating a clear separation between the two layers. The implementation of the data layer can be changed without affecting the business layer.

The following snippet shows an extract from the pom.xml file of the business layer:

    <dependency>
<groupId>com.in28minutes.example.layering</groupId>
<artifactId>data-api</artifactId>
</dependency>

<dependency>
<groupId>com.in28minutes.example.layering</groupId>
<artifactId>data-impl</artifactId>
<scope>runtime</scope>
</dependency>

While the data-api dependency has the default scope--compile--the data-impl dependency has a scope runtime. This ensures that the data-impl module is not available during the compilation of business layer.

While separate API and impl can be implemented for all layers, it is recommended that you use it at least for the business layer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset