Return to site

🎬📦 JPMS MODULES QUIZ: WHERE SHOULD THE SERVICE INTERFACE LIVE?

· java

Java modules become much clearer when you recognize this pattern:

consumer → service contract → provider

🔸 THE QUESTION

You have these two module declarations:

Which statements are correct?

▪️ MovieService should be declared inside actor

▪️ MovieService should be declared inside production.movie

▪️ MovieService should be declared inside serviceapi

▪️ production.movie is required at runtime when running actor, but actor only needs serviceapi during compilation

▪️ actor is required at runtime when running production.movie

🔸 PAUSE HERE AND TRY IT YOURSELF

A simple way to reason about it:

▪️ actor is the client

▪️ production.movie is the implementation

▪️ serviceapi is the shared contract

Now ask yourself:

▪️ Where should the service interface live so both sides can use it properly?

▪️ Which module is needed only when the application runs and service discovery happens?

So the right place for the service interface is serviceapi.

🔸 TLDR

For JPMS service questions, the interface usually belongs in a separate API module.

Think about it like this:

▪️ serviceapi = contract

▪️ actor = consumer

▪️ production.movie = provider

Once you see that pattern, these module questions become much easier.

🔸 THE ANSWER

The correct statements are:

▪️ MovieService should be declared in serviceapi

▪️ production.movie must be present at runtime for actor, but actor only needs serviceapi during compilation

Why?

Because both uses and provides ... with ... point to the same service type.

That service type should be placed in a separate shared module, so:

▪️ the consumer depends on the contract

▪️ the provider depends on the contract

▪️ neither side is tightly coupled to the other side

That is the clean JPMS service design.

If MovieService were inside actor, then the provider would depend on the consumer, which is poor design.

If MovieService were inside production.movie, then the consumer would depend on the implementation, which also breaks the clean architecture.

🔸 TAKEAWAYS

▪️ uses means the module consumes a service

▪️ provides ... with ... means the module offers an implementation

▪️ The service interface usually belongs in a shared API module

▪️ Consumers depend on the contract, not on providers

▪️ Providers depend on the contract, not on consumers

▪️ In service-loading questions, some modules are needed only at runtime

#Java #OCP #JavaCertification #JPMS #JavaModules #ServiceLoader #OpenJDK #Programming #SoftwareArchitecture #Developers

Go further with Java certification:

Java👇

Spring👇

SpringBook👇

JavaBook👇