设置超过100个具有不同值的对象

huangapple go评论80阅读模式
英文:

Setting More than 100 Object having different Values

问题

我正在处理一个遗留项目,在该项目中,我们正在调用一个SOAP服务,该服务要求在我们发送给JAXB对象之前在我们这一端设置许多字段值。
目前,我们是通过如下所示的setter方法来完成的 -

需要设置100个这样的标签,

    FieldValue field1 = new FieldValue(); // 字段值
	field1.setFieldName("FieldLabel1");
	field1.setValue(fileValue);
	lstfield.add(field1);

    FieldValue field2 = new FieldValue(); // 日期格式的操作日期 12-Feb-14
	field2.setFieldName("FieldLabel2");
	field2.setValue(DateUtill.getCurrentdate());
	lstfield.add(field2);

设置100个FieldValue对象将会很繁琐,如果WebService提供商更改任何标签名称,找到并更改标签名称将会非常繁琐。

另一种设置的方法是使用构造函数,但这似乎不是正确的选择。
我在寻找是否有其他方法可以完成上述任务,这将非常有帮助。

英文:

I am working on a legacy project, where we are consuming a SOAP service, that service requires lots of Field Value to be set at our end before we can send it to the JAXB object.
At the moment we are doing it via setter method as shown below -

100 such label needs to be set,

    FieldValue field1 = new FieldValue(); // field value
	field1.setFieldName("FieldLabel1");
	field1.setValue(fileValue);
	lstfield.add(field1);

    FieldValue field2 = new FieldValue(); // Action date in format 12-Feb-14
	field2.setFieldName("FieldLabel2");
	field2.setValue(DateUtill.getCurrentdate());
	lstfield.add(field2);

Setting 100 FieldValue objects would be cumbersome for maintainance, if incase the webservice provider changes any label name it would be very hectic to find and change the label name.

Another way to set is using a constructor but that doesn't seem to be right option.
I was looking if there is any another way to do the above mentioned task that would be very helpful.

答案1

得分: 2

你可以使用循环创建100个 FieldValue 对象,并通过使用 for 循环将它们添加到列表中:

    List<FieldValue> fieldValueList = new ArrayList<>();
    final int NUM_OF_OBJECTS = 100;
    for (int i = 0; i < NUM_OF_OBJECTS; i++) {
      fieldValueList.add(new FieldValue());
    }

然后,你可以在 FieldValue 类中编写一个方法来设置对象的 fieldName 和 value:

  public void setFieldNameAndValue(String fieldName, Object value) {
    setFieldName(fieldName);
    setValue(value);
  }

现在,你可以为生成的列表中的每个项设置 fieldName 和 value:

    fieldValueList.get(0).setFieldNameAndValue("FieldLabel1", fileValue);
    fieldValueList.get(1).setFieldNameAndValue("FieldLabel2", DateUtill.getCurrentdate());
    // 为剩余的对象设置 fieldName 和 value

另一种方法是使用数组而不是列表:

    final int NUM_OF_OBJECTS = 100;
    FieldValue[] fieldValues = new FieldValue[NUM_OF_OBJECTS];
    for (int i = 0; i < NUM_OF_OBJECTS; i++) {
      fieldValues[i] = new FieldValue();
    }

    fieldValues[0].setFieldNameAndValue("FieldLabel1", fileValue);
    fieldValues[1].setFieldNameAndValue("FieldLabel2", DateUtill.getCurrentdate());

已更新,新增另一种解决方案:
我认为在 FieldValue 类中声明 setFieldNameAndValue 不是一个好的方法。请忘记那个解决方案 设置超过100个具有不同值的对象

    final int NUM_OF_OBJECTS = 100;
    final ArrayList<FieldValue> filedValueList = new ArrayList<>(NUM_OF_OBJECTS);
    addNewFieldValue(filedValueList, "fieldName1", fieldValue);
    addNewFieldValue(filedValueList, "fieldName2", DateUtill.getCurrentdate());

addNewFieldValue 可以像这样编写:

  private static void addNewFieldValue(ArrayList<FieldValue> filedValueList, String fieldName, Object value) {
    final FieldValue fieldValue = new FieldValue();
    fieldValue.setFieldName(fieldName);
    fieldValue.setValue(value);
    filedValueList.add(fieldValue);
  }

希望这些代码可以对你有所帮助 设置超过100个具有不同值的对象

英文:

