Java 使用字段序列化对象取决于其他字段

Java 使用字段序列化对象取决于其他字段,java,serialization,coding-style,refactoring,Java,Serialization,Coding Style,Refactoring,假设我有一个具有给定结构的类: class Problem { private String longString; private String firstHalf; private String secondHalf; } firstHalf和secondHalf是根据longString计算的,在我的应用程序中广泛使用,但我不想序列化它们。现在,为了使这个对象的序列化工作,我需要一个用于longString的setter。我想保护从longString计算firstHalf和

假设我有一个具有给定结构的类:

class Problem {
  private String longString;
  private String firstHalf;
  private String secondHalf;
}
firstHalfsecondHalf是根据longString计算的,在我的应用程序中广泛使用,但我不想序列化它们。现在,为了使这个对象的序列化工作,我需要一个用于longString的setter。我想保护从longString计算firstHalfsecondHalf的不变量,只有当longString存在时才存在,并且传递给longString的值在某种意义上是正确的,即可以计算出前半部分和后半部分。我目前的解决方案是让长字符串的setter编写如下:

public void setLongString(String value) {
  this.longString=value;
  this.firstHalf=computeFirstHalf(value);
  this.secondHalf=computeSecondHalf(value);
}
该代码还暗示了长串和上下半场之间的紧密相关性

class Problem {
  private String longString;
  private String firstHalf;
  private String secondHalf;

  //Getters of All Variables
    ......
    ......
    ......

  // Setters of All Variables.

  public void setLongString(String longString){
     this.longString = longString;
     }

  // public but no Arguments so that user won't be able to set this Explicitly but 
  //make a call Outside of the Class to set Only and only if longString is there.

  public void setFirstHalf(){   
       if(this.longString == null)
            throw new Exception("Long String is Not Set.");
       this.firstHalf = this.computeFirstHalf(this.longString);
   }

     // public but no Arguments so that user won't be able to Set Explicitly but 
    //make a call Outside of the Class to set Only and only if longString is there.

   public void setSecondHalf(){  
       if(this.longString == null)
            throw new Exception("Long String is Not Set.");
       this.secondHalf = this.computeSecondHalf(this.longString);
   }
//private method not Accessible outside of Class
   private String computeFirstHalf(final String value){ 
     //Your Logical Code. 
    }
 //private method not Accessible outside of Class
   private String computeSecondHalf(final String value){ 
        //Your Logical Code.
}
然而,有一件事让我感到困扰,那就是方法setlonstring实际上做了三件事,而它的名称并不能反映它的真实行为

有没有更好的编码方法

编辑1:
我正在使用Jackson-to-json序列化程序,我有上半部分和下半部分的getter,用@JsonIgnore注释


我想表达一下长串和它的一半之间的紧密耦合。

上半部分和下半部分的计算可以惰性地完成(即使用getter)


后半段也可以这样做。

您正在通过标准java序列化机制或自定义序列化程序进行序列化?如果您想序列化对象,但不想在对象内序列化特定字段,请将这些字段标记为transient`transient String something=“”;'此外,无论如何都应该将所有字段设置为私有,并提供适当的getter/setter方法。然后,您可以在set方法中检查字段是否已设置,而不重置值,或任何其他有意义的业务逻辑。这是面向对象设计的一个好处我正在使用Jackson to json序列化程序。我的字段是私有的,并且我有getter(被Jackson忽略),如果longString的所有值都可以计算上半部分/下半部分,我将编辑我的问题以显示它看起来不错。如果不是这种情况,我更喜欢计算半个setLong字符串。这样我们就可以保护类不变量。如果longString不好,我们会立即失败,而不是在getFirstHalf调用期间。当我们不需要符合JavaBeans时,它看起来很好。建议的解决方案不符合要求(设置*无参数的方法)。有些框架能够适应这种定制(对于jackson,我们可以用@JsonProperty注释compute*Half)
class Problem {
  private String longString;
  private String firstHalf;
  private String secondHalf;

  //Getters of All Variables
    ......
    ......
    ......

  // Setters of All Variables.

  public void setLongString(String longString){
     this.longString = longString;
     }

  // public but no Arguments so that user won't be able to set this Explicitly but 
  //make a call Outside of the Class to set Only and only if longString is there.

  public void setFirstHalf(){   
       if(this.longString == null)
            throw new Exception("Long String is Not Set.");
       this.firstHalf = this.computeFirstHalf(this.longString);
   }

     // public but no Arguments so that user won't be able to Set Explicitly but 
    //make a call Outside of the Class to set Only and only if longString is there.

   public void setSecondHalf(){  
       if(this.longString == null)
            throw new Exception("Long String is Not Set.");
       this.secondHalf = this.computeSecondHalf(this.longString);
   }
//private method not Accessible outside of Class
   private String computeFirstHalf(final String value){ 
     //Your Logical Code. 
    }
 //private method not Accessible outside of Class
   private String computeSecondHalf(final String value){ 
        //Your Logical Code.
}