Java 9 Modular Programming Concept

Java 9 Modular Programming Concept is a powerful feature which is introduced in JDK 1.9. Modularity specifies the interrelation and intercommunication between the parts that comprise a softwaresystem. Modular programming outline a concept called the module. Modules are software components thatcontain data (attributes) and functions (operations or methods). Integrated with other modules, together they form a unitary software system. Modular programming provides a technique to decompose an entire system into independent softwaremodules. Modularity plays a crucial role in modern software architecture. It divides a big software systeminto separate entities and helps reduce the complexity of software applications while simultaneouslydecreasing the development effort.

Two most important dimension of modularity are maintainability and reusability, both of whichbring great benefits. If you are familiar with

Maintainability :Maintainability refers to the degree to which a software system is upgraded or modified after its first delivery to production environment. A big,monolithic software system is hard to maintain, especially if it has many dependencies inside the code.

Reusability :Object-oriented programming can be used to obtain reusability, especially via inheritance. In order to reusethe functionality encapsulated in an object, a second object must inherit the first object.

The Definition of a Module An application (or system or software) module is an independent and deployable software body of a larger system that interacts with other modules and hides its inner implementation. It has an interface that allowsinter-modular communication. The interface defines which components it provides for external useand which components it requires for internal use. A module determines a boundary by specifyingwhich part of the source code is inside the module. It also provides flexibility and increases thereusability of the software system.

Modules can be discovered starting from compile-time. A module can expose some of its classes tooutside or can encapsulate them in order to prevent external access. Figure 1-1 illustrates this concept withan example of a module containing classes that are exposed to outside (classes in green color) and classesthat are not exposed to outside (classes in red color).A module can also be viewed as a black box. It has an input and an output and performs a specificfunction. It takes the input, applies some business logic on it, and returns an output.

Module Concept Java 9

A software module is reusable, testable, manageable, and deployable. Multiple modules can becombined together to form a new module. Modular programming is the key to reducing the number ofbugs in complex software systems to a minimum. By dividing the application into very small modules, eachmodules will have fewer defects because its functionality is not complex. Assembling these less error-pronemodules results in an application with fewer errors.

Characteristics of a module

A change performed on a specific module should not have an impact on other modules. Additionally,it should be possible to add a new module to the core system without breaking the system. Because only theinterface of a module is visible from outside the module, it should be possible for developers to change themodules internal implementation without breaking the code in the application. The structure of a modularsoftware application is basically defined by the connections and correlations between modules.Some of the characteristics of a module include the following:

  1. A module must define interfaces for communication with other modules.
  2. A module defines a separation between the module interface and the module implementation.
  3. A module should present a set of properties that contain information.
  4. Two or more modules can be nested together.
  5. A module should have a clear, defined responsibility. Each function should beimplemented by only one module.
  6. A module must be able to be tested independently from other modules.
  7. An error in a module should not propagate to other modules.

Lets give a short example. If we have two Jigsaw modules called M01 and M02 and one package in module M02 called Pkg02 that we want to be accessible in module M01, then the following conditions have to be met:

  1. Module M01 should depend on module M02, module M01 should specify in itsdeclaration that it requires module M02.
  2. Module M02 should export the package Pkg2 in order to make it available to the modulesthat depend on it. In our case, in the declaration of the module M02 we should specifythat it exports package Pkg2

Foundation of Modular application

Four concepts that build the foundation of a modular application:

  1. Strong encapsulation
  2. Explicit interfaces
  3. High module cohesion
  4. Low module coupling

Strong Encapsulation : Encapsulation defines the process of preventing data access from outside by allowing a component todeclare which of its public types are available to other components and which arent. Encapsulation improves code reusability and diminishes the number of software defects. It helps obtain modularity bydecoupling the internal behavior of each object from the other elements of the software application

Explicit Interfaces :The interfaces of a modular system should be as small as possible. If an interface is too big, it should bedivided into several smaller interfaces. An interface should make available to a module only the methods that the module really needs in order to be able to fulfill its business requirements.A modular system typically provides module management and configuration management. Modulemanagement refers to the capacity to install, uninstall, and deploy a module. The installation could bedone from a module repository, for example. In some cases, a module could be deployed instantly withoutrequiring that the system is restarted. Configuration management specifies the capacity to dynamicallyconfigure modules and specify the dependencies between them.

High Module Cohesion :Cohesion measures how the elements of a module are residing together. Module cohesion denotes themodules integrality and coherence in regard to its internal structure. It expresses the rate to which themodules items are defining only one functionality.

Low Module Coupling :Coupling specifies the level of interdependence between modules. Module coupling refers to thedependency between modules and the way they interact. The objective is to reduce module coupling as much as possible, and this is achieved by specifying interfaces for the inter-modular communication. Theinterfaces have the role of hiding the module implementation. The resulting modules are independentand can be modified or swapped without fear of breaking other modulesor worse, breaking the entireapplication.

Tight Coupling vs. Loose Coupling

Tight and loose coupling can refer to classes or modules. Tight coupling between classes is when a class uses logic from another class. It basically uses another class, instantiates an object of it, and then calls the object to access methods or instance variables.Loose coupling is encountered when a class doesnt directly use an instance of another class but usesan intermediate layer that primarily defines the object to be injected. One framework that defines loosecoupling is the Spring framework, where the dependency objects are being injected by the container into another object. Loosely coupled modules can be altered with less effort. Loose coupling is generally accomplished by using small or medium-sized modules. Replacing a module wont affect the system if the new module has the same interface as the module being replaced.Tight coupling means classes are dependent on other classes andit doesnt allow a module to bereplaced so easily because it has dependencies on the implementation of other modules.