R
Rhino
Can someone remind me of the idiom for accessing the getters for a class
that invoked another class?
For example, let's say I have instantiated an instance of class Alpha and it
is called alpha. My alpha instance sets some class variables, say foo and
bar, and invokes a class Beta, creating an instance called beta. Now, beta,
the instance of class Beta, needs to see alpha's variables foo and bar. What
is the idiom for seeing those values?
public class Alpha {
String foo;
int bar;
public Alpha() {
setFoo("Dog");
setBar(1);
}
public void actionPerformed() {
Beta beta = new Beta();
}
public void setFoo(String myFoo) {
foo = myFoo;
}
public void setBar(int myBar) {
bar = myBar;
}
public String getFoo() {
return foo;
}
public int getBar() {
return bar;
}
} //end Alpha
public class Beta {
String foo;
int bar;
public Beta() {
// How do I get the values of foo and bar that were set in the invoking
instance?
this.foo = ??.getFoo();
this.bar = ??.getBar();
} //end Beta()
} //end Beta
I *think* I had to do this at some point in the fairly distant past but I
can't find an example in my source libraries. I don't know the exact
terminology for what I'm trying to do so a Google search or looking through
an FAQ gets tricky. I'm hoping someone can remind me of this idiom.
By the way, is this - using the invoking class's getters - considered a bad
thing to do in OO terms?
I'm trying to reduce the number of parameters that Alpha has to pass to Beta
when Beta is instantiated and, since Alpha has already set those values, it
seems reasonable to simply ask alpha what values those variables have. One
alternative, of course, is that I could simply pass those variables to Beta
when I instantiate it but I'm concerned that I'm passing too many parameters
when I do everything that way; some of my "Betas" are getting almost a dozen
parameters passed to them. I'm thinking of passing important values as
parameters and lesser values via the idiom I am asking about.
Any guidance you have to offer on either the specific idiom I've asked about
or the general issue of the best techniques for passing parameters to a
constructor would be greatly appreciated.
that invoked another class?
For example, let's say I have instantiated an instance of class Alpha and it
is called alpha. My alpha instance sets some class variables, say foo and
bar, and invokes a class Beta, creating an instance called beta. Now, beta,
the instance of class Beta, needs to see alpha's variables foo and bar. What
is the idiom for seeing those values?
public class Alpha {
String foo;
int bar;
public Alpha() {
setFoo("Dog");
setBar(1);
}
public void actionPerformed() {
Beta beta = new Beta();
}
public void setFoo(String myFoo) {
foo = myFoo;
}
public void setBar(int myBar) {
bar = myBar;
}
public String getFoo() {
return foo;
}
public int getBar() {
return bar;
}
} //end Alpha
public class Beta {
String foo;
int bar;
public Beta() {
// How do I get the values of foo and bar that were set in the invoking
instance?
this.foo = ??.getFoo();
this.bar = ??.getBar();
} //end Beta()
} //end Beta
I *think* I had to do this at some point in the fairly distant past but I
can't find an example in my source libraries. I don't know the exact
terminology for what I'm trying to do so a Google search or looking through
an FAQ gets tricky. I'm hoping someone can remind me of this idiom.
By the way, is this - using the invoking class's getters - considered a bad
thing to do in OO terms?
I'm trying to reduce the number of parameters that Alpha has to pass to Beta
when Beta is instantiated and, since Alpha has already set those values, it
seems reasonable to simply ask alpha what values those variables have. One
alternative, of course, is that I could simply pass those variables to Beta
when I instantiate it but I'm concerned that I'm passing too many parameters
when I do everything that way; some of my "Betas" are getting almost a dozen
parameters passed to them. I'm thinking of passing important values as
parameters and lesser values via the idiom I am asking about.
Any guidance you have to offer on either the specific idiom I've asked about
or the general issue of the best techniques for passing parameters to a
constructor would be greatly appreciated.