aspose file tools*
The moose likes Java in General and the fly likes Package Dependencies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Package Dependencies" Watch "Package Dependencies" New topic
Author

Package Dependencies

Chinmay Bajikar
Ranch Hand

Joined: Dec 08, 2001
Posts: 159
Hi all,
Though this is not a pure Java Question...
I would like to know various ways to elimnate package dependencies.
Thanks,
Chinmay


The strength of the Wolf is the pack & the strength of the pack is the wolf....Rudyard Kipling
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

What kind of packages are we talking about if this is not a Java question?


[How To Ask Questions On JavaRanch]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Chinmay Bajikar
Ranch Hand

Joined: Dec 08, 2001
Posts: 159
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

Joined: Jan 29, 2003
Posts: 8791
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Package Dependencies