简体   繁体   中英

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

I would recommend to avoid this by the following reasons:

  1. You lose separation of concern between model and model validation logic. 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. This validation will be executed by spring bean that implements some spec. This way you don't have above issues but still you can keep validation rule (without implementation details) on the model class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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