Forums Register Login

double indirection

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send

of course this is the minimum to do, variables should be private and access via getter methods.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

 
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
>> 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.
+Pie Number of slices to send: Send
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.
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1816 times.
Similar Threads
object Vs instance
Super Class Question?
Protected Modifier still a challenge
constructor
inner classes
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:18:49.