WouterOet / UsesProvidesExample

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Provides / Uses example

Compile

javac -d mods/module.api module.api/module-info.java module.api/api/Api.java

javac -d mods/module.b -p mods module.b/module-info.java module.b/b/Provider.java

javac -d mods/module.a -p mods module.a/module-info.java module.a/a/User.java

Run

java -p mods -m module.a/a.User

Usage

Although out of scope for the exam, this might be fun to know. We have 3 modules:

  • module.a: A user of the Api interface
  • module.api: Which contains the Api interface
  • module.b: Provides an implementation of the Api interface

Module.a loads the implementation of the Api interface via the ServiceLoader. Note that the module is unaware of the implementation!. It doesn't depend on an implementation, only on the api itself. The ServiceLoader will find one (or more) implementations on the modulepath.

This allows for a very clean separation between the api and implementations as they can be in different modules. A user doesn't have to hardcode a dependency. This can be done at runtime.

About


Languages

Language:Java 100.0%