This content has been marked as final. Show 7 replies
974472 wrote:Or you could have EnglishNumber, FrenchNumber classes...
I'm not comfortable about this design. Maybe I should have a class Number, or Date, or Time? with methods to_words().
But that would mean inside each object I would have if language = ENG.. then..
How would you design your classes? Which pattern works best with this scenario?Well, as this is a completely synthetic example it's hard to say. Your interface version would definitely be simpler. The abstract factory pattern could be used to create "EnglishNumber/EnglishTime" etc. classes.
I wouldn't get too worked up over this, since in the real world it would be done completely different.
There is a small heuristic you can use: it's easier to add a class than to add a method .
So you can try to evaluate which dimension of your problem is likely to grow in the future:
So far you have identified 3 concepts (number date time) to translate, and 4 languages (so far) to translate them into.
- If you plan to add more languages, with your current design you know that for each new language, you will have to implement one class with 3 methods. Whereas with the other approach, you would have to edit each concept class and add the new language in the method's code.
- If you plan to add more concepts, with your current design you know that for each new concept you will have to edit all 4 language classes, and add the new concept-to-words method. With the other approach you add one concept class and implement the translation for all 4 languages.
At this stage this makes little difference (3/4 methods, 3/4 classes). In the long run (N1 concepts, N2 classes), there may be a significant difference, especially if N1 and N2 are very different (like, 5 concepts and 80 languages).
Ok. I think that EnglishNumber, EnglishDate, EnglishTime... LangNumber, LangDate, LangTime makes more sense. If I have an issue with Lang to number conversion, easy to know what needs fixing.
But then, wouldn't it be overwhelming to have languages * 3 (methods) = total number of classes?
Also, when I instantiate, I would have like:
if session.lang = "ENG" then
n = new EnglishNumber().
t = new EnglishTime().
d = new EnglishDate().
// how deep this nesting depends on the number of languages.
How to make the instantiation simplier?
Edited by: 974472 on Dec 4, 2012 3:34 PM
You need to be aware that Java already contains extensive localization facilities. See java.util.Locale. So the actual dimensions of your task aren't what you might think. You should also be aware of the localization possibilities provided by java.util.ResourceBundle, which largely obviates the need for extra classes at all.