Skip to content

Commit 20e804b

Browse files
committed
docs: improvements to the Context Object docs
1 parent 982526f commit 20e804b

File tree

2 files changed

+61
-76
lines changed

2 files changed

+61
-76
lines changed

context-object/README.md

+61-75
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,49 @@
11
---
2-
title: Context object
3-
category: Creational
2+
title: Context Object
3+
category: Behavioral
44
language: en
55
tags:
6-
- Data access
6+
- Context
7+
- Decoupling
8+
- Encapsulation
79
---
810

9-
## Name / classification
10-
11-
Context Object
12-
1311
## Also known as
1412

15-
Context, Encapsulate Context
13+
* Context
14+
* Context Encapsulation
15+
* Context Holder
1616

1717
## Intent
1818

19-
Decouple data from protocol-specific classes and store the scoped data in an object independent
20-
of the underlying protocol technology.
19+
Encapsulate the context (state and behaviors) relevant to the user or the request being processed in order to decouple application components from the complexities of the environment.
2120

2221
## Explanation
2322

2423
Real-world example
2524

26-
> This application has different layers labelled A, B and C with each extracting specific information
27-
> from a similar context for further use in the software. Passing down each pieces of information
28-
> individually would be inefficient, a method to efficiently store and pass information is needed.
25+
> This application has different layers labelled A, B and C with each extracting specific information from a similar context for further use in the software. Passing down each pieces of information individually would be inefficient, a method to efficiently store and pass information is needed.
2926
3027
In plain words
3128

