• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

double indirection

 
Chris Pat
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Yes, I know that subject is "C-ish", but please dont condemn me, grin. I have a class C that creates an instance of class B. Now class B has an instance variable of class A. From class C I want to get access to an member variable of class A. I am not extending the classes so they are not on the stack, but how do I get access to that "buried" variable? My trivial understanding is something like IWant = C.B.A.ThisVariable.
 
Julien Grenier
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

of course this is the minimum to do, variables should be private and access via getter methods.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get comfortable with making this work, then come back and ask why it might not be a good idea some times. I'm trying to tease you into getting interested in more advanced design ideas. Let us know how this goes.
 
Chris Pat
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan
Okay, so I was crudely right. From my reading the bad thing about this is that the instance variables are public and other classes can use them "behind my back". What I should do is make them private and proxy them with s/getters. How "deep" can I go with that "." referencing?


Originally posted by Stan James:
Get comfortable with making this work, then come back and ask why it might not be a good idea some times. I'm trying to tease you into getting interested in more advanced design ideas. Let us know how this goes.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can go arbitrarily deep.

Any time class A needs to access a variable in a class B object it's holding, that's a sign that you've probably got a problem with your design: the code that you're writing in class A probably belongs in class B.

If you're writing code in A and you have a reference to B and B has a reference to C, and A needs a variable in C, then you don't "probably" have a flawed design: you absolutely have one. The code absolutely doesn't belong in A: it belongs either in B, or, ideally, in C.

And if you feel the need to type "A.B.C.D.x", then don't be surprised if the Java Police kick down your door and yell FREEZE!
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> From my reading the bad thing about this is that the instance variables are public and other classes can use them...

Yes, a long-standing C++ convention that is also used in Java. C++ has far better protection facilities such as declaring methods as immutable, having "protected" apply to sub-classes and not everything in the package, for example.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I first learned OO and worked with some pretty cool Smalltalk experts long chains of variables were common. I don't know when people realized that every class suddenly knew a whole lot about a bunch of other classes and that made them all fragile - change one and the whole chain breaks. It's better to just ask an object to do something.

topLevelObject.doSomethingUseful

and let that object hide how it does it. For a humorous look at this, see Don't Confuse Your Dog near the bottom of that link.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic