You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: context-object/README.md
+39-15Lines changed: 39 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -6,13 +6,15 @@ tags:
6
6
- Context
7
7
- Decoupling
8
8
- Encapsulation
9
+
- Session management
9
10
---
10
11
11
12
## Also known as
12
13
13
14
* Context
14
15
* Context Encapsulation
15
16
* Context Holder
17
+
* Encapsulate Context
16
18
17
19
## Intent
18
20
@@ -22,7 +24,7 @@ Encapsulate the context (state and behaviors) relevant to the user or the reques
22
24
23
25
Real-world example
24
26
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.
26
28
27
29
In plain words
28
30
@@ -34,6 +36,8 @@ In plain words
34
36
35
37
**Programmatic Example**
36
38
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
+
37
41
Define the data that the service context object contains.
38
42
39
43
```Java
@@ -107,28 +111,48 @@ public class LayerC {
107
111
Here is the context object and layers in action.
108
112
109
113
```Java
110
-
var layerA=newLayerA();
114
+
@Slf4j
115
+
publicclassApp {
116
+
117
+
privatestaticfinalStringSERVICE="SERVICE";
118
+
119
+
publicstaticvoidmain(String[] args) {
120
+
//Initiate first layer and add service information into context
121
+
var layerA =newLayerA();
111
122
layerA.addAccountInfo(SERVICE);
112
-
LOGGER.info("Context = {}",layerA.getContext());
113
-
var layerB=newLayerB(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 =newLayerB(layerA);
114
128
layerB.addSessionInfo(SERVICE);
115
-
LOGGER.info("Context = {}",layerB.getContext());
116
-
var layerC=newLayerC(layerB);
129
+
130
+
logContext(layerB.getContext());
131
+
132
+
//Initiate third layer and preserving information retrieved in first and second layer through passing context object
*[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)
0 commit comments