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: gateway/README.md
+43-22
Original file line number
Diff line number
Diff line change
@@ -1,33 +1,35 @@
1
1
---
2
2
title: Gateway
3
-
category: Structural
3
+
category: Integration
4
4
language: en
5
5
tag:
6
-
- Decoupling
7
-
6
+
- API design
7
+
- Data access
8
+
- Decoupling
9
+
- Enterprise patterns
8
10
---
9
11
12
+
## Also known as
13
+
14
+
* Service Gateway
15
+
10
16
## Intent
11
17
12
-
Provide a interface to access a set of external systems or functionalities. Gateway provides a simple uniform view of
13
-
external resources to the internals of an application.
18
+
The Gateway design pattern aims to encapsulate the interaction with a remote service or external system, providing a simpler and more unified API to the rest of the application.
14
19
15
20
## Explanation
16
21
17
22
Real-world example
18
23
19
-
> Gateway acts like a real front gate of a certain city. The people inside the city are called
20
-
> internal system, and different outside cities are called external services. The gateway is here
21
-
> to provide access for internal system to different external services.
24
+
> Gateway acts like a real front gate of a certain city. The people inside the city are called internal system, and different outside cities are called external services. The gateway is here to provide access for internal system to different external services.
22
25
23
26
In plain words
24
27
25
28
> Gateway can provide an interface which lets internal system to utilize external service.
26
29
27
30
Wikipedia says
28
31
29
-
> A server that acts as an API front-end, receives API requests, enforces throttling and security
30
-
> policies, passes requests to the back-end service and then passes the response back to the requester.
32
+
> A server that acts as an API front-end, receives API requests, enforces throttling and security policies, passes requests to the back-end service and then passes the response back to the requester.
31
33
32
34
**Programmatic Example**
33
35
@@ -117,33 +119,52 @@ interface Gateway {
117
119
Program output:
118
120
119
121
```java
120
-
ExecutingServiceA
121
-
ExecutingServiceB
122
-
ExecutingServiceC
122
+
ExecutingServiceA
123
+
ExecutingServiceB
124
+
ExecutingServiceC
123
125
```
124
126
125
127
## Class diagram
126
128
127
-

129
+

128
130
129
131
## Applicability
130
132
131
-
Use the Gateway pattern
132
-
133
-
* To access an aggregate object's contents without exposing its internal representation.
134
-
* To integration with multiple external services or APIs.
135
-
* To provide a uniform interface for traversing different aggregate structures.
133
+
Use the Gateway pattern when you need to integrate with remote services or APIs, and you want to minimize the coupling between your application and external systems. It is particularly useful in microservices architectures where different services need to communicate through well-defined APIs.
136
134
137
135
## Tutorials
138
136
139
137
*[Pattern: API Gateway / Backends for Frontends](https://microservices.io/patterns/apigateway.html)
*[10 most common use cases of an API Gateway](https://apisix.apache.org/blog/2022/10/27/ten-use-cases-api-gateway/)
142
+
* API Gateways in Microservices: Acts as an intermediary that processes incoming requests from clients, directing them to appropriate services within a microservices architecture.
143
+
* Database Gateways: Provides a unified interface to access data from various database systems, hiding the specifics of database querying and data retrieval.
144
+
145
+
## Consequences
146
+
147
+
Benefits:
148
+
149
+
* Reduces complexity by hiding the details of the external API or service behind a simpler interface.
150
+
* Promotes loose coupling between the application and its dependencies on external systems.
151
+
* Makes the system easier to test and maintain.
152
+
153
+
Trade-offs:
154
+
155
+
* Introduces an additional layer that could potentially impact performance.
156
+
* Requires careful design to avoid creating a monolithic gateway that becomes a bottleneck.
157
+
158
+
## Related Patterns
159
+
160
+
*[Facade](https://java-design-patterns.com/patterns/facade/): Similar to Gateway in abstracting complex subsystems, but Gateway specifically targets external or remote interfaces.
161
+
*[Adapter](https://java-design-patterns.com/patterns/adapter/): While both patterns provide a different interface to a subsystem, Gateway focuses more on networked data sources and services.
162
+
*[Proxy](https://java-design-patterns.com/patterns/proxy/): Often used together, as both can control and manage access to another object, but Gateway specifically deals with external services.
163
+
*[API Gateway](https://java-design-patterns.com/patterns/api-gateway/): Often considered a specialization of the Gateway pattern, it specifically manages API requests and routes them to the appropriate services within a backend system.
*[What is the difference between Facade and Gateway design patterns?](https://stackoverflow.com/questions/4422211/what-is-the-difference-between-facade-and-gateway-design-patterns)
167
+
*[Gateway - Martin Fowler](https://martinfowler.com/articles/gateway-pattern.html)
168
+
*[What is the difference between Facade and Gateway design patterns? - Stack Overflow](https://stackoverflow.com/questions/4422211/what-is-the-difference-between-facade-and-gateway-design-patterns)
169
+
*[Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions](https://amzn.to/3WcFVui)
170
+
*[Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
Use Guarded Suspension pattern when the developer knows that the method execution will be blocked for a finite period of time
18
+
## Intent
19
+
20
+
The Guarded Suspension pattern manages operations that require both a lock and a condition to proceed, allowing a thread to wait for an appropriate condition while being efficient with resource use.
17
21
18
22
## Explanation
19
23
20
24
Real world example
21
25
22
-
> When we reserve a dining room online and arrive to find it unready, the manager has it cleaned while we wait.
23
-
> Once ready, we're escorted to the room. This process exemplifies the Guarded Suspension pattern.
26
+
> When we book a dining room online and arrive to find it not yet prepared, the manager arranges for it to be cleaned while we wait. Once the room is ready, we are then escorted to it. This scenario illustrates the Guarded Suspension pattern, where our access to the room is contingent upon a specific condition being met—namely, the room being cleaned.
24
27
25
28
In plain words
26
29
27
30
> Guarded Suspension pattern is used when one thread waits for the result of another thread's execution.
28
31
29
32
Wikipedia says
30
33
31
-
> In concurrent programming, Guarded Suspension manages operations requiring a lock
32
-
> and a precondition, delaying execution until the precondition is met.
34
+
> In concurrent programming, Guarded Suspension manages operations requiring a lock and a precondition, delaying execution until the precondition is met.
33
35
34
36
**Programmatic Example**
35
37
36
-
The `GuardedQueue` class encapsulates a queue, and provides two synchronized methods, `get` and `put`.
37
-
The `get` method waits if the queue is empty, and the `put` method adds an item to the queue and notifies waiting threads:
38
+
The `GuardedQueue` class encapsulates a queue, and provides two synchronized methods, `get` and `put`. The `get` method waits if the queue is empty, and the `put` method adds an item to the queue and notifies waiting threads:
This pattern is used in scenarios where a thread needs to wait for certain conditions to be met before it can proceed, ensuring that resources are utilized only when necessary and reducing the overhead of busy waiting.
111
+
112
+
## Known Uses
113
+
114
+
* Network servers waiting for client requests.
115
+
* Producer-consumer scenarios where the consumer must wait for the producer to provide data.
116
+
* Event-driven applications where actions are triggered only after specific events have occurred.
117
+
118
+
## Consequences
119
+
120
+
Benefits:
121
+
122
+
* Reduces CPU consumption by preventing busy waiting.
123
+
* Increases system responsiveness by synchronizing actions to the availability of necessary conditions or resources.
124
+
125
+
Trade-offs:
126
+
127
+
* Complexity in implementation, especially when multiple conditions need to be managed.
128
+
* Potential for deadlocks if not carefully managed.
129
+
130
+
## Related Patterns
131
+
132
+
* Monitor Object: Both patterns manage the synchronization of threads based on conditions. Guarded Suspension specifically deals with suspending threads until conditions are met, while Monitor Object encapsulates condition and mutual exclusion handling.
133
+
* Producer-Consumer: Often implemented using Guarded Suspension to handle waiting consumers and producers efficiently.
134
+
* Balking: Similar to Guarded Suspension, Balking is used when a thread checks a condition and only proceeds if the condition is favorable; if not, it immediately returns or bails out. This pattern complements Guarded Suspension by managing actions based on immediate condition checks without waiting.
89
135
90
-
## Related patterns
136
+
## Credits
91
137
92
-
* Balking
138
+
*[Java Concurrency in Practice](https://amzn.to/3JxnXek)
139
+
*[Pattern-Oriented Software Architecture Volume 2: Patterns for Concurrent and Networked Objects](https://amzn.to/49Ke1c9)
0 commit comments