title: Design pattern(Java): Multiple classes, same field set
body: <p>Consider a Java program that includes 3 classes: <code>Entry</code>, <code>Validator</code>, and <code>Context</code>. <code>Validator</code> has a method <code>boolean isValid(Entry entry, Context context)</code>, which will determine the validity of an entry object based on a context. Validator can either be set to check each <code>entry.field</code> against a preset specific value (i.e. <code>MatchingMode.SPECIFIC</code>) or against the value of the corresponding field from Context (i.e. <code>MatchingMode.CONTEXT</code>). <code>MatchingMode</code> is a nested enum within <code>Validator</code> class. Consider the following pseudo code for further elaboration:</p>
  
  <pre><code>Validator::boolean isValid(Entry entry, Context context)
  {
      boolean valid = true;
      for(each field):
          if(this.field.matchingMode == MatchingMode.SPECIFIC)
              valid &amp;= (entry.field.equals(this.field));
          else if(this.field.matchingMode == MatchingMode.CONTEXT)
              valid &amp;= (entry.field.euqals(context.field));
      return valid;
  }
  </code></pre>
  
  <p><strong>Notes:</strong></p>
  
  <ul>
  <li>All validator objects are created in the beginning of the code's execution and will persist until its termination.</li>
  <li>Entries are user's input to the program and will continually be fed to the code.</li>
  <li>Context objects will be created based on the entries.</li>
  <li>Fields have different types.</li>
  </ul>
  
  <p>Given the above validation use case, how do you suggest implementing this code in a DRY and type-safe manner?</p>
  
  <p><strong>Edit 1:</strong></p>
  
  <p>The logic to validate each and every field is the same. Take a look at the current live code(simplified to make sense as an example):</p>
  
  <pre><code>class Validator
  {
      int field1;
      MatchingMode field1MM;
  
      String field2;
      MatchingMode field2MM;
  
      // and a few more filed/MMs
  
      boolean isValid(Entry entry, Context context)
      {
          boolean valid = true;
          if(this.field1MM == MatchingMode.SPECIFIC)
              valid &amp;= (entry.field1.equals(this.field1));
          else if(this.field1MM == MatchingMode.CONTEXT)
              valid &amp;= (entry.field1.euqals(context.field1));
  
          if(this.field2MM == MatchingMode.SPECIFIC)
              valid &amp;= (entry.field2.equals(this.field2));
          else if(this.field2MM == MatchingMode.CONTEXT)
              valid &amp;= (entry.field2.euqals(context.field2));
  
          // copy and paste for each field/MM
  
          return valid;
      }
  }
  </code></pre>
  
  <p>The maintenance overhead to only add/remove 1 field just does not feel right:</p>
  
  <ul>
  <li>Add/remove the field from 3 classes</li>
  <li>Add/remove fieldMM field to the validator</li>
  <li>Copy/delete the 4-line-long validation for the field</li>
  </ul>
  
  <p>Is there really no better way of doing this kind of validation?</p>