Separated APIs and implementation project structure

The parent-api-impl project structure is new to me. Our Java maven project structure is as follows:

> com.sample.myproject > com.sample.myproject.api > com.sample.myproject.impl 

My questions:

  • How to create such a project in Eclipse so that they are connected with each other?

  • When is such a structure recommended?

  • Is there any site or link I can link to that discusses this structure? Perhaps there is a guide or guide?

+6
source share
2 answers

1) In eclipse, you create project 3 and make a pom.xml file for each to control maven. After that you should do:

  • parent depend on api
  • impl depend on parent and api

Thus, the eclipse m2eclipse plugin will link projects to each other.

2) This project structure is recommended only if you ever need to change the entire implementation of a specific api. This allows you to easily place another jar with a different implementation of the application without changing any java code. Of course, this only applies if you are referring only to the api and not to the implementation. This can be done using EJB or Spring.

3) I can’t think of any textbooks or sites, sorry.

+1
source

I think it makes sense to answer your questions in reverse order ...

Since you have an API, a Snapshot Data Facade can be a good start. The purpose of this template is essentially to provide an interface to which external users can connect.

This type of structure is usually required if you want to expose a number of functions (APIs) without disclosing how you really are about implementing such functions. So, for example, you simply show public BigDecimal calculateTax(BigDecimal amount, double percentage) your users, without showing how you went and implemented your method.

Finally, so I would do it:

  • com.sample.myproject will be my main project, and in it I will put any basic functions, such as saving objects.

  • com.sample.myproject.api will com.sample.myproject.api be a project with Interfaces and other things that I want to open.

  • com.sample.myproject.impl will implement the two projects above, using the main project and some additional logic to determine which API level is available.

+2
source

Source: https://habr.com/ru/post/955892/


All Articles