简体   繁体   English

在POJO Getter / Setter中添加静态方法逻辑

[英]Adding Static Method logic in POJO Getter/Setter

I have a Persistence Entity class (JPA) where I'm debating adding a static method call in the getter and setter to do some conversion logic, but for some reason it just doesn't seem like the proper thing to do. 我有一个持久性实体类(JPA),在这里我要在getter和setter中添加一个静态方法调用来执行一些转换逻辑,但是由于某种原因,这似乎不适合做。 Is there a best practice for getter/setter custom logic? 获取/设置自定义逻辑是否有最佳实践? Any downsides to this approach? 这种方法有什么缺点? The UI needs to enter the data with "/" but then another system interfacing with this one needs the "/" converted to "_". UI需要使用“ /”输入数据,但是与此接口连接的另一个系统需要将“ /”转换为“ _”。

@Entity
public class Sample {
 @Id   
 private id;
 @Column
 private String test;

 public String getTest() {
        return Util.convert(test);
 }
 public void setTest(String test) {
        this.test = Util.convert(test);
 }

}

    import org.apache.commons.lang3.StringUtils;
    public class Util{

    public static String convert(String enteredValue){
        return StringUtils.replace(enteredValue,"/","_"); //just example
    }
    }

No, your POJO should not be converting its data format just to become compatible with a system. 不,您的POJO不应只是为了与系统兼容而转换其数据格式。 This approach would break as soon as the POJO needs to work with disparate systems with different data format requirements. 一旦POJO需要与具有不同数据格式要求的不同系统一起使用,这种方法就会中断。

The POJO should save its state true to its user input. POJO应将其状态保存为真实的用户输入。 The difference in format should be handled by the system that's requesting the data. 格式上的差异应由请求数据的系统处理。 This way if the format changes down the line the responsibility lies with the requesting system not with the POJO. 这样,如果格式沿线更改,则责任在于请求系统,而不是POJO。 If the POJO directly tries to accommodate the new format it risks breaking any other system that might have also become dependent on it. 如果POJO直接尝试适应新格式,则可能会破坏其他可能也依赖该格式的系统。

If the client system requires the data in a particular format and does not wish to deal with it; 如果客户端系统需要特定格式的数据并且不希望处理该数据; it should then be handled by YourSystemFacade API that the client is using to interface with you. 然后,应使用客户端用来与您连接的YourSystemFacade API来处理它。 For example 例如

// at the other system
YourSystemFacade.getInstance().getTestSample();

// at your system
class YourSystemFacade {
    // ...
    public String getTestSample() {
        return Utils.convert(sample.getTest());
    }
}

Making static method call in getter is ok. 在getter中进行静态方法调用是可以的。 But you might face issues if another part of your application needs data in original format or some other format than this, lets say / replaced with . 但是,如果应用程序的另一部分需要原始格式或其他格式的数据,则可能会遇到问题,用/代替.

Alternative approach would be to have another getter method, say getTestWithHyphen or getTestWithDot . 另一种方法是使用另一个getter方法,例如getTestWithHyphengetTestWithDot

Or let your clients call Util method whenever they need data in different format. 或者让您的客户在需要不同格式的数据时调用Util方法。

Since the conversion logic is very simple you should put it in the setter, because all other methods in your class can then rely on the fact that the instance variable is always in a correct state. 由于转换逻辑非常简单,因此应将其放在setter中,因为类中的所有其他方法随后都可以依赖于实例变量始终处于正确状态这一事实。 Also JPA can access it then through field access. JPA也可以通过现场访问来访问它。

If the conversion would be more complex or time consuming I would place it in the getter and let all other methods only access it through the getter. 如果转换会更加复杂或耗时,我会将其放入getter中,并让所有其他方法仅通过getter进行访问。 But then you need one more field to track that state and you must configure JPA to use property access. 但是随后您需要再一个字段来跟踪该状态,并且必须将JPA配置为使用属性访问。

PS: rename setName or getTest if they belong to the same property PS:重命名setName或getTest如果它们属于同一属性

Well , there is nothing stop you to do this. 好吧,没有什么可以阻止您执行此操作。 You can do it. 你能行的。

I belive a POJO is also a simple java class, nothing special other than a proper pre defined structure. 我相信POJO也是一个简单的Java类,除了适当的预定义结构外没有什么特别的。

You can use it safely. 您可以安全地使用它。

Take some example from inbuilt methods too.Look at the getBytes() method. 也可以从内置方法中获取一些示例,看看getBytes()方法。

From Open JDK String class Open JDK String类

     public byte[] getBytes() {
     return StringCoding.encode(value, 0, value.length);
    }

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

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