Java 11 Cookbook
上QQ阅读APP看书,第一时间看更新

Introduction

Modular programming enables one to organize code into independent, cohesive modules, which can be combined to achieve the desired functionality. This allows us to create code that is:

  • More cohesive, because the modules are built with a specific purpose, so the code that resides there tends to cater to that specific purpose.
  • Encapsulated, because modules can interact with only those APIs that have been made available by the other modules.
  • Reliable, because the discoverability is based on the modules and not on the individual types. This means that if a module is not present, the dependent module cannot be executed until it is discoverable by the dependent module. This helps to prevent runtime errors.
  • Loosely coupled. If you use service interfaces, the module interface and the service interface implementation can be loosely coupled. 

So, the thought process in designing and organizing the code will now involve identifying the modules, code, and configuration files that go into the module and the packages in which the code is organized within the module. After that, we have to decide upon the public APIs of the module, thereby making them available for use by dependent modules.

Coming to the development of the Java Platform Module System, it is being governed by Java Specification Request (JSR) 376 (https://www.jcp.org/en/jsr/detail?id=376). The JSR mentions that a module system should address the following fundamental issues:

  • Reliable configuration: Provide an alternative to the classpath for declaring dependency between components such that developers can prevent their applications from throwing surprises on runtime due to missing dependencies in the classpath.
  • Strong encapsulation: Provide more strict access-control such that something private to a component is private in true sense i.e not accessible even via Reflection and allow the developer to selectively expose parts in the component for use by other components.

The JSR lists the advantages that result from addressing the preceding issues:

  • A scalable platform: The specification in JSR 376 will allow leveraging the different profiles introduced in JSR 337 in the right way by allowing the creation of profiles using different components/modules created in the new platform. This modular platform will also allow other developers to package different components of the Java Platform to create custom runtime thereby giving them an option to create runtime just enough for their use.
  • Greater platform integrity: The strong encapsulation will prevent the purposeful or accidental use of the Java internal APIs thereby giving greater platform integrity. 
  • Improved performance: With the clear dependency between components, it now becomes much easier to optimize the individual components based on the components they interact within in the Java SE platform and outside of it. 

In this chapter, we will look at a few important recipes that will help you get started with modular programming.