lundi 3 septembre 2018

design patterns how to avoid instanceOf when using List

Imagine you have a menu with dishes each dish should be available in multiple languages (French , English , Arabic , ...). The dish class contains a list with language type objects.

     class Dish { 
           List<Language> languages
           void addLanguage(Language lg){...}
     }

     class Language {String getDescription(){}}

     class French extends Language{}

     class Menu {List<Dish> dishes }

How do i avoid using instance of when wanting a description of a specific language for that dish?

Should i define for each language a get method in the dish class:getFrench(), getArabic(),..?

Or should i keep it in list and check for instance of French by looping the list and then call getDescription() on this list object ?

Or is there a way for a more polymorphic approach?

Aucun commentaire:

Enregistrer un commentaire