Re: Design question - methods calling methods
Lew <noone@lewscanon.com> wrote in news:ht76kf$e1c$1@news.albasani.net:
Rhino wrote:
... getLocales(), which returns a TreeMap, and then displays the
contents of the TreeMap on the console.
It's mostly a good idea to declare variables as an interface type
rather than a concrete type. The rule of thumb is that you declare
the variable with the loosest possible type that has the behavioral
contract you need.
Thus, you probably want 'getLocales()' to return a 'SortedMap', as
someone suggested in another thread, unless there's something specific
about 'TreeMap' in particular that requires you use only that type or
a subtype thereof.
For a second there, I had no idea what you were proposing.
But I reviewed the Collection Classes in the Java Tutorial and I think I
get it now.
In all honesty, I hadn't even thought of SortedMap when I wrote the code
again the other day. I just knew that I wanted the result to be in
alphabetical order, not random the way that Locales.getAvailableLocales()
provides them. The article on the Map interface pointed out that TreeMap
would assure that I had alphabetical order so I went with that. Now that
you've reminded me about SortedMap, I can see the merit of it. It's not
much different than TreeMap but it does give those additional features,
like range operations. I don't see those as being _necessary_ for my
humble little getLocales() method, which I'm really just writing for
myself, but some future user of the class could conceivably benefit from
those extra features. Or maybe _I_ will get a benefit from those features
a little further down the road! I've modified the code to produced a
SortedMap - just replaced all "Map" with "SortedMap", dead easy! - and
reran my unit tests. Naturally, they still worked fine.
Hmm. Did I do this right?
I had this:
=========================================================================
public Map<String, Locale> getLocales() {
Map<String, Locale> sortedLocales = new TreeMap<String, Locale>();
for (Locale oneLocale : Locale.getAvailableLocales()) {
sortedLocales.put(oneLocale.getDisplayName(locale), oneLocale);
}
return sortedLocales;
}
========================================================================
and changed it to:
========================================================================
public SortedMap<String, Locale> getLocales() {
SortedMap<String, Locale> sortedLocales = new TreeMap<String, Locale>();
for (Locale oneLocale : Locale.getAvailableLocales()) {
sortedLocales.put(oneLocale.getDisplayName(locale), oneLocale);
}
return sortedLocales;
}
========================================================================
The first line of the revised method looks a bit funny:
SortedMap ... = TreeMap ....
Did I do what you meant?
Sigh! I still struggle with understanding what I am doing sometimes.... I
still don't REALLY understand the difference between these:
- Map<String, Locale> sortedLocales = new TreeMap<String, Locale>();
- SortedMap<String, Locale> sortedLocales = new TreeMap<String, Locale>
();
- Map<String, Locale> sortedLocales = new SortedMap<String, Locale>();
- and so forth
Or, by the same token:
- Calendar cal = Calendar.getInstance();
- Calendar cal = new GregorianCalendar();
- GregorianCalendar gcal = new GregorianCalendar();
If I had to write an exam, clearly articulating the distinction between
those and what the implications of each is, I'd surely make a hash of
it....
I can tell if I'm getting a compile error and I can tell if my code is
doing what I want it to do but I still don't always understand WHY one
thing is better than an other or what the real difference is between
things that look very very similar.....
That doesn't fill me with optimism about my ability to persuade an
employer that I would be a useful addition to their team. I imagine
they're going to want people that know that stuff backwards and
forwards....
--
Rhino