官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 温泉县| 达州市| 孝昌县| 黑水县| 泸定县| 随州市| 镇赉县| 安阳县| 弥勒县| 马龙县| 靖边县| 黄龙县| 洛川县| 锦屏县| 新龙县| 慈溪市| 霍州市| 合作市| 手游| 平利县| 上思县| 鄢陵县| 石景山区| 民乐县| 宁都县| 焉耆| 林周县| 秦皇岛市| 武清区| 随州市| 启东市| 广丰县| 乌恰县| 伊春市| 邓州市| 永平县| 张家川| 商城县| 屯昌县| 西安市| 宝鸡市|