Skip to content

Commit a4f3f8a

Browse files
committed
docs: context object
1 parent f6e1518 commit a4f3f8a

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

context-object/README.md

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ tags:
66
- Context
77
- Decoupling
88
- Encapsulation
9+
- Session management
910
---
1011

1112
## Also known as
1213

1314
* Context
1415
* Context Encapsulation
1516
* Context Holder
17+
* Encapsulate Context
1618

1719
## Intent
1820

@@ -22,7 +24,7 @@ Encapsulate the context (state and behaviors) relevant to the user or the reques
2224

2325
Real-world example
2426

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.
27+
> Imagine a busy airport where multiple services need to access and share passenger information throughout their journey. Instead of each service requesting and passing passenger details separately, the airport uses a "Passenger Context Object." This context object holds all relevant passenger information, such as identity, flight details, and preferences. Various services like check-in, security, boarding, and customer service access this context object to get or update passenger data as needed. This approach ensures consistent and efficient information handling without tightly coupling the services, similar to how the Context Object design pattern works in software.
2628
2729
In plain words
2830

@@ -34,6 +36,8 @@ In plain words
3436
3537
**Programmatic Example**
3638

39+
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.
40+
3741
Define the data that the service context object contains.
3842

3943
```Java
@@ -107,28 +111,48 @@ public class LayerC {
107111
Here is the context object and layers in action.
108112

109113
```Java
110-
var layerA=new LayerA();
114+
@Slf4j
115+
public class App {
116+
117+
private static final String SERVICE = "SERVICE";
118+
119+
public static void main(String[] args) {
120+
//Initiate first layer and add service information into context
121+
var layerA = new LayerA();
111122
layerA.addAccountInfo(SERVICE);
112-
LOGGER.info("Context = {}",layerA.getContext());
113-
var layerB=new LayerB(layerA);
123+
124+
logContext(layerA.getContext());
125+
126+
//Initiate second layer and preserving information retrieved in first layer through passing context object
127+
var layerB = new LayerB(layerA);
114128
layerB.addSessionInfo(SERVICE);
115-
LOGGER.info("Context = {}",layerB.getContext());
116-
var layerC=new LayerC(layerB);
129+
130+
logContext(layerB.getContext());
131+
132+
//Initiate third layer and preserving information retrieved in first and second layer through passing context object
133+
var layerC = new LayerC(layerB);
117134
layerC.addSearchInfo(SERVICE);
118-
LOGGER.info("Context = {}",layerC.getContext());
135+
136+
logContext(layerC.getContext());
137+
}
138+
139+
private static void logContext(ServiceContext context) {
140+
LOGGER.info("Context = {}", context);
141+
}
142+
}
119143
```
120144

121145
Program output:
122146

123-
```Java
124-
Context=SERVICE null null
125-
Context=SERVICE SERVICE null
126-
Context=SERVICE SERVICE SERVICE
147+
```
148+
08:15:32.134 [main] INFO com.iluwatar.context.object.App -- Context = com.iluwatar.context.object.ServiceContext@5577140b
149+
08:15:32.136 [main] INFO com.iluwatar.context.object.App -- Context = com.iluwatar.context.object.ServiceContext@5577140b
150+
08:15:32.137 [main] INFO com.iluwatar.context.object.App -- Context = com.iluwatar.context.object.ServiceContext@5577140b
127151
```
128152

129153
## Class diagram
130154

131-
![alt text](./etc/context-object.png "Context object")
155+
![Context Object](./etc/context-object.png "Context object")
132156

133157
## Applicability
134158

@@ -166,6 +190,6 @@ Trade-offs:
166190
## Credits
167191

168192
* [Core J2EE Design Patterns](https://amzn.to/3IhcY9w)
169-
* [Core J2EE Design Patterns website - Context Object](http://corej2eepatterns.com/ContextObject.htm)
170-
* [Allan Kelly - The Encapsulate Context Pattern](https://accu.org/journals/overload/12/63/kelly_246/)
171-
* [Arvid S. Krishna et al. - Context Object](https://www.dre.vanderbilt.edu/~schmidt/PDF/Context-Object-Pattern.pdf)
193+
* [Context Object (Core J2EE Patterns)](http://corej2eepatterns.com/ContextObject.htm)
194+
* [The Encapsulate Context Pattern (Accu)](https://accu.org/journals/overload/12/63/kelly_246/)
195+
* [Context Object - A Design Pattern for Efficient Information Sharing across Multiple System Layers (Arvid S. Krishna et al.)](https://www.dre.vanderbilt.edu/~schmidt/PDF/Context-Object-Pattern.pdf)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,5 @@ public static void main(String[] args) {
6666

6767
private static void logContext(ServiceContext context) {
6868
LOGGER.info("Context = {}", context);
69-
7069
}
7170
}

0 commit comments

Comments
 (0)