简体   繁体   English

java static factory methods in Spring Model class

[英]java static factory methods in Spring Model class

Is it good to use java static factory method in Spring Model Class for validate field with some kind of equation Is it good to use java static factory method in Spring Model Class for validate field with some kind of equation

I would recommend to avoid this by the following reasons:我建议避免这种情况,原因如下:

  1. You lose separation of concern between model and model validation logic.您失去了 model 和 model 验证逻辑之间的关注点分离。 Especially if validation touches only a few fields.特别是如果验证只涉及几个字段。 Adding/modifying fields and modifying validation logic at the same time should not cause the code change conflict.同时添加/修改字段和修改验证逻辑不应导致代码更改冲突。
  2. You make it much harder for testing (static variables are evil for testing).您使测试变得更加困难(静态变量对测试不利)。
  3. You violate "inversion of control" principle.你违反了“控制反转”原则。 You will have bad time when you need to replace validation implementation in a flexible way.当您需要以灵活的方式替换验证实现时,您将遇到困难。

What you can probably do is adding custom validation annotation to the model.您可能会做的是向 model 添加自定义验证注释 This validation will be executed by spring bean that implements some spec.此验证将由实现某些规范的 spring bean 执行。 This way you don't have above issues but still you can keep validation rule (without implementation details) on the model class.这样您就没有上述问题,但您仍然可以在 model class 上保留验证规则(没有实现细节)。

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

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