[英]What approach better for POJO (fields formatting logic)?
I'm working with JSON. 我正在使用JSON。 So I have following POJO classes
Position
, Person
. 所以我有以下POJO类
Position
, Person
。 Where Person
my needed class. 哪里
Person
我需要的类。
I need to receive formatted values of fields only of Person
(I'm using only this class, Position
it's class accroding my JSON strucutre) 我只需要接收
Person
字段的格式化值(我仅使用此类, Position
其Position
为添加了JSON strucutre的类)
Where better implement formatting logic in Position
or Person
? 在哪里更好地在
Position
或Person
实现格式化逻辑?
1st variant, formatting logic in Position class
第一种变体,
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();
2nd variant, formatting logic in Person class
第二变体,
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();
I would suggest to have second variant, as you Position class should not have formatting logic. 我建议使用第二个变体,因为您的Position类不应具有格式化逻辑。 The second one is more loosely coupled.
第二个是松散耦合的。
I have been having problems converting between JSON, POJO and XML. 我一直无法在JSON,POJO和XML之间进行转换。
So it seems best to use the exact name for get / set, albeit the first character - which is always capitalized. 因此,最好使用确切的名称作为get / set的名称,尽管第一个字符始终为大写。
Eg. 例如。 If the fields use correct formatting, Android Studio usually suggests the methods(functions) names.
如果字段使用正确的格式,则Android Studio通常会建议方法(函数)名称。 Where the class contains
private String value;
其中该类包含
private String value;
, when you begin typing public get...
AS should provide you a list. ,当您开始输入
public get...
AS时应提供一个列表。 The correct syntax for the get method would be public getValue() ...
. 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.