Real world examples
- ApplicationContext is the central interface within a Spring application for providing configuration information to the application.
- SecurityContext is used to store the details of the currently authenticated user and which can further be accessed through the application.
- ServletContext is used to share configuration information with all the servlets.
This pattern is divided into a number of sections for simplicity like the problem, forces, solution, structure, implementation, applicability etc.
| Table of contents |
|---|
| Problem |
| Forces |
| Solution |
| Explanation |
| Structure - Class Diagram, Sequence Diagram |
| Participants and Responsibilities |
| Implementation |
| Consequences |
| Applicability |
| Real world examples |
| References |
Problem
Forces
- You have components and services that need access to system information.
- You want to decouple application components and services from the protocol specifics of system information.
- You want to expose only the relevant APIs within a context.
Solution
Structure
Class Diagram
Sequence Diagram
Participants and Responsibilities
Implementation
The ContextFactory creates a RequestContext(ContextObject) and transfers state into it from the HttpServletRequest. The data in the RequestContext typically goes through an initial form-level validation at this point; for example, a check for empty fields or a check for a credit card number having the correct number of digits.
As business processing is performed, the ContextObject state will typically go through the second round of validation that is business-related, such as whether a value is within an appropriate range. The relevant request state is transferred into a standard Map implementation, which is passed around.
public class FrontController extends HttpServlet {
...
private void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
java.io.IOException {
// create RequestContext object using Map Strategy
Map requestContextMap = new HashMap(request.getParameterMap());
Dispatcher dispatcher = new Dispatcher(request, response);
requestContextMap.put("dispatcher", dispatcher);
// Create ApplicationController instance
ApplicationController applicationController =
new ApplicationControllerImpl();
// Request processing
ResponseContext responseContext =
applicationController.handleRequest(requestContextMap);
// Response processing
applicationController.handleResponse(requestContextMap,
responseContext);
}
...
}
Applicability
In layered Architecture, if we want to share system information across different System layers then use this design patternReferences
Related Presentation Tier Posts
- Intercepting Filter Design Pattern in Java
- Front Controller Design Pattern in Java
- Application Controller Design Pattern in Java
- View Helper Design Pattern in Java
- Composite View Design Pattern in Java
- Context Object Design Pattern in Java
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner


Comments
Post a Comment
Leave Comment