I need to capture user input using a form. Each field within the form will undergo validation. The field will be either valid or invalid. Depending on the user input, certain parts of the form may be enabled, disabled, filtered or otherwise modified.
I am considering the state pattern to model the state transitions through the form. Each state will affect how the form is displayed, filtered etc. However, my understanding of the state pattern is that it would require a very large number of states to represent my form.
For example; if I have 10 fields that can be valid or invalid that is:
10P2 = 90 permutations.
That is an enormous number of states to represent in code, and I have grossly simplified the problem.
Questions:
Am I misunderstanding how to implement the state pattern for my problem?
I think you've understood it correctly.
If not, is the state pattern the wrong solution to my problem?
Yes. The State pattern is a good solution when there are a limited number of states (conditions). This is not true in your case.
If yes to the last question, what is a good general solution?
I would recommend using the Specification pattern. You can have any number of rules attached to your input fields. The rules can determine if the field should be enabled or disabled, visible or hidden. Also worth noting is that the rules can be easily unit tested separately.