繁体   English   中英

哪种方法更适合POJO(字段格式化逻辑)?

[英]What approach better for POJO (fields formatting logic)?

我正在使用JSON。 所以我有以下POJO类PositionPerson 哪里Person我需要的类。

我只需要接收Person字段的格式化值(我仅使用此类, PositionPosition为添加了JSON strucutre的类)

在哪里更好地在PositionPerson实现格式化逻辑?

第一种变体, Position class格式化逻辑

public Position {
    private String value;


    public String getFormattedValue() {
        //Value formatting...
        return value;
}

public Person {
    private Position position;
    ..other fields

    public String getFormattedValue() {
        return position.getFormattedValue();
    }
}

//using 
String neededFormattedValue = new Person().getFormattedValue();

第二变体, Person class格式化逻辑

public Position {
    private String value;

    public String getValue() {
        return value;
}

public Person {
    private Position position;
    ..other fields

    public String getFormattedValue() {
        String value = position.getValue()
        //Value formatting...
        return value;
    }
}

//using 
String neededFormattedValue = new Person().getFormattedValue();

我建议使用第二个变体,因为您的Position类不应具有格式化逻辑。 第二个是松散耦合的。

我一直无法在JSON,POJO和XML之间进行转换。

因此,最好使用确切的名称作为get / set的名称,尽管第一个字符始终为大写。

例如。 如果字段使用正确的格式,则Android Studio通常会建议方法(函数)名称。 其中该类包含private String value; ,当您开始输入public get... AS时应提供一个列表。 get方法的正确语法是public getValue() ...

public class abc()
{
  private String value;
  private int some_id_of_a_record;
  private String anotherString;

  public String getValue()
  {...}

  public int getSome_id_of_a_record()
  {...}

  public String getAnotherString()
  {...}

...}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM