I have already discussed many theoretical concepts about “Java 9 Module System” and also developed couple of examples using CMD prompt and IDEs in my previous posts.Now I’m going to discuss about “How to Develop and Test Implied Readability Between Modules With IntelliJ IDEA IDE” in this post.
我已经讨论了许多有关“ Java 9模块系统”的理论概念,并且在以前的文章中还使用CMD提示和IDE开发了一些示例。现在,我将讨论“如何使用IntelliJ开发和测试模块之间的隐式可读性”。这篇文章中的“ IDEA IDE”。
In this series of “Java 9 Module System” posts, it is my fifth post. Before reading this post, please go through my previous post by clicking the following links to understand some basics about Java 9 Modules.
在“ Java 9模块系统”系列文章中,这是我的第五篇文章。 在阅读本文之前,请单击以下链接以浏览我的上一篇文章,以了解有关Java 9 Modules的一些基础知识。
- Introduction to Java 9 Module System Java 9模块系统简介
- Java 9 Module and Module Descriptor Basics Java 9模块和模块描述符基础知识
- Java 9 HelloWorld Module with CMD Prompt 带有CMD提示的Java 9 HelloWorld模块
- Java 9 HelloWorld Module with Eclipse and IntelliJ IDEs 带有Eclipse和IntelliJ IDE的Java 9 HelloWorld模块
发表简要目录: (Post Brief Table of Content:)
- What is Readability? 什么是可读性?
- What is Accessibility? 什么是辅助功能?
- What is Implied Readability? 什么是隐含可读性?
- Develop Module Dependency With Bad Approach 用错误的方法开发模块依赖性
- Test Module Dependency For Bad Approach 测试模块对不良方法的依赖性
- Develop Implied Readability(Good Approach) 开发隐含的可读性(好的方法)
- Advantages of Implied Readability 隐含可读性的优点
什么是可读性? (What is Readability?)
If Module-1 directly depends on Module-2, then it is know as “Module-1 Reads Module-2”. In other words, we can say that “Module-2 is Readable by Module-1”.
如果模块1直接依赖于模块2,则称为“模块1读取模块2”。 换句话说,我们可以说“模块2可以被模块1读取”。
So, It is know as Readability relationship between Module-1 and Module-2. Let us explore it with some JDK 9 Modules using below diagram.
因此,称为模块1和模块2之间的可读性关系。 让我们使用下图通过一些JDK 9模块进行探索。
Here “java.sql” module reads “java.xml”, “java.base” and “java.logging” modules.
在这里,“ java.sql”模块读取“ java.xml”,“ java.base”和“ java.logging”模块。
什么是辅助功能? (What is Accessibility?)
If Module-1 has Readability Relationship with Module-2, then Module-1 can “Access” all Public API of Module-2. It is know as Accessibility Relationship between those two modules. As we discussed in my previous posts, a Module can have only Public API or both Public and Private API.
如果模块1与模块2具有可读性关系,则模块1可以“访问”模块2的所有公共API。 这就是这两个模块之间的可访问性关系。 正如我们在之前的文章中所讨论的,模块只能具有公共API或公共和私有API。
In simple words, Public API means Public Types. If you don’t understand it well, We will discuss on “How to develop Public and Private API” in my coming posts.
简而言之,Public API表示公共类型。 如果您不太了解,我们将在我的后续文章中讨论“如何开发公共和私有API”。
Both Readability and Accessibility concepts are the basis for achieving the two main Goals of Java 9 Module System:
可读性和可访问性概念都是实现Java 9 Module System的两个主要目标的基础:
- Reliable Configuration 可靠的配置
- Strong Encapsulation 强封装
We can define Readability and Accessibility Relationships between modules using the following concepts:
我们可以使用以下概念定义模块之间的可读性和可访问性关系:
- exports clause 出口条款
- requires clause 要求条款
- public modifier 公共修饰语
- to clause 到条款
We will discuss all these concepts in this post with examples except last one (check for my coming posts).
我们将在本文中讨论所有这些概念,并附上最后一个例子(请检查我的后续文章)。
什么是隐含可读性? (What is Implied Readability?)
If Module-1 reads on Module-2 and Module-2 reads on Module-3, then Module-1 reads Module-3. This kinds of Transitive Dependency is known as “Implied Readability” from Moudle-3 to Module-1.
如果模块1读取模块2,而模块2读取模块3,则模块1读取模块3。 这种传递依赖性从Moudle-3到Module-1被称为“隐含可读性”。
It is also known as Implied Dependency.
也称为隐含依赖关系。
Suppose if we have relationship between three modules as shown in the below diagram.
假设我们在三个模块之间具有关系,如下图所示。
“LastName” Module depends on “FirstName”, “FullName” Module depends on Both Modules : “FirstName” and “LastName”. That means there is a Transitive Dependency between these three modules:
“FullName ==> LastName ==> FirstName”
“ LastName”模块取决于“ FirstName”,“ FullName”模块取决于两个模块:“ FirstName”和“ LastName”。 这意味着这三个模块之间存在传递依赖关系:
“ FullName ==> LastName ==> FirstName ”
In Java 9 Module System, We have two approaches to resolve these Module Dependencies.
1. Bad Approach
This approach solves Transitive Dependency between modules without using “Implied Readability”.
在Java 9 Module System中,我们有两种方法来解决这些模块依赖性。
1.错误的方法
这种方法无需使用“隐式可读性”即可解决模块之间的传递依赖性。
Here “FullName” imports both modules using “requires” clauses as shown below:
这里,“ FullName”使用“ requires”子句导入两个模块,如下所示:
module FirstName{
exorts FirstName;
}
module LastName{
requires FirstName;
exports LastName;
}
module FullName{
requires FirstName;
requires LastName;
exports FullName;
}
It is bad approach because LastName is already importing FirstName Module then why don’t we get that import feature to FullName Module?? Instead of using that, FullName is importing both modules. It works perfectly, however it’s not a recommended approach.
这是一种不好的方法,因为LastName已经在导入FirstName模块,那么为什么我们不将该导入功能导入FullName Module? FullName而不是使用它,而是导入两个模块。 它可以完美运行,但是不建议这样做。
In real-time projects, we will have this kind of dependencies on many modules, is it good to import all modules?? Definitely NO, right.
在实时项目中,我们将在许多模块上具有这种依赖关系,是否可以导入所有模块? 绝对不行,对。
2. Good Approach
This approach solves Transitive Dependency between modules using “Implied Readability” Technique.
2.好的方法
这种方法使用“隐含可读性”技术解决了模块之间的传递依赖性。
Here “FullName” imports only LastName module using “requires” clause, but it gets “FirstName” automatically without importing it as shown below:
在这里,“ FullName”仅使用“ requires”子句导入LastName模块,但它会自动获取“ FirstName”,而不会如下所示导入:
module FirstName{
exorts FirstName;
}
module LastName{
requires public FirstName;
exports LastName;
}
module FullName{
requires LastName;
exports FullName;
}
This good approach is also know as “Implied Readability” in Java 9 Module System. So we can implement “Implied Readability” using “public” modifier as shown in the above example.
这种好的方法在Java 9 Module System中也称为“隐式可读性”。 因此,我们可以使用“ public”修饰符实现“隐式可读性”,如上例所示。
We will develop both approaches in the coming sections to understand it well.
我们将在接下来的部分中开发两种方法,以使其更好地理解。
Please go through this post Java 9 HelloWorld Module with Eclipse and IntelliJ IDEs to know on “How to develop Java 9 Modules using IntelliJ IDE”.
请仔细阅读这篇带有Eclipse和IntelliJ IDE的Java 9 HelloWorld模块,以了解“如何使用IntelliJ IDE开发Java 9模块”。
制定错误的方法 (Develop Bad Approach)
In this section we will develop bad approach to solve this Transitive Dependency without using “Implied Readability”. Once we are comfortable with this approach, we will develop right approach in the coming sections.
在本节中,我们将开发不使用“隐式可读性”来解决此传递依赖关系的错误方法。 一旦对这种方法感到满意,我们将在接下来的部分中开发正确的方法。
Let us understand this approach with this digarm:
让我们通过以下摘要了解这种方法:
- Create an IntelliJ IDEA Project 创建一个IntelliJ IDEA项目
Project Name: ImpliedDependency
项目名称:隐式依赖
- Develop FirstName Module Code 开发名字模块代码
- Create FirstName Module 创建名字模块
Module Name: : com.firstname
模块名称:com.firstname
- Create FirstName Module Package 创建名字模块包
Package name: com.firstname.name
套件名称:com.firstname.name
- Create Java Component(s) 创建Java组件
FirstName.java
名字.java
package com.firstname.name;
public class FirstName {
public String getFirstName() {
// Some logic to get FirstName
return "FName";
}
}
- Create FirstName Module Descriptor 创建名字模块描述符
module-info.java
module-info.java
module com.firstname { exports com.firstname.name; }
- Develop LastName Module Code 开发姓氏模块代码
- Create LastName Module 创建姓氏模块
Module Name: : com.lastname
模块名称:: com.lastname
- Create LastName Module Package 创建姓氏模块包
Package name: com.lastname.name
套件名称:com.lastname.name
- Create Java Component(s) 创建Java组件
LastName.java
LastName.java
package com.lastname.name; public class LastName { public String getLastName() { // Some logic to get LastName return "LName"; } }
Name.java
名称.java
package com.lastname.name; import com.firstname.name.FirstName; public class Name { public String getName() { FirstName fname = new FirstName(); LastName lname = new LastName(); return fname.getFirstName() + " " + lname.getLastName(); } }
- Create LastName Module Descriptor 创建姓氏模块描述符
module-info.java
module-info.java
module com.lastname { requires com.firstname; exports com.lastname.name; }
As this module is dependent on FirstName Module, we should add “requires” clause to that module as shown in the above example.
由于此模块依赖于FirstName模块,因此我们应该在该模块中添加“ requires”子句,如上面的示例所示。
- Develop FullName Module Code 开发全名模块代码
- Create FullName Module 创建全名模块
Module Name: : com.fullname
模块名称:com.fullname
- Create FullName Module Package 创建全名模块包
Package name: com.fullname.name
套件名称:com.fullname.name
- Create Java Component(s) 创建Java组件
MiddleName.java
MiddleName.java
package com.fullname.name; public class MiddleName { public String getMiddleName() { // Some logic to get MiddleName return "MName"; } }
FullName.java
FullName.java
package com.fullname.name; import com.firstname.name.FirstName; import com.lastname.name.LastName; public class FullName { public String getFullName() { FirstName fname = new FirstName(); MiddleName mname = new MiddleName(); LastName lname = new LastName(); return fname.getFirstName() + " " + mname.getMiddleName() + " " + lname.getLastName(); } }
- Create FullName Module Descriptor 创建全名模块描述符
module-info.java
module-info.java
module com.fullname { requires com.firstname; requires com.lastname; exports com.fullname.name; }
As this module is dependent on FirstName and LastName Modules, we should add requires clauses to both of them as shown in the above example.
由于此模块依赖于FirstName和LastName模块,因此我们应该在这两个模块上添加require子句,如上例所示。
开发测试模块和测试不良方法 (Develop Test Module and Test Bad Approach)
In this section, we will develop a Test Module to test this Bad Approach.
在本节中,我们将开发一个测试模块来测试这种不良方法。
- Create Test Module 创建测试模块
Module Name: : com.name.test
模块名称:com.name.test
- Create Test Module Package 创建测试模块包
Package name: com.name.test
套件名称:com.name.test
- Create Java Component(s) 创建Java组件
NameTester.java
NameTester.java
package com.name.test; import com.fullname.name.FullName; public class NameTester { public static void main(String a[]){ FullName fullName = new FullName(); System.out.println("Name = " + fullName.getFullName()); } }
- Final Project looks like below. 最终项目如下所示。
- Run the NameTester class 运行NameTester类
Output:-
输出:-
Name = FName MName LName
It works perfectly, however it is not a recommended approach. Let us develop same application using “Implied Readability” Technique.
它可以完美运行,但是不建议这样做。 让我们使用“隐含可读性”技术开发相同的应用程序。
开发和测试隐含依赖性 (Develop and Test Implied Dependency)
- Please follow same steps as we did for Bad approach. 请按照与不良方法相同的步骤进行操作。
- Change “LastName” Module Descriptor 更改“姓氏”模块描述符
module-info.java
module-info.java
module com.lastname { requires public com.firstname; exports com.lastname.name; }
Here we can observe that “public” modifier before Module name. When we use this in requires clause as shown in the above Module Descriptor, that means any Module which reads this “LastName” Module can also reads “FirstName” Module automatically. No need to use extra “requires” clause.
在这里,我们可以看到模块名称之前的“ public”修饰符。 当我们在上面的模块描述符中显示的require子句中使用此模块时,这意味着任何读取此“ LastName”模块的模块也可以自动读取“ FirstName”模块。 无需使用额外的“ requires”子句。
- Change “FullName” Module Descriptor 更改“全名”模块描述符
module-info.java
module-info.java
module com.fullname { requires com.lastname; exports com.fullname.name; }
Here we are not using extra “requires” clause for “FirstName” Module. Still, it can reads that module because of “LastName” Module’s “Implied Readability” Technique.
在这里,我们不在“ FirstName”模块上使用额外的“ requires”子句。 尽管如此,由于“姓氏”模块的“隐含可读性”技术,它仍可以读取该模块。
- Use same Tester module to test this application. 使用相同的测试器模块来测试此应用程序。
Same output for this application too.
此应用程序的输出也相同。
NOTE:-
Here we are using just few modules. However, it is not the same case in Real-time projects. A Real-time Module may depend on more Modules and it is not the right approach to import all of them. Am I right??注意:-
在这里,我们仅使用几个模块。 但是,在实时项目中情况并非如此。 实时模块可能依赖于更多模块,并且导入所有模块都不是正确的方法。 我对吗??几点要记住的要点 (Few Important Points to Remember)
In this section, we will briefly discuss about some important points to remember about Java 9 Module System.
在本节中,我们将简要讨论一些有关Java 9 Module System的重要注意事项。
- Every Module, by definition reads itself. 根据定义,每个模块都会读取自身。
- Java 9 Module System does NOT supoort Cyclic Graphs that means it supports only Acyclic Graphs. Java 9模块系统不支持循环图,这意味着它仅支持非循环图。
- Two or more Modules may have same package names. That means we can use same package names in two different Modules. 两个或多个模块可能具有相同的程序包名称。 这意味着我们可以在两个不同的模块中使用相同的软件包名称。
- By default, all Java 9 Modules (JDK Modules or User Define Modules) depends on “java.base” Module. 默认情况下,所有Java 9模块(JDK模块或用户定义模块)都依赖于“ java.base”模块。
That’s it all about “Develop and Test Implied Dependency With Eclipse IDE” topic. We will discuss some more concepts about Java SE 9 Modules Development in my coming posts.
这就是“使用Eclipse IDE开发和测试隐式依赖关系”主题的全部内容。 在我的后续文章中,我们将讨论有关Java SE 9 Modules Development的更多概念。
Please drop me a comment if you like my post or have any issues/suggestions/type errors.
如果您喜欢我的帖子或有任何问题/建议/类型错误,请给我评论。
Thank you for reading my tutorials.
感谢您阅读我的教程。
Happy Java SE 9 Learning!
Java SE 9学习愉快!
翻译自: https://www.journaldev.com/13632/javase9-implied-readability-part5