I'm diving into Latin and trying to integrate its interesting grammatical rules into a Java library. The challenge is that in Latin, a word stem often remains the same while numerous suffixes change depending on a word's context. For instance, the word "Servus" (meaning a male slave) transforms based on case: nominative is "Servus," genitive is "servi," dative is "servo," and accusative is "servum." The plural forms also have variations: "Servi," "servorum," "servis," and "servos."
This is a lot to handle, especially since verbs have even more variations! I initially thought of creating a Noun class with a constructor to accept the stem and gender, then writing methods for different cases, like the one shown below that generates the nominative singular. But I can't help feel there could be a more efficient solution. Should I develop a multi-dimensional array for various word states or is there a better approach or design pattern I could apply?
3 Answers
A solid approach might be to create a noun interface or an abstract base class that includes shared rules. For nouns that deviate from those rules, you can create specific classes. This structure allows for easier management if you encounter exceptions. You might also want to implement a NounFactory to return the appropriate class based on the noun you’re working with.
Instead of having specific methods for each form like getNominativeSingular(), you might want to make one generic method that takes both the case and form as parameters. This could simplify your code and avoid repeating similar logic all over the place.
Putting too much logic in the constructor isn't ideal. Consider using design patterns like Factory or Builder. In game development, a common method is to represent words as entities with properties, so you could set up a Word class that tracks different aspects like whether it's a noun or in the nominative case. This helps when dealing with irregular forms, which are common in languages.

Definitely keep the base noun handling separate from the constructor. Store the base noun and just generate the correct form when needed.