Post-compile fitness function
As soon as we use the public modifier on a class, the compiler will let any other class use it, even if the direction of the dependency points in the wrong direction according to our architecture.
Since the compiler won’t help us out in these cases, we have to find other means to check that the Dependency Rule isn’t violated.
One way is to introduce a fitness function – a function that takes our architecture as input and determines its fitness in regard to a specific aspect. In our case, fitness is defined as the Dependency Rule is not violated.
Ideally, a compiler runs a fitness function for us during compilation, but, lacking that, we can run such a function at runtime, after the code has already been compiled. Such runtime checks are best run during automated tests within a continuous integration build.
A tool that supports this kind of architectural fitness function for Java is ArchUnit.1 Among other things, ArchUnit...