Context Object Design Pattern – Core J2EE Patterns

Context object Design Pattern is one of the J2EE design patterns used to store references and pointers to configuration information and services needed by other objects. It allows different objects to interact with each other without every object needing to maintain all the information.

Context Object Design Pattern

The structure of the context object is such that it has a client, initial context, service locator, and service factory and business service. Each of the components has different tasks to perform and different responsibilities to cater. A client is an object which requires access to the business objects. It is basically, a client of the service locator. The service locator provides an interface to the clients. The clients can reuse the service locator. In the lookup and creation process, the initial context is the starting point. Service providers are responsible for providing the context objects. This might vary depending on the type of business object that is provided by the lookup and creation service of the Service locator. A certain object is responsible for providing life cycle management for the business service objects, Service Factory represents this object. The business service is basically a task that is required to be carried out by the service that the client is pursuing to access.

Spring 5 Design Pattern Book

You could purchase my Spring 5 book that is with title name “Spring 5 Design Patterns“. This book is available on the Amazon and Packt publisher website. Learn various design patterns and best practices in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- “AUTHDIS40“.
Spring-5-Design-Pattern

Context object improve the maintainability and reusability of a program as with shared resources and reference points, all saved in context objects, we can easily decipher which part of the program can be reused for another application and what needs changing. It also makes testing a great deal easier as for every error we can refer to context object and it will point us in the right direction. Using context objects allows us to change interface without much trouble because the program is divided into small parts and we can make changes without it affecting the whole program and whatever it does affect can be reached and corrected quite easily.

Class Diagram for Context Objects Pattern

Let’s see the following class diagram for context objects pattern.

Context Objects Design Pattern
ContextFactory

Client uses this factory to produce Context Object.

Context Object

It used by client to share resource in the program.

Benefits for Context Objects

  • Improves reusability and maintainability
  • Improves testability

For Example

There are following example for this pattern

  • ApplicationContext in the Spring Framework
  • SecurityContext in the Security API
  • ServletContext in the Servlet API
Previous
Next