Re: Accessing an invoking class's getters?
- From: "Rhino" <no.offline.contact.please@xxxxxxxxxx>
- Date: Thu, 10 Nov 2005 13:03:46 -0500
"Bjorn Abelli" <bjorn_abelli@xxxxxxxxxxxxxxxxxxxxx> wrote in message
news:43737477_1@xxxxxxxxxxxxxxx
>
> "Rhino" wrote...
> >
> > "Bjorn Abelli" wrote...
>
> >> public void actionPerformed() {
> >>
> >> Beta beta = new Beta(this);
> >> }
> >> -------------------------
> >> public class Beta {
> >> Alpha a;
> >>
> >> public Beta(Alpha a) {
> >> this.a = a;
> >> }
> >> }
>
>
> > So, this is a perfectly valid thing to do, right?
>
> Yes.
>
> > I guess I'm still a bit worried about the bigger design
> > question though. Is it better to pass a list of values
> > from Alpha to Beta, even a fairly long list of values,
> > as discrete parameters in the method constructor signature
> > itself?
>
> If there's no other relation between an instance of an Alpha and a Beta,
> this would be the prefered method, as you then decouple the dependence to
> Alpha from a Betas point of view.
>
> If there's a need to have a very long list of values, there are patterns
to
> deal with this.
>
> One is to encapsulate the needed values into one single object, and to
pass
> that instead...
>
I was starting to think along exactly those lines but wasn't sure if that
was good design.
But now that I know it is not bad design to pass an instance of Alpha to a
new instance of Beta via it's constructor, my list of parameters is much
smaller and much more manageable so I don't think I need to pass the
remaining parameters via a single Object.
> > Or is it better to pass a single reference to the invoking
> > class and then use getters from the invoking class to
> > determine the different values needed?
>
> If the Beta instance *anyway* have a dependency of some sort towards an
> instance of Alpha (e.g. if it at some point needs to invoke some method on
> the Alpha instance), this would be the prefered method, and simplifies
> much...
>
> > Also, is it bad form to do a mix of the two techniques,
> > i.e. to pass "important" values as parameters to the
> > constructor and obtain "lesser" values from a reference
> > to the invoking class?
>
> IMHO I would consider it "ugly", and not thoroughly pondered upon...
>
Let me explain myself a bit better with more concrete parameters.
The Beta in my scenario is actually a preferences dialog for Alpha and
should probably be called AlphaPreferences, not Beta. Many of the values
AlphaPreferences needs are things that it is sharing with Alpha, such as a
logger and a locale. But some of the values that AlphaPreferences needs are
to know whether it is modal and what its title should be so that the correct
title is placed in its GUI. The logger and locale seem like things that I
should be obtaining from the alpha instance that is passed to
AlphaPreferences via the 'this' reference. But the modal boolean and the
dialog title "feel" different somehow so I am inclined to pass them as
explicit separate parameters.
I'm not sure I can explain why the logger and locale are different than the
modal boolean and the title though. Maybe it is an unimportant distinction.
For what it's worth, AlphaPreferences subclasses JDialog and shares an
interface of constants with Alpha. I don't expect that anyone is ever going
to subclass AlphaPreferences or that any other class but Alpha is ever going
to instantiate AlphaPreferences. (Maybe I should make AlphaPreferences final
to emphasize that; what do you think?)
Should the fact that no class other than Alpha is ever going to instantiate
AlphaPreferences and that no one is ever going to subclass AlphaPreferences
change anything in how I am going to invoke it?
Rhino
.
- Follow-Ups:
- Re: Accessing an invoking class's getters?
- From: Bjorn Abelli
- Re: Accessing an invoking class's getters?
- From: Chris Uppal
- Re: Accessing an invoking class's getters?
- References:
- Accessing an invoking class's getters?
- From: Rhino
- Re: Accessing an invoking class's getters?
- From: Bjorn Abelli
- Re: Accessing an invoking class's getters?
- From: Rhino
- Re: Accessing an invoking class's getters?
- From: Bjorn Abelli
- Accessing an invoking class's getters?
- Prev by Date: Re: java ifconfig
- Next by Date: Design question re common code
- Previous by thread: Re: Accessing an invoking class's getters?
- Next by thread: Re: Accessing an invoking class's getters?
- Index(es):
Relevant Pages
|