mercredi 22 janvier 2020

How to design class property containing a collection of translations? For patterns gurus

I am design pattern newbie, please help to properly organize classes.

My entity has a property 'name' that should have several translations:

class Entity {
   private int id;
   private TranslationCollection name;
   ...
}

'name' - should contain a set of translations:

{
  'en': 'english word',
  'sp': 'spanish word',
  ... 
}

So I decided to make class 'Translation':

class Translation {
  private string locale;
  private string value;
  public constructor (locale, value) { ... }
  ...
}

and class 'TranslationCollection':

class TranslationCollection {
   private translationArray[];
   public constructor() { 
     this.translationArray = new array() 
   }
   public addTranslation(Translation translation) {
      translationArray.push(translation);
   }
   ...
}

and there is requirements:

1 locales could be only from 'listOfLocales' which is available only at runtime.
2 Translation collection could not have repeating locales.
3 Unit testable.

Is it normal to inject 'listOfLocales' into 'Translation' constructor to check locale is in the list? In that case I will have three parameters in 'Translation' constructor. And I think it is not good design.

Or should I have some 'TranslationFactory' with 'listOfLocales' injected in the constructor to create Translation with proper locale?

Or may be I am on the wrong way, and there is a better solution?

An how to organize check that 'TranslationCollection' has no repeating locales? Should I check in addTranslation method that there is no array items with locations matching new Translation.location programmatically, which seems not best solution. Is it a way to organize non repeating collection more declaratively?

Aucun commentaire:

Enregistrer un commentaire