Using Java Platform Module Services to improve project design

by | Mar 24, 2022 | code fussion

What exactly is a Java Platform Module, and why do we need them?

Modules are a new and important feature introduced in JDK 9. Modules provide a means of describing the relationships and dependencies of the code that makes up an application. Modules also allow you to specify which parts of a module are accessible to other modules. Modules allow you to create more dependable, scalable programs.

Using services to decouple modules

Services are recurrently used to support a pluggable architecture. They are architecture in its purest form. They were designed to make applications extensible, allowing you to add functionality without having to recompile the entire application.

The Structure of a Service

The Service-Based Keywords

  • Exports: <Package> contains a package name and informs the JVM that other modules can see public types in that package depending on the declared one.
  • Requires: <Module> contains a module name and informs the JVM that the declaring module is dependent on the directive.
  • Requires Transitive <Module> This indicates that the module and all modules that use it are dependent on another module.
  • Provides <interface> with <class> Indicates that a module provides a service implementation.
  • Uses <interface> This indicates that a module makes use of a service.

The core Service architecture

A service is an accessible type that one module wants to use and another module provides an instance of:

A specific type, here called Service, is at the heart of using services. It is implemented by the class Provider, and the module that contains it declares it with a provides — with the directive. Modules that consume services must declare this with a uses directive. They can then use the ServiceLoader at runtime to get instances of all providers for a given service.

Components of A well-designed service

  • Service—In the JPMS, a class or an interface.
  • Consumer—Any piece of code that wants to use services.
  • Provider—A concrete implementation of the service.
  • Locator—The puppet master that, triggered by the consumer’s
  • Services need to be public and in an exported package.
  • Consumers can be internal. They need to read the module defining the service or may even be part of it.
  • Providers must be public but shouldn’t be in an exported package, to minimize misuse and API surface. They need to read the module defining the service.

Invoking a consumer – Code Example

package zoo.visitor;
import java.util.*;
import zoo.tours.api.*;
import zoo.tours.reservations.*;
public class Tourist {
public static void main(String[] args) {
Tour tour = TourFinder.findSingleTour();
System.out.println("Single tour: " + tour);
List<Tour> tours = TourFinder.findAllTours();
System.out.println("# tours: " + tours.size());
}

//module-info.java
module zoo.visitor {
requires zoo.tours.api;
requires zoo.tours.reservations;
}

Finally, modules are expected to play a significant role in Java programming. Although their use is not required at this time, they provide significant advantages for commercial applications that no Java programmer can afford to overlook. Module-based development is almost certainly in everyone’s future as a Java programmer.\

0 Comments

Submit a Comment

Written by Lee N

Lee N is a Certified System Architect, Certified Cloud Engineer, Certified Oracle Database Programmer, and RedHat Administrator II Expert with 5 years of experience in designing, developing, and monitoring web-based systems.

We are Experts in Data Modeling & Intelligent Systems

We develop fast, secure, & reliable systems to simplify business transactions and model data to discover useful information for business decision making. read more

Related Articles

Sealed Classes and Interfaces

Sealed Classes and Interfaces

Sealed classes and interfaces are most applicable to developers of API libraries in which subclasses and subinterfaces must be strictly controlled.

read more

Stay Up to Date With The Latest Technology Updates

Lenhac Limited

Developing world-class software solutions designed to meet your specialized needs to streamline your business operations.

Join Our Newsletter

Stay updated on Ways to Leverage Your Network for Rapid Business Growth using technology

Follow Us

Let’s get social

×