32-
> Create an object and store the data there and pass this object to where it is needed.
29+
> Create an object to store the context data and pass it where needed.
3330
3431
[Core J2EE Patterns](http://corej2eepatterns.com/ContextObject.htm) says
3532

36-
> Use a Context Object to encapsulate state in a protocol-independent way to be shared throughout your application.
33+
> Use a Context Object to encapsulate state in a protocol-independent way to be shared throughout your application.
3734
3835
**Programmatic Example**
3936

40-
We define what data a service context object contains.
37+
Define the data that the service context object contains.
4138

4239
```Java
40+
@Getter
41+
@Setter
4342
public class ServiceContext {
4443

45-
String ACCOUNT_SERVICE, SESSION_SERVICE, SEARCH_SERVICE;
46-
47-
public void setACCOUNT_SERVICE(String ACCOUNT_SERVICE) {
48-
this.ACCOUNT_SERVICE = ACCOUNT_SERVICE;
49-
}
50-
51-
public void setSESSION_SERVICE(String SESSION_SERVICE) {
52-
this.SESSION_SERVICE = SESSION_SERVICE;
53-
}
54-
55-
public void setSEARCH_SERVICE(String SEARCH_SERVICE) {
56-
this.SEARCH_SERVICE = SEARCH_SERVICE;
57-
}
58-
59-
public String getACCOUNT_SERVICE() {
60-
return ACCOUNT_SERVICE;
61-
}
62-
63-
public String getSESSION_SERVICE() {
64-
return SESSION_SERVICE;
65-
}
66-
67-
public String getSEARCH_SERVICE() {
68-
return SEARCH_SERVICE;
69-
}
70-
71-
public String toString() { return ACCOUNT_SERVICE + " " + SESSION_SERVICE + " " + SEARCH_SERVICE;}
44+
String accountService;
45+
String sessionService;
46+
String searchService;
7247
}
7348
```
7449

@@ -83,10 +58,10 @@ public class ServiceContextFactory {
8358
}
8459
```
8560

86-
Instantiate the context object in the first layer and the adjoining layer upcalls the context in the current layer, which
87-
then further structures the object.
61+
Instantiate the context object in the first layer. The adjoining layer calls the context in the current layer, which then further structures the object.
8862

8963
```Java
64+
@Getter
9065
public class LayerA {
9166

9267
private static ServiceContext context;
@@ -95,15 +70,12 @@ public class LayerA {
9570
context = ServiceContextFactory.createContext();
9671
}
9772

98-
public static ServiceContext getContext() {
99-
return context;
100-
}
101-
10273
public void addAccountInfo(String accountService) {
10374
context.setACCOUNT_SERVICE(accountService);
10475
}
10576
}
10677

78+
@Getter
10779
public class LayerB {
10880

10981
private static ServiceContext context;
@@ -112,15 +84,12 @@ public class LayerB {
11284
this.context = layerA.getContext();
11385
}
11486

115-
public static ServiceContext getContext() {
116-
return context;
117-
}
118-
11987
public void addSessionInfo(String sessionService) {
12088
context.setSESSION_SERVICE(sessionService);
12189
}
12290
}
12391

92+
@Getter
12493
public class LayerC {
12594

12695
public static ServiceContext context;
@@ -129,54 +98,71 @@ public class LayerC {
12998
this.context = layerB.getContext();
13099
}
131100

132-
public static ServiceContext getContext() {
133-
return context;
134-
}
135-
136101
public void addSearchInfo(String searchService) {
137102
context.setSEARCH_SERVICE(searchService);
138103
}
139104
}
140105
```
106+
141107
Here is the context object and layers in action.
142108

143109
```Java
144-
var layerA = new LayerA();
145-
layerA.addAccountInfo(SERVICE);
146-
LOGGER.info("Context = {}",layerA.getContext());
147-
var layerB = new LayerB(layerA);
148-
layerB.addSessionInfo(SERVICE);
149-
LOGGER.info("Context = {}",layerB.getContext());
150-
var layerC = new LayerC(layerB);
151-
layerC.addSearchInfo(SERVICE);
152-
LOGGER.info("Context = {}",layerC.getContext());
110+
var layerA=new LayerA();
111+
layerA.addAccountInfo(SERVICE);
112+
LOGGER.info("Context = {}",layerA.getContext());
113+
var layerB=new LayerB(layerA);
114+
layerB.addSessionInfo(SERVICE);
115+
LOGGER.info("Context = {}",layerB.getContext());
116+
var layerC=new LayerC(layerB);
117+
layerC.addSearchInfo(SERVICE);
118+
LOGGER.info("Context = {}",layerC.getContext());
153119
```
154120

155121
Program output:
156122

157123
```Java
158-
Context = SERVICE null null
159-
Context = SERVICE SERVICE null
160-
Context = SERVICE SERVICE SERVICE
124+
Context=SERVICE null null
125+
Context=SERVICE SERVICE null
126+
Context=SERVICE SERVICE SERVICE
161127
```
162128

163129
## Class diagram
164130

165131
![alt text](./etc/context-object.png "Context object")
166132

167-
## Application
133+
## Applicability
168134

169-
Use the Context Object pattern for:
170-
171-
* Sharing information across different system layers.
172-
* Decoupling software data from protocol-specific contexts.
173-
* Exposing only the relevant API's within the context.
135+
* When there is a need to abstract and encapsulate context information from different parts of an application to avoid cluttering the business logic with environment-specific code.
136+
* In web applications, to encapsulate request-specific information and make it easily accessible throughout the application without passing it explicitly between functions or components.
137+
* In distributed systems, to encapsulate contextual information about the task being performed, user preferences, or security credentials, facilitating their propagation across different components and services.
174138

175139
## Known uses
140+
141+
* Web application frameworks often implement a Context Object to encapsulate HTTP request and response objects, session information, and other request-specific data.
142+
* Enterprise applications use Context Objects to manage and propagate transactional information, security credentials, and user-specific settings across different layers and services.
176143
* [Spring: ApplicationContext](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ApplicationContext.html)
177144
* [Oracle: SecurityContext](https://docs.oracle.com/javaee/7/api/javax/ws/rs/core/SecurityContext.html)
178145
* [Oracle: ServletContext](https://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html)
179146

147+
## Consequences
148+
149+
Benefits:
150+
151+
* Decoupling: Components and services are decoupled from the specificities of the execution environment, enhancing modularity and maintainability.
152+
* Centralization: Contextual information is centralized in one place, making it easier to manage, access, and debug.
153+
* Flexibility: The pattern allows for flexible and dynamic context management, which can adapt to changes in the environment or requirements.
154+
155+
Trade-offs:
156+
157+
* Overhead: Introducing a Context Object can add overhead in terms of performance, especially if not implemented efficiently.
158+
* Complexity: If the Context Object is not well-designed, it can become a bloated and complex monolith, difficult to manage and understand.
159+
160+
## Related Patterns
161+
162+
* [Singleton](https://java-design-patterns.com/patterns/singleton/): The Context Object is often implemented as a Singleton to ensure a global point of access.
163+
* [Strategy](https://java-design-patterns.com/patterns/strategy/): Context Objects can use Strategies to adapt their behavior based on the context they encapsulate.
164+
* [Decorator](https://java-design-patterns.com/patterns/decorator/): Can be used to dynamically add responsibilities to the Context Object.
165+
180166
## Credits
181167

182168
* [Core J2EE Design Patterns](https://amzn.to/3IhcY9w)

context-object/src/main/java/com/iluwatar/context/object/ServiceContext.java

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import lombok.Getter;
2828
import lombok.Setter;
29-
import lombok.ToString;
3029

3130
/**
3231
* Where context objects are defined.

0 commit comments

Comments
 (0)