title: stateful dependency and thread safety
body: <p>I've Struts 1 action class (actions are singletons by design in struts 1) that needs to collect some data and then combine them all into single response.
  I'd like to extract all the response generation logic into separate class called</p>
  
  <pre><code>ResponseBuilder
  </code></pre>
  
  <p>Normally I'd put ResponseBuilder as a field and have setter for it (e.g. for testing).
  My response builder looks as follows</p>
  
  <pre><code>class JsonResponseBuilder implements ResponseBuilder {
      public void addElement(String key, Object value) {
          ...
      }
  
      public String buildResponse() {
          // build response from data collected
      }
  }
  </code></pre>
  
  <p>With such implementation I can't do it due to thread safety issues here.</p>
  
  <p>How can I change this design to be ok? Is Factory pattern here applicable?
  I mean is using</p>
  
  <pre><code>ResponseBuilderFactory
  </code></pre>
  
  <p>as a dependency and calling it that way:</p>
  
  <pre><code>ResponseBuilder builder = factory.getBuilder();
  builder.addElement(...);
  ...
  String response = builder.build();
  </code></pre>
  
  <p>is ok from design and testability point of view? 
  If it is ok. how to write test code for this? Mock factory? Mock builder?</p>