Creating the second module

Let's start by splitting the address book application into two separate modules. The obvious candidate for moving to its own module is the sorting logic. At this point, there's nothing about the sorting class, SortUtil, that has anything to do with the address book. We've designed the class to be generic and provide functionality to sort any list. That's good practice in general, but it makes additional sense when breaking it out as a separate module. What we will do is move the code related to sorting into a brand new module, called packt.sortutil. Here are the steps at a high level:

  1. Create a new module called packt.sortutil.
  2. Move the code related to sorting into this newly created module.
  3. Configure the packt.sortutil module to define its interface--what it exports and how the module needs to be used.
  4. Configure the packt.addressbook module to use the new packt.sortutil module.

Let's start by creating a new module. We've looked at the four steps to create a module in Chapter 2, Creating Your First Java Module, already. We know what the module name is. Next, the module structure requires creating a module root directory in the source folder. Just like the packt.addressbook folder resides in the src folder and holds all the contents of the packt.addressbook module, the packt.sortutil module requires the creation of a folder named packt.sortutil in the same src location. What makes this folder a module root folder is the presence of the module descriptor module-info.java:

    module packt.sortutil { 
    } 

Here's the folder structure at this point:

Now that we have a module, we can move the necessary classes from the packt.addressbook module into the packt.sortutil module. There's just one class related to sorting--SortUtil.java. With the package folders located at the module folder, the folder structure after moving the class over should look as follows:

When creating a module, you'd typically also configure the interface, that is, defining the module's inputs and outputs. We didn't do this to the packt.addressbook module in Chapter 2, Creating Your First Java Module because it existed as a standalone module. However, that's not the case anymore in this chapter, since we'll now need the two modules packt.addressbook and packt.sortutil to work together. This involves updating the module-info.java file for both the modules to provide this information. But let's skip this step for now and examine the default behavior first. Let's observe what happens if we don't add any module configuration and compile the two modules with empty module definition files.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.20.231