• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

cyclic dependency

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm trying to get a few classes to call each other's methods. It's been driving me nuts for a while and eventually I read that this was a cyclic dependency, which I understand is bad and symptomatic of poor design, probably true in this case.

My qustion is what code do I need to add to allow B's methods to call A's and vice versa?

Thanks.

Scott
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please correct me, if this is a bad approach

How about the following ?

public class A:
instance var:
-B
instance methods:
+setB(B newB)
+doStuffOnB()

public class B:
instance var:
-A
instance methods:
+setA(A newA)
+doStuffOnA()

/Svend Rost
 
scott beveridge
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh sorry, the dependency exists between more than two classes.
What I want to do is farely simple: take an input and pass it to an object, perform an operation on it and pass it on. Eventually it is passed back along the chain in the reverse order. Surely there is an easy way to do this?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cyclic dependencies are fairly common in simple callbacks. One way to avoid them is to code to interfaces so everyone depends on interfaces and the classes are nicely decoupled.

Now all three classes depend on IService and IClient, RunTest depends on the two implementations, but the two implementations do not depend on each other. That's a fair bit of effort to remove the cycle, but it allows flexible packaging of interfaces and implementations to achieve reusable components or pluggable components. Good clean fun?
 
scott beveridge
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replys gentlemen, they were a great help. I now have something which does what it is supposed to, so its on to stage 2.

The first time I heard of a callback was when Stan mentioned it in his post. After much research and experimentation I can move on. All those books about java apparently teach you only how to write code, not programs.

So thanks again, and expect more questions.

BTW, Stan I looked into your sig line- very cool stuff.

Scott
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Conway's Law" seeme pretty dull, but it shook me up the first time I read it and recently it seemed like a pretty profound analysis of a situation at work so I dragged it out here.

Keep having fun with design. The OO, UML, etc forum down the page has lots of discussion on OO principles.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic