This discussion is archived
7 Replies Latest reply: Dec 4, 2012 8:13 PM by EJP RSS

OOP - Class Design

977475 Newbie
Currently Being Moderated
Hi!

I'm in very early stages of OOP. Coming from a procedural programming, I'm starting to see the change in mindset when designing my application.

My current task:

Convert numbers, date, time values in different languages. Language is not known until run time.


This is how I designed my classes.

Interface ILang. With methods num_to_words(), date_to_words(), time_to_words().

And above interface implemented by these classes: French. English. Spanish. German...

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?


Thank you!
  • 1. Re: OOP - Class Design
    Kayaman Guru
    Currently Being Moderated
    974472 wrote:
    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..
    Or you could have EnglishNumber, FrenchNumber classes...
    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.
  • 2. Re: OOP - Class Design
    jduprez Pro
    Currently Being Moderated
    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).
  • 3. Re: OOP - Class Design
    977475 Newbie
    Currently Being Moderated
    there will be more languages indeed.. thx!
  • 4. Re: OOP - Class Design
    977475 Newbie
    Currently Being Moderated
    Hi!

    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?


    thank you!

    Edited by: 974472 on Dec 4, 2012 3:34 PM
  • 5. Re: OOP - Class Design
    EJP Guru
    Currently Being Moderated
    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.
  • 6. Re: OOP - Class Design
    977475 Newbie
    Currently Being Moderated
    hi! thanks for the tip. but i'm in the very early stages of learning oop. i would like to know how best to approach a problem/issue like this.
  • 7. Re: OOP - Class Design
    EJP Guru
    Currently Being Moderated
    I'm telling you. Study the design that has already been done, and use as much of it as you can. A new class per language seems like a really bad idea. This is a case where you would want to build a language-agnostic engine and have it data-driven.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points