I am new to the pattern way of coding and would like to start implementing patterns in my code. I have a webservice that has multiple operations and receives different xml inputs for each operation(having its own schema). I would like to implement a pattern to validate the input provided.
My design:
Have an interface IValidate
implement the interface to EntityAValidate, EntityBValidate etc..,
each implemented validate method will check for validity of the xml and also do the individual node validation such as the string cannot have special characters etc..,
My questions :
Can we use any other design? please let me know the pattern name
How can I reuse some common validations like numeric check, date check across operations
How can the selection of corresponding validator be done automatically?
Based on this statement “I have a webservice that has multiple operations and receives different xml inputs for each operation”, the Command Pattern would probably be a good fit. It would apply more generally though, it need not be specific to validation.
The Command Pattern encapsulates each operation as an object, each derived from a common base class (call it class Operation) which provides an ‘execute’ method. From each incoming xml operation, you would instantiate the corresponding Operation sub-class, passing them to some operation processing entity who calls execute.
You could build your validation into this, adding a “validate” method to your Operation base class. The processor could then “validate” each Operation before “execute”ing it.
In general, I agree with the commenters on your post. You should study the design patterns themselves, then they’ll jump out at you when your solving problems. Don’t pick a pattern and try to cram it into an existing design.