Tuesday, October 25, 2016

Simplifying Validations Using the Strategy(Enum) Pattern

In one of my previous projects we had to perform series of complex validations to prepare some input data. Below will explain the scenario.
  • Call a service and perform some complex validations based on this service response to prepare one of the input values for using it in another service call. 
  • You may need to check for multiple inputs with separate complex logic, but at the end you would select only a particular input for another service call.
  • You decide the number of inputs. The validation will be performed for all these inputs. You may add or remove number of inputs at any point of time so, we need a flexible way to specify that.
Generally, we use multiple if...else blocks, but, it would lead to readability, maintainability issues. Hence, we came up with an approach of using strategy pattern via Enum. 

The approach is explained with working sample code and posted in DZone site. Hope this will be useful.

No comments:

Post a Comment

Do you know - Series - 4: Boxing and Unboxing - Integer assignment and comparison

We know about boxing and unboxing in Java. It is about automatic conversion of primitive type value into its Wrapper Object equivalent type...