[英]What approach better for POJO (fields formatting logic)?
我正在使用JSON。 所以我有以下POJO類Position
, Person
。 哪里Person
我需要的類。
我只需要接收Person
字段的格式化值(我僅使用此類, Position
其Position
為添加了JSON strucutre的類)
在哪里更好地在Position
或Person
實現格式化邏輯?
第一種變體, 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.