Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package Dependencies

 
Chinmay Bajikar
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Though this is not a pure Java Question...
I would like to know various ways to elimnate package dependencies.
Thanks,
Chinmay
 
Joe Ess
Bartender
Posts: 9319
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of packages are we talking about if this is not a Java question?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the subject of much discussion. Robert Martin's "Agile Software Development" spends a lot of time on it. The basic secret is interfaces. To take one of his classic examples, say you have three classes (packages in a sec)
Reader - reads bytes from disk
Writer - writes bytes to disk
Copier - Calls reader to read a byte, calls writer to write a byte
This works fine. But one day we get a requirement to read from a stream instad of a file. Copier depends on Reader, so if we change Reader, we have to change and retest Copier. Bummer.
How do we make Copier NOT depend on Reader? We give Copier ownership of an interface which Reader must implement, and we have a driver program give Copier an object that implements the interface. To give copier ownership of the interface, we package them together. Now reader has a dependency on Copier. This is good, because Copier is much more stable.
Martin calls this the "Dependency Inversion Principle". We often draw architectures with "higher" layers depending on "lower" layers. Here we inverted the dependency so the lower details depend on the higher abstractions, the less stable code depends on the more stable code.
Here is a bit more that I wrote on this ... see if you think it's correct! http://www.surfscranton.com/architecture/DependencyInversion.htm
BTW: This is not easy! I'm writing something with about 50 classes in 6 packages. I downloaded the free JDepend from Clarkware.com, wrote a little script to extract dependencies from JDepend output into the input format for DOT from GraphViz.org to draw package dependency graphs. I haven't spotted an elegant way to get rid of my last cycle yet!
[ October 27, 2003: Message edited by: Stan James ]
 
Chinmay Bajikar
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan,
Your reply was an enlightning one.
The Dependency Inversion Principle will surely help me in the long run.
Thanks again,
Chinmay
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot to mention, further down the forum list is one about OO, UML, etc. You can draw that gang into wonderful debates about this kind of theory and practice.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic