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
ResponseBuilder
Normally I'd put ResponseBuilder as a field and have setter for it (e.g. for testing). My response builder looks as follows
class JsonResponseBuilder implements ResponseBuilder {
public void addElement(String key, Object value) {
...
}
public String buildResponse() {
// build response from data collected
}
}
With such implementation I can't do it due to thread safety issues here.
How can I change this design to be ok? Is Factory pattern here applicable? I mean is using
ResponseBuilderFactory
as a dependency and calling it that way:
ResponseBuilder builder = factory.getBuilder();
builder.addElement(...);
...
String response = builder.build();
is ok from design and testability point of view? If it is ok. how to write test code for this? Mock factory? Mock builder?