in Education by
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? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
A factory would work. You're essentially doing a form of Dependency Injection with a factory. When you instantiate or initialise your action, you would set the factory: public void setResponseFactory(ResponseBuilderFactory factory) { this.responseFactory = factory; } and in the factory, just return a new instance of JsonResponseBuilder when you need it. Make sure that you do not store your instance of JsonResponseBuilder as an instance variable in your action; it must remain local to the method you're using, or passed around as a method parameter. As for testing, it becomes easy to replace the factory with a mock factory that returns a mock ResponseBuilder. There are many libraries to do this, like Mockito or JMock. All of them work well with JUnit and TestNG. Edit: You'd need to have ResponseBuilderFactory as an interface, like: public interface ResponseBuilderFactory { public ResponseBuilder getResponseBuilder(); } When you do your testing, just create a class that returns a mock of your ResponseBuilder: @Test public void testMyAction() throws Exception { ResponseBuilderFactory mockFactory = new ResponseBuilderFactory() { public ResponseBuilder getResponseBuilder() { ResponseBuilder builder = context.mock(ResponseBuilder.class); // set up mock behaviour return builder; } } } So you're not injecting a mock factory, just a factory that returns mocks. Also, see Dependency Injection vs Factory Pattern. Edit 2: If you can somehow manage to re-code your JsonResponseBuilder so that it doesn't maintain state, then you could potentially avoid the whole factory mess all together and just use your original approach. Objects that do not maintain state are inherently thread-safe.

Related questions

0 votes
    I've Struts 1 action class (actions are singletons by design in struts 1) that needs to collect some ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I've Struts 1 action class (actions are singletons by design in struts 1) that needs to collect some ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    In a multi user multi-threaded environment, thread safety is important as one may erroneously gain access to another ... (1)OS commands (2)Race conditions (3)Session Integrity...
asked May 17, 2021 in Technology by JackTerrance
0 votes
    _____ platforms are used for safety and protection of information in the cloud. (a) Cloud workload protection platforms ( ... questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 5, 2021 in Education by JackTerrance
0 votes
    Which of the following protocols ensures conflict serializability and safety from deadlocks? (a) Two-phase ... topic in portion Concurrency Control of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    Identify the safety guards of rights Select the correct answer from above options...
asked Aug 2, 2022 in Education by JackTerrance
0 votes
    Select the correct answer from above options...
asked Aug 2, 2022 in Education by JackTerrance
0 votes
    I'm using SafetyNet API for checking if device is rooted or not and using the below helpful code but ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    What is the goal of psychological safety?...
asked Nov 28, 2020 in Technology by JackTerrance
0 votes
    What are the differences between stateful session bean and stateless session bean?...
asked Nov 7, 2020 in Technology by JackTerrance
0 votes
    Stateful Multilayer Inspection firewall cannot perform which of the following? (a) Filter network layer packets (b) Check ... questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 4, 2021 in Education by JackTerrance
0 votes
    We can also implement ____________ in Stateful Multilayer Inspection firewall. (a) external programs (b) algorithms (c) ... questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 4, 2021 in Education by JackTerrance
0 votes
    One advantage of Stateful Multilayer Inspection firewall is __________ (a) costlier but easy to understand (b) large to ... questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 4, 2021 in Education by JackTerrance
0 votes
    Stateful Multilayer firewalls are also called ____________ (a) first generation firewalls (b) second generation firewalls (c) ... and answers pdf, mcq on Cyber Security pdf,...
asked Nov 4, 2021 in Education by JackTerrance
0 votes
    What are the callback annotations for stateful bean?...
asked Nov 7, 2020 in Technology by JackTerrance
...