Alex Rivera | Logout

When exactly does a class/package depend on another?

Asked 2011-09-03T16:22:24.747
9

Many articles/books/.... talk about class or package dependency, few explain what it is. I did find some definitions, but they vary and probably don't cover all cases. E.g.:

Further aspects to consider are method parameters, dependency injection, aspect oriented programming, generics. Any more aspects?

So, can you give a (formal) definition for dependency amongst classes and amongst packages that is fool-proof and covers all these cases and aspects?

Edit
Report

1 Answer

1

This post is about static dependency - for dynamic dependency and the difference, see Patrick Smacchia's answer.

In an easy to understand way: an entity (class or package) A depends on an entity B when A cannot be used standalone without B.

Inheritance, aggregation, composition, all of them introduces dependency between related entities.

so there exists no dependency on an interface?

there is, but interface only serves as the glue.

what about inheritance?

see above.

so dependency is a transitive relationship not just on packages, but also on class level?

yep.

but how do you define "relies"?

see above "easy to understand" definition. also related to the 3rd definition you posted.


Update:

So if you have interface A in Package P1, and class C in Package P2 uses A as

  • method parameter, or
  • local variable woven into C via AOP, or
  • class C implements A, or
  • class C<E extends A>,

then C depends on A and P2 depends on P1.

But if interface A is implemented by class B and class C programs against the interface A and only uses B via dependency injection, then C still (statically!) only depends on A, not on B, because the point of dependency injection is that it doesn't make glued components dependent.

answered 2011-09-03T17:08:33.447

Your Answer