mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-16 14:59:21 +00:00
docs: update gateway
This commit is contained in:
+43
-22
@@ -1,24 +1,27 @@
|
||||
---
|
||||
title: Gateway
|
||||
category: Structural
|
||||
category: Integration
|
||||
language: en
|
||||
tag:
|
||||
- Decoupling
|
||||
|
||||
- API design
|
||||
- Data access
|
||||
- Decoupling
|
||||
- Enterprise patterns
|
||||
---
|
||||
|
||||
## Also known as
|
||||
|
||||
* Service Gateway
|
||||
|
||||
## Intent
|
||||
|
||||
Provide a interface to access a set of external systems or functionalities. Gateway provides a simple uniform view of
|
||||
external resources to the internals of an application.
|
||||
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.
|
||||
|
||||
## Explanation
|
||||
|
||||
Real-world example
|
||||
|
||||
> 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.
|
||||
> 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.
|
||||
|
||||
In plain words
|
||||
|
||||
@@ -26,8 +29,7 @@ In plain words
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> 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.
|
||||
> 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.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
@@ -117,22 +119,18 @@ interface Gateway {
|
||||
Program output:
|
||||
|
||||
```java
|
||||
Executing Service A
|
||||
Executing Service B
|
||||
Executing Service C
|
||||
Executing Service A
|
||||
Executing Service B
|
||||
Executing Service C
|
||||
```
|
||||
|
||||
## Class diagram
|
||||
|
||||

|
||||

|
||||
|
||||
## Applicability
|
||||
|
||||
Use the Gateway pattern
|
||||
|
||||
* To access an aggregate object's contents without exposing its internal representation.
|
||||
* To integration with multiple external services or APIs.
|
||||
* To provide a uniform interface for traversing different aggregate structures.
|
||||
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.
|
||||
|
||||
## Tutorials
|
||||
|
||||
@@ -140,10 +138,33 @@ Use the Gateway pattern
|
||||
|
||||
## Known uses
|
||||
|
||||
* [API Gateway](https://java-design-patterns.com/patterns/api-gateway/)
|
||||
* [10 most common use cases of an API Gateway](https://apisix.apache.org/blog/2022/10/27/ten-use-cases-api-gateway/)
|
||||
* API Gateways in Microservices: Acts as an intermediary that processes incoming requests from clients, directing them to appropriate services within a microservices architecture.
|
||||
* Database Gateways: Provides a unified interface to access data from various database systems, hiding the specifics of database querying and data retrieval.
|
||||
|
||||
## Consequences
|
||||
|
||||
Benefits:
|
||||
|
||||
* Reduces complexity by hiding the details of the external API or service behind a simpler interface.
|
||||
* Promotes loose coupling between the application and its dependencies on external systems.
|
||||
* Makes the system easier to test and maintain.
|
||||
|
||||
Trade-offs:
|
||||
|
||||
* Introduces an additional layer that could potentially impact performance.
|
||||
* Requires careful design to avoid creating a monolithic gateway that becomes a bottleneck.
|
||||
|
||||
## Related Patterns
|
||||
|
||||
* [Facade](https://java-design-patterns.com/patterns/facade/): Similar to Gateway in abstracting complex subsystems, but Gateway specifically targets external or remote interfaces.
|
||||
* [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.
|
||||
* [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.
|
||||
* [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.
|
||||
|
||||
## Credits
|
||||
|
||||
* [Gateway](https://martinfowler.com/articles/gateway-pattern.html)
|
||||
* [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)
|
||||
* [Gateway - Martin Fowler](https://martinfowler.com/articles/gateway-pattern.html)
|
||||
* [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)
|
||||
* [Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions](https://amzn.to/3WcFVui)
|
||||
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
|
||||
|
||||
@@ -26,7 +26,6 @@ package com.iluwatar.gateway;
|
||||
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* ExternalServiceA is one of external services.
|
||||
@@ -40,4 +39,3 @@ class ExternalServiceA implements Gateway {
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ class ExternalServiceC implements Gateway {
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
|
||||
public void error() throws Exception {
|
||||
public void error() {
|
||||
// Simulate an exception
|
||||
throw new RuntimeException("Service C encountered an error");
|
||||
}
|
||||
|
||||
@@ -25,17 +25,17 @@
|
||||
package com.iluwatar.gateway;
|
||||
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ServiceFactoryTest {
|
||||
private GatewayFactory gatewayFactory;
|
||||
@@ -65,7 +65,7 @@ public class ServiceFactoryTest {
|
||||
@Test
|
||||
public void testGatewayFactoryRegistrationWithNonExistingKey() {
|
||||
Gateway nonExistingService = gatewayFactory.getGateway("NonExistingService");
|
||||
assertEquals(null, nonExistingService);
|
||||
assertNull(nonExistingService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -88,6 +88,6 @@ public class ServiceFactoryTest {
|
||||
}
|
||||
|
||||
latch.await();
|
||||
assertTrue("This should not fail", !failed.get());
|
||||
assertFalse("This should not fail", failed.get());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user