N
Novice
Sorry, that's probably not the best of subject lines but I'm having trouble
coming up with a concise one....
I'm trying to reason out the best way to pass information from an
instantiating class to an instantiated class. So, let's say class Foo
invokes class Bar to do something. Bar needs some specific information from
Foo to do its job. What is the best way to pass this information from Foo
to Bar?
For instance, Foo is a class that is used to edit a table of information
presented as a JTable. Bar is a class that is used when a new row needs to
be inserted into the table. The person using Foo right-clicks, chooses
"Insert" on the context menu, and Bar gets instantiated to display a dialog
with input fields so that the user can supply the values for a new row of
the JTable.
Bar needs various things to do its job. Among these are:
- the locale to be used since Bar can display its text in various languages
- the logger to be used for error messages
- a reference to Foo since Bar wants to know the parent JFrame
- the title of the dialog
There are obviously various techniques for passing information from Foo to
Bar. You can put the information in the paramater list. You can use getters
to obtain the information from Foo. You can make values in Foo accessible
to Bar by making the public so that Bar can access them directly. All of
these methods can and do get used in Java.
What are the rules of thumb to use in deciding which techniques to use in
any given case?
Clearly parameter passing is used widely but parameter lists tend to be
short, mostly in the 0 to 4 parameter range. How do you decide which of the
possibly many things needed by class Bar get passed as parameters and which
get passed via getters or accessed directly from Foo?
My strong impression is that it's bad form to access variables directly in
Foo and that getters are preferred, but please correct me if I'm wrong. But
I still don't quite see when you prefer passing something in a parameter
list rather than via a getter.
I'm not sure if there is a generally agreed-upon formula here or whether it
is more a case of individual style and preference. I'd be very interested
in your comments on this subject. Right now, I feel like I'm being pretty
inconsistent in my techniques and would like to standardize them along the
best possible lines.
coming up with a concise one....
I'm trying to reason out the best way to pass information from an
instantiating class to an instantiated class. So, let's say class Foo
invokes class Bar to do something. Bar needs some specific information from
Foo to do its job. What is the best way to pass this information from Foo
to Bar?
For instance, Foo is a class that is used to edit a table of information
presented as a JTable. Bar is a class that is used when a new row needs to
be inserted into the table. The person using Foo right-clicks, chooses
"Insert" on the context menu, and Bar gets instantiated to display a dialog
with input fields so that the user can supply the values for a new row of
the JTable.
Bar needs various things to do its job. Among these are:
- the locale to be used since Bar can display its text in various languages
- the logger to be used for error messages
- a reference to Foo since Bar wants to know the parent JFrame
- the title of the dialog
There are obviously various techniques for passing information from Foo to
Bar. You can put the information in the paramater list. You can use getters
to obtain the information from Foo. You can make values in Foo accessible
to Bar by making the public so that Bar can access them directly. All of
these methods can and do get used in Java.
What are the rules of thumb to use in deciding which techniques to use in
any given case?
Clearly parameter passing is used widely but parameter lists tend to be
short, mostly in the 0 to 4 parameter range. How do you decide which of the
possibly many things needed by class Bar get passed as parameters and which
get passed via getters or accessed directly from Foo?
My strong impression is that it's bad form to access variables directly in
Foo and that getters are preferred, but please correct me if I'm wrong. But
I still don't quite see when you prefer passing something in a parameter
list rather than via a getter.
I'm not sure if there is a generally agreed-upon formula here or whether it
is more a case of individual style and preference. I'd be very interested
in your comments on this subject. Right now, I feel like I'm being pretty
inconsistent in my techniques and would like to standardize them along the
best possible lines.