You can create 100 FieldValue objects and add them to a list by using for block:

    List&lt;FieldValue&gt; fieldValueList = new ArrayList&lt;&gt;();
    final int NUM_OF_OBJECTS = 100;
    for (int i = 0; i &lt; NUM_OF_OBJECTS; i++) {
      fieldValueList.add(new FieldValue());
    }

Then you can write a method in FieldValue class to set fieldName and value of objects:

  public void setFieldNameAndValue(String fieldName, Object value) {
    setFieldName(fieldName);
    setValue(value);
  }

and now you can set fieldName and value for each item of the generated list:

    fieldValueList.get(0).setFieldNameAndValue(&quot;FieldLabel1&quot;, fileValue);
    fieldValueList.get(1).setFieldNameAndValue(&quot;FieldLabel2&quot;, DateUtill.getCurrentdate());
    // set fieldName and value for remained objects

Another way is using Array instead of List:

    final int NUM_OF_OBJECTS = 100;
    FieldValue[] fieldValues = new FieldValue[NUM_OF_OBJECTS];
    for (int i = 0; i &lt; NUM_OF_OBJECTS; i++) {
      fieldValues[i] = new FieldValue();
    }

    fieldValues[0].setFieldNameAndValue(&quot;FieldLabel1&quot;, fileValue);
    fieldValues[1].setFieldNameAndValue(&quot;FieldLabel2&quot;, DateUtill.getCurrentdate());

UPDATED with one more solution:
I think declaring setFieldNameAndValue in the FieldValue class is not a good way. Please forget that solution 设置超过100个具有不同值的对象

    final int NUM_OF_OBJECTS = 100;
    final ArrayList&lt;FieldValue&gt; filedValueList = new ArrayList&lt;&gt;(NUM_OF_OBJECTS);
    addNewFieldValue(filedValueList, &quot;fieldName1&quot;, fieldValue);
    addNewFieldValue(filedValueList, &quot;fieldName2&quot;, DateUtill.getCurrentdate());

addNewFieldValue can be programmed like this:

  private static void addNewFieldValue(ArrayList&lt;FieldValue&gt; filedValueList, String fieldName, Object value) {
    final FieldValue fieldValue = new FieldValue();
    fieldValue.setFieldName(fieldName);
    fieldValue.setValue(value);
    filedValueList.add(fieldValue);
  }

Hope these lines can help you 设置超过100个具有不同值的对象

答案2

得分: 1

如果您有一个包含字段的UI类您可以始终创建一个静态字段名称列表更好的方法是创建一个`enum`。例如

    public enum Field {
        FIELD_1("FieldName1"),
        ...
        String name;
        Field(String name) {this.name = name;}
        public String getName() { return this.name; }
    }
    
    ...
    
    FieldValue field1 = new FieldValue(); // 字段值
    field1.setFieldName(Field_1.getName());
    field1.setValue(fileValue);
    lstfield.add(field1);
    

如果您想要进行重构只需在此枚举中更改名称

**根据楼主的评论进行更新**
虽然不是理想的方法但您可以为`setValue`调用创建一个值列表然后使用以下方法添加字段

    public void setFields(Object[] values) {
       for (int i = 0; i < Field.values().length(); ++i) {
           final FieldValue fv = new FieldValue();
           fv.setFieldName(Field.values()[i].getName());
           fv.setValue(values[i]));
           lstField.add(fv);
       }
    }
英文:

If you have a UI class containing the fields, you can always create a static list of field names. Better still, you can create an enum. For example:

public enum Field {
    FIELD_1(&quot;FieldName1&quot;),
    ...
    String name;
    Field(String name) {this.name = name;}
    public String getName() { return this.name; }
}

...

FieldValue field1 = new FieldValue(); // field value
field1.setFieldName(Field_1.getName());
field1.setValue(fileValue);
lstfield.add(field1);

If you want to refactor, Just change the name in this enum.

UPDATED based on OP's Comment
Not the ideal way, but you can create a list of value for setValue call and then use the following method for adding the fields:

public void setFields(Object[] values) {
   for (int i = 0; i &lt; Field.values().length(); ++i) {
       final FieldValue fv = new FieldValue();
       fv.setFieldName(Field.values()[i].getName());
       fv.setValue(values[i]));
       lstField.add(fv);
   }
}

huangapple
  • 本文由 发表于 2020年4月7日 13:26:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/61073331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定