Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do all of the leading open-source Java libraries have circular dependencies among their packages?

I've been researching Java package structure and dependency patterns over the last few weeks. One of the common threads across the writings on the subject is the simple rule that package dependencies should form a directed acyclic graph (DAG). Author Robert Martin even formalized the Acyclic Dependencies Principle (ADP), which states

The dependency structure between packages must be a directed acyclic graph (DAG). That is, there must be no cycles in the dependency structure.

A few Java libraries do adhere to this simple rule. Namely, Spring Framework libraries (spring-core, spring-web, etc) and Google Guava.

However, to my surprise, the majority of leading open-source Java projects do not!

The following open-source projects have circular dependencies among packages:

  • Netflix Hystrix (every package is part of a cycle!)
  • AWS SDK
  • Commons-Lang
  • Commons-Collections
  • Dagger
  • Google Gson
  • Google Guice
  • Hibernate ORM
  • Hibernate Validator
  • Jackson Core
  • Joda Time
  • Play Framework
  • Junit
  • Logback
  • Jetty
  • AspectJ
  • Netty
  • java.util
  • java.lang

Have I misunderstood the software engineering principle? Or do developers discount this package organization technique?

References:

  • Spring-core 4.2.3 [GraphML] [JPG]
  • Google Guava [GraphML] [JPG]
  • Hystrix [GraphML] [JPG]
  • java.util [GraphML] [JPG]
like image 416
Elliot Avatar asked Nov 19 '15 16:11

Elliot


1 Answers

I can confirm that after the analysis of many java projects using JArchitect, many of them contains circular dependencies between packages, the reason is that many of them choose the "package by feature" rather than the "package by layer" approach.

Here's a good article talking about the difference between these two approachs.

Let's take as example some packages from the JDK

enter image description here

These packages are designed by feature, the regex feature is grouped in the java.util.regex package which need some security features from the java.security package and the security classes need also some regex functionalities.

like image 173
James from CppDepend Team Avatar answered Oct 18 '22 21:10

James from CppDepend Team