Design Pattern

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“.

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.


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
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago