OO design patterns to use for validation

前端 未结 4 1733
感情败类
感情败类 2021-01-31 21:38

I am in the process of writing some validation code based on these assumptions:

  • Validation code must be in an external class
    • i.e. no data class contains
4条回答
  •  不要未来只要你来
    2021-01-31 21:58

    I had this same problem and I found the visitor pattern to be really effective at decoupling the validation logic from the data object. You'll need to instrument your data class hierarchy with accept( visitor ) methods, but if you're building everything that's simple enough. Even if you're using a third-party hierarchy without visitor support, you can create wrappers that provide the accept traversal tree and that's pretty close to having the method inside the class.

    To perform the different validation you implement a different validator class and pass it to the accept method on the root object. I was also able to create other utility visitors around the model which allowed me to create a generator visitor that filled in all the fields with sample/random data. I went a little visitor crazy on it because I was so excited. You can probably tell I'm still excited about it, especially having the change to tell someone else about it.

提交回复
热议问题