Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 如何设计包含翻译集合的类属性?模式大师_Oop_Design Patterns - Fatal编程技术网

Oop 如何设计包含翻译集合的类属性?模式大师

Oop 如何设计包含翻译集合的类属性?模式大师,oop,design-patterns,Oop,Design Patterns,我是设计模式新手,请帮我正确组织课程 我的实体有一个属性“名称”,该属性应具有多个翻译: class Entity { private int id; private TranslationCollection name; ... } { 'en': 'english word', 'sp': 'spanish word', ... } “名称””-应包含一组翻译: class Entity { private int id; private Tr

我是设计模式新手,请帮我正确组织课程

我的实体有一个属性“名称”,该属性应具有多个翻译:

class Entity {
   private int id;
   private TranslationCollection name;
   ...
}
{
  'en': 'english word',
  'sp': 'spanish word',
  ... 
}
名称””-应包含一组翻译:

class Entity {
   private int id;
   private TranslationCollection name;
   ...
}
{
  'en': 'english word',
  'sp': 'spanish word',
  ... 
}
所以我决定上“翻译”课:

class Translation {
  private string locale;
  private string value;
  public constructor (locale, value) { ... }
  ...
}
class TranslationCollection {
   private translationArray[];
   public constructor() { 
     this.translationArray = new array() 
   }
   public addTranslation(Translation translation) {
      translationArray.push(translation);
   }
   ...
}
和类“TranslationCollection”:

class Translation {
  private string locale;
  private string value;
  public constructor (locale, value) { ... }
  ...
}
class TranslationCollection {
   private translationArray[];
   public constructor() { 
     this.translationArray = new array() 
   }
   public addTranslation(Translation translation) {
      translationArray.push(translation);
   }
   ...
}
还有一个要求:

1 locales could be only from 'listOfLocales' which is available only at runtime.
2 Translation collection could not have repeating locales.
3 Unit testable.
将“listofocales”注入“Translation”构造函数以检查区域设置是否在列表中是正常的吗?在这种情况下,我将在“Translation”构造函数中有三个参数。我认为这不是一个好的设计

或者我应该在构造函数中注入一些带有“listOfLocales”的“TranslationFactory”,以创建具有适当语言环境的翻译

或者我走错了路,有更好的解决办法吗

如何组织检查“TranslationCollection”是否没有重复的区域设置?我是否应该以编程方式在addTranslation方法中检查没有位置与new Translation.location匹配的数组项,这似乎不是最佳解决方案。这是一种更具声明性地组织非重复集合的方法吗