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
+61-75
Original file line number
Diff line number
Diff line change
@@ -1,74 +1,49 @@
1
1
---
2
-
title: Context object
3
-
category: Creational
2
+
title: Context Object
3
+
category: Behavioral
4
4
language: en
5
5
tags:
6
-
- Data access
6
+
- Context
7
+
- Decoupling
8
+
- Encapsulation
7
9
---
8
10
9
-
## Name / classification
10
-
11
-
Context Object
12
-
13
11
## Also known as
14
12
15
-
Context, Encapsulate Context
13
+
* Context
14
+
* Context Encapsulation
15
+
* Context Holder
16
16
17
17
## Intent
18
18
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.
21
20
22
21
## Explanation
23
22
24
23
Real-world example
25
24
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.
29
26
30
27
In plain words
31
28
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.
@@ -83,10 +58,10 @@ public class ServiceContextFactory {
83
58
}
84
59
```
85
60
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.
* 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.
174
138
175
139
## 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.
* 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.
0 commit comments