Your Privacy

This site uses cookies to enhance your browsing experience and deliver personalized content. By continuing to use this site, you consent to our use of cookies.
COOKIE POLICY

Java 9 | When to Use Modules

Java 9 | When to Use Modules
Back to insights

The release of Java 9 added strong encapsulation to the module system. This seemingly small addition has major implications for improving software development efforts – the module system offers additional support for SOLID development.

When to Use Java Modules

There are a couple of reasons why it makes sense to choose modules during development. Many Java development houses select Spring or Spring Boot just to satisfy the “D” in SOLID development. If a Java project only requires Dependency Injection, Java 9 can do this without any additional frameworks. Spring, and occasionally Spring Boot, require configuration files to map Java class implementation to interfaces. Creating these configuration files can be tedious. Modules do not require the same configuration files, which saves development time and, therefore, money. Modules also support the “S” in SOLID development. Software developers can decompose a monolithic Java application into many single-purpose modules. Properly architected applications using SOLID principles and modules can yield applications that are easier to maintain than if the application were a monolith, which again saves money.

Code Example

If you’d like to get started with an example implementation, download the code located on Github here. Edit the build script variables as necessary to match your file system and then run it. The output of the main class will display. Notice that the main class only imports interfaces Coffee and CoffeeService to achieve its goal. Further inspection shows that interfaces are encapsulated inside of their own modules, com.example.coffee and com.example.coffeeservice. Implementation files are in different modules, com.example.coffee.impl and com.example.coffeeservice.impl.

Modularization requires a module-info.java file for each module. Compare the module-info.java files inside of com.example.coffee and com.example.coffee.impl for guidance on the visibility keywords required for separating interfaces from implementation.

It is important to point out the ServiceLoader class in the main class. The ServiceLoader is the class responsible for resolving the implementation files. Given an interface, the ServiceLoader can return an Iterable containing the files that implement the interface. The desired implementation is selected from the Iterable.

Conclusion

It makes sense to select Java modules over Spring or Spring Boot if you just need a lightweight system for SOLID development. UDig has a crew of excellent Java developers that are skilled in everything from Spring Boot to Adobe Experience Manager and can be a great resource for your next Java project.

About The Author

Cody Halbleib is a Senior Consultant on the Software team. His family is his partner, Jamie, and his Border Collie mix, Walter.

Digging In

  • Digital Products

    Unlocking Business Potential: The Power of Custom Application Development

    Like any savvy business leader, you’re likely always on the lookout for tools to give your company a competitive edge. And in doing so, you’ve undoubtedly considered investing in custom application development. But the question is, how do you ensure that such a major investment in a custom web application development provides a strong return on […]

  • Digital Products

    Mastering Legacy Application Modernization: Strategies for Success

    The ironic truth of the business world is that change is the only constant. But this means that failing to keep pace with the competition and its technologies will only end with you falling behind. That’s where legacy application modernization enters the fold. When you modernize legacy applications, your team gains access to new features […]

  • Digital Products

    CTO Confessions Podcast

    In this episode of CTO Confessions, Rob Phillips, the Vice President of Software Engineering at UDig, digs into his journey from a passionate technologist in his youth to a seasoned leader in the tech industry. He shares valuable lessons on transitioning to senior leadership, the importance of understanding and articulating company problems, and the art of empowering teams for high performance.

  • Digital Products

    Navigating the Challenges of On Premise to Cloud Migration

    In today’s rapidly evolving technological landscape, the shift from on premise solutions to cloud-based infrastructure has become a pivotal transformation for organizations seeking to modernize their IT operations. This transition holds the promise of increased agility, cost savings, and enhanced scalability. However, it is not without its set of formidable challenges that organizations must navigate. […]

  • Digital Products

    The Power of Transferrable Skills in Tech Projects

    Every project has its own unique elements that require flexibility to be effective and achieve success. This often requires picking up new pieces of a tech stack, learning a new programming language, or a new project methodology. Fortunately, there are also many transferrable skills that carry over from one project to the next. In my […]

  • Digital Products

    The Four Pillars of Effective Digital Product Development

    In 2020 alone, approximately two billion consumers purchased at least one digital product. From software licenses to mobile apps and tech tools, consumers are becoming increasingly active in the digital product market, a trend that has naturally spurred brands across a wide range of industries to reevaluate their digital product design and development process workflows. […]