Modularity is central to modern software engineering - or may be all type of engineering not just software. We’ll explore support for modularity in Java and .Net platforms, but before that let’s first try to find answers for what and why of modularity.
What is a Module?
Dictionary say a module is “one of a set of separate parts that, when combined, form a complete whole”
So a module needs to have two main characteristics:
What is Modular thinking?
Modular thinking would involve the following:
1. Break down the problem in small parts i.e. modules. Each part would satisfy the following:
2. Create the final solution by composing different modules together
Almost everything around us is an example of modular thinking e.g. Automobiles, Personal Computer, Electronic Devices, etc.
Most of today’s software solutions are built using tools and platforms that are built in modular approach e.g., Spring Framework and .Net Framework, two very popular frameworks in Java and .Net, are both modular in nature. Both have number of modules (libraries) and those modules can be used (or reused!) in solutions for different problems.
But why Modularity?
What do we gain from modular approach to the solution? I think the following three are the primary benefits:
Logical & Physical Modules
In software applications, logical grouping of functions and services is done to create a modular design. This would be an outcome of the application of Software Architecture principles on the problem domain e.g., the following modules may be identified for a Ticket Booking application, each focused on a specific area of the solution.
However, that’s still a design of the software application. Working software (application binaries) can be in one of the two forms shown below:
a. an executable file containing all the modules. Here, the modules may internally be organized separately in the source tree of the application, but physically they all are in one executable file produced by building the application.
b. each module is a library and the application is using the libraries. Libraries are built and released separately from the main Application.
Option (b) is the modular approach that we all are familiar with and use everyday. Option (b) gives all the benefits of modularity listed above.
It is said that Unit of Release is the Unit of Reuse. There is no scope for reuse in Option (a).
Required Platform Capabilities
To support the modularity of option (b), a platform used to build the software application needs to have at least the following capabilities:
Let’s compare Java and .Net on these parameters:
*The package structure at runtime is used to locate .class files. JVM would look for exact same directory structure as the package name e.g. all types under “com.covidservice.api.service” should be found under “com/covidservice/api/service” on the Classpath JARs. So a package is not really treated as a dependency by JVM. JVM searches for .Class file when it has to create a new Class or Instance — this is different from how dependencies are managed in Java 9 and .Net.
**Java does not support versioning at JVM layer. The versions of JARs are maintained using naming convention i.e. including version in JAR filenames like spring-web-1.0.0-RELEASE.jar. If you have wrong version on Classpath or Modulepath, JVM would use that as long as the .Class file (Java 8) or Module (Java 9) are found.
Before module system was introduced in Java, OSGi came up with a dynamic module system. OSGi is an industry alliance for “a vendor-independent, standards-based approach to modularizing Java software applications and infrastructure”. OSGi was formed in late 1990s. There are many implementations of OSGi specifications like Apache Felix, Eclipise Equinox, etc.OSGi component system is used to build Eclipse IDE, JBoss, WebLogic, etc.
Summary
Modular architecture is a necessity once complexity and scope grows beyond certain point. It is difficult to introduce modularity at a later stage so better to do it from the beginning.
Modular design can be created by applying principles of Software Architecture, but platforms used to build the software need to support modular architecture. Java 9 and .Net are more capable than Java 8 — for reasons explained above — to enforce modularity.
Previously published at https://dev.to/pathiknd/modularity-in-java-and-net-3dnj