title: Design pattern suggestion/implementation using java 8
body: <p>I have to process requests in a series of steps.</p>
  
  <p><strong>For example:</strong> if request 1 comes, then I have to apply step1, step2, step3 and finally step4 which will persist the processed request into the database.  </p>
  
  <p>I thought of implementing the <strong>Template design</strong> pattern, as it solves a similar problem.  </p>
  
  <p>When I started implementing the design pattern, I suddenly found it hard to implement, because of the logical complexity.  </p>
  
  <p>Let me explain the requirement:  </p>
  
  <p><strong>Request -> Controller -> run()</strong>  </p>
  
  <p>The <strong>request</strong> will contain <code>List&lt;objects&gt;</code>.<br>
  Inside <strong>the run</strong> method, a series of operations will be fired.  </p>
  
  <pre><code>request.parallelStream()
      .map(step1 -&gt; executeStep1.apply(step1))
      .map(step2 -&gt; executeStep2.apply(step2, action))
      .map(step3 -&gt; executeStep3.apply(step3, rules))
      .map(step4 -&gt; executeStep4.apply(step4))
      .collect(Collectors.toList());
  
      Function&lt;String, List&lt;PersonDto&gt;&gt; executeStep1= person-&gt; {
          return metaData.getMetaDataPerson(person);
      };
  
      BiFunction&lt;List&lt;PersonDto&gt;, String, TransTemplateDTO&gt; executeStep2= (metaData, action) -&gt; {
          return temlate.createTemplate(metaData, action);
      };
  </code></pre>
  
  <p>Now, as we can see I am passing the first element of <code>request</code> as an input to step1(), and then processing it and further passing the output as an input to subsequent steps.  </p>
  
  <ul>
  <li><p><strong>Problem 1</strong>:<br>
  Till this point, there was no problem. But suddenly the requirement changed and now I have to add rules logic in the step3 i.e.  <code>executeStep3.apply(step3)</code>.  </p>
  
  <p>step3 is taking 2 parameters, one is the output of step2 and second is List rules.<br>
  Step3 should apply all the rules and return the results equal to the rules.<br>
  For ex. If there are 3 rules, then step3 should return a List of 3 objects.<br>
  Let's assume step3 received List of <code>PersonDto</code> with 10 items and List of rules of 3 items, then step3 should return 10*3 = 30 records.<br>
  Also for each person rules will vary as per the command.  </p></li>
  <li><p><strong>Problem 2</strong>:<br>
  In step 3, I need the request object, so that I can make use of values.
  Something like this:<br>
      <code>.map(step3 -&gt; executeStep3.apply(step3, rules, request))</code>  </p></li>
  </ul>
  
  <p>What design patterns help out here, and how?</p>