Skip to content

Commit bf77e0b

Browse files
authored
Merge branch 'iluwatar:master' into master
2 parents dd9b0d0 + f0f4c7d commit bf77e0b

File tree

24 files changed

+1233
-14
lines changed

24 files changed

+1233
-14
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3357,6 +3357,15 @@
33573357
"contributions": [
33583358
"code"
33593359
]
3360+
},
3361+
{
3362+
"login": "shahdhoss",
3363+
"name": "Shahd Hossam",
3364+
"avatar_url": "https://avatars.githubusercontent.com/u/132148556?v=4",
3365+
"profile": "https://github.com/shahdhoss",
3366+
"contributions": [
3367+
"code"
3368+
]
33603369
}
33613370
],
33623371
"contributorsPerLine": 6,

.github/workflows/presubmit.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Presubmit.ai
2+
3+
permissions:
4+
contents: read
5+
pull-requests: write
6+
issues: write
7+
8+
on:
9+
pull_request_target: # Handle forked repository PRs in the base repository context
10+
types: [opened, synchronize]
11+
pull_request_review_comment: # Handle review comments
12+
types: [created]
13+
14+
jobs:
15+
review:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Check required secrets
19+
run: |
20+
if [ -z "${{ secrets.LLM_API_KEY }}" ]; then
21+
echo "Error: LLM_API_KEY secret is not configured"
22+
exit 1
23+
fi
24+
25+
- name: Check out PR code
26+
uses: actions/checkout@v3
27+
with:
28+
ref: ${{ github.event.pull_request.head.sha }}
29+
30+
- name: Run AI Reviewer
31+
uses: presubmit/ai-reviewer@latest
32+
env:
33+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
35+
LLM_MODEL: "gemini-1.5-flash"

.github/workflows/stale.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ jobs:
1717
days-before-pr-stale: 60
1818
days-before-issue-close: -1
1919
days-before-pr-close: -1
20+
exempt-issue-labels: 'info: help wanted'

PULL_REQUEST_TEMPLATE.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
<!--
2-
Thank you for contributing to Java Design Patterns!
1+
# Pull Request Template
32

4-
If you're unsure where to start, please refer to the contributing doc:
3+
## What does this PR do?
54

6-
https://github.com/iluwatar/java-design-patterns/wiki/01.-How-to-contribute
5+
<!-- Provide a short description of what this pull request does. -->
76

8-
If you still have questions, please let us know via issues or [gitter](https://matrix.to/#/#iluwatar_java-design-patterns:gitter.im).
9-
-->
10-
11-
## What problem does this PR solve?
12-
13-
<!-- Please describe the problem you're trying to solve. Uncomment the following line if this PR closes some issues -->
14-
<!-- Close #<issue number> -->
7+
<!-- Fixes #<issue-number> (if applicable) -->

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=coverage)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
77
[![Join the chat at https://gitter.im/iluwatar/java-design-patterns](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
88
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
9-
[![All Contributors](https://img.shields.io/badge/all_contributors-368-orange.svg?style=flat-square)](#contributors-)
9+
[![All Contributors](https://img.shields.io/badge/all_contributors-369-orange.svg?style=flat-square)](#contributors-)
1010
<!-- ALL-CONTRIBUTORS-BADGE:END -->
1111

1212
<br/>
@@ -551,6 +551,7 @@ This project is licensed under the terms of the MIT license.
551551
<tr>
552552
<td align="center" valign="top" width="16.66%"><a href="https://github.com/depthlending"><img src="https://avatars.githubusercontent.com/u/164312726?v=4?s=100" width="100px;" alt="BiKangNing"/><br /><sub><b>BiKangNing</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=depthlending" title="Documentation">📖</a></td>
553553
<td align="center" valign="top" width="16.66%"><a href="https://github.com/TarunVishwakarma1"><img src="https://avatars.githubusercontent.com/u/138651451?v=4?s=100" width="100px;" alt="Tarun Vishwakarma"/><br /><sub><b>Tarun Vishwakarma</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=TarunVishwakarma1" title="Code">💻</a></td>
554+
<td align="center" valign="top" width="16.66%"><a href="https://github.com/shahdhoss"><img src="https://avatars.githubusercontent.com/u/132148556?v=4?s=100" width="100px;" alt="Shahd Hossam"/><br /><sub><b>Shahd Hossam</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=shahdhoss" title="Code">💻</a></td>
554555
</tr>
555556
</tbody>
556557
</table>

dynamic-proxy/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<dependency>
3939
<groupId>com.fasterxml.jackson.core</groupId>
4040
<artifactId>jackson-core</artifactId>
41-
<version>2.18.1</version>
41+
<version>2.18.2</version>
4242
</dependency>
4343
<dependency>
4444
<groupId>com.fasterxml.jackson.core</groupId>

event-sourcing/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<dependency>
4343
<groupId>com.fasterxml.jackson.core</groupId>
4444
<artifactId>jackson-core</artifactId>
45-
<version>2.18.1</version>
45+
<version>2.18.2</version>
4646
</dependency>
4747
<dependency>
4848
<groupId>com.fasterxml.jackson.core</groupId>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
<module>microservices-distributed-tracing</module>
220220
<module>microservices-client-side-ui-composition</module>
221221
<module>microservices-idempotent-consumer</module>
222+
<module>session-facade</module>
222223
<module>templateview</module>
223224
<module>money</module>
224225
<module>table-inheritance</module>

session-facade/README.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
title: "Session Facade Pattern in Java: Simplifying Complex System Interfaces"
3+
shortTitle: Session Facade
4+
description: "Learn how to implement the Session Facade Design Pattern in Java to create a unified interface for complex subsystems. Simplify your code and enhance maintainability with practical examples and use cases."
5+
category: Structural
6+
language: en
7+
tag:
8+
- Abstraction
9+
- API design
10+
- Code simplification
11+
- Decoupling
12+
- Encapsulation
13+
- Gang Of Four
14+
- Interface
15+
---
16+
17+
## Also known as
18+
19+
* Session Facade
20+
21+
## Intent of Session Facade Design Pattern
22+
23+
Abstracting the underlying business object interactions by providing a service layer that exposes only the required interfaces
24+
25+
## Detailed Explanation of Session Facade Pattern with Real-World Examples
26+
27+
Real-world example
28+
29+
> In an e-commerce website, users interact with several subsystems like product catalogs, shopping carts,
30+
> payment services, and order management. The Session Facade pattern provides a simplified, centralized interface for these subsystems,
31+
> allowing the client to interact with just a few high-level methods (e.g., addToCart(), placeOrder(), selectPaymentMethod()), instead of directly communicating with each subsystem, using a facade supports low coupling between classes and high cohesion within each service, allowing them to focus on their specific responsibilities.
32+
33+
In plain words
34+
35+
> The Session Facade design pattern is an excellent choice for decoupling complex components of the system that need to be interacting frequently.
36+
37+
## Programmatic Example of Session Facade Pattern in Java
38+
39+
The Session Facade design pattern is a structural design pattern that provides a simplified interface to a set of complex subsystems, reducing the complexity for the client. This pattern is particularly useful in situations where the client needs to interact with multiple services or systems but doesn’t need to know the internal workings of each service.
40+
41+
In the context of an e-commerce website, imagine a system where users can browse products, add items to the shopping cart, process payments, and place orders. Instead of the client directly interacting with each individual service (cart, order, payment), the Session Facade provides a single, unified interface for these operations.
42+
43+
Example Scenario:
44+
In this example, the ShoppingFacade class manages interactions with three subsystems: the `CartService`, `OrderService`, and `PaymentService`. The client interacts with the facade to perform high-level operations like adding items to the cart, placing an order, and selecting a payment method.
45+
46+
Here’s a simplified programmatic example:
47+
```java
48+
public class App {
49+
public static void main(String[] args) {
50+
ShoppingFacade shoppingFacade = new ShoppingFacade();
51+
shoppingFacade.addToCart(1);
52+
shoppingFacade.order();
53+
shoppingFacade.selectPaymentMethod("cash");
54+
}
55+
}
56+
```
57+
58+
The `ShoppingFacade` acts as an intermediary that facilitates interaction between different services promoting low coupling between these services.
59+
```java
60+
public class ShoppingFacade {
61+
62+
private final CartService cartService;
63+
private final OrderService orderService;
64+
private final PaymentService paymentService;
65+
66+
public ShoppingFacade() {
67+
Map<Integer, Product> productCatalog = new HashMap<>();
68+
productCatalog.put(1, new Product(1, "Wireless Mouse", 25.99, "Ergonomic wireless mouse with USB receiver."));
69+
productCatalog.put(2, new Product(2, "Gaming Keyboard", 79.99, "RGB mechanical gaming keyboard with programmable keys."));
70+
Map<Integer, Product> cart = new HashMap<>();
71+
cartService = new CartService(cart, productCatalog);
72+
orderService = new OrderService(cart);
73+
paymentService = new PaymentService();
74+
}
75+
76+
public Map<Integer, Product> getCart() {
77+
return this.cartService.getCart();
78+
}
79+
80+
public void addToCart(int productId) {
81+
this.cartService.addToCart(productId);
82+
}
83+
84+
85+
public void removeFromCart(int productId) {
86+
this.cartService.removeFromCart(productId);
87+
}
88+
89+
public void order() {
90+
this.orderService.order();
91+
}
92+
93+
public Boolean isPaymentRequired() {
94+
double total = this.orderService.getTotal();
95+
if (total == 0.0) {
96+
LOGGER.info("No payment required");
97+
return false;
98+
}
99+
return true;
100+
}
101+
102+
public void processPayment(String method) {
103+
Boolean isPaymentRequired = isPaymentRequired();
104+
if (Boolean.TRUE.equals(isPaymentRequired)) {
105+
paymentService.selectPaymentMethod(method);
106+
}
107+
}
108+
```
109+
110+
Console output for starting the `App` class's `main` method:
111+
112+
```
113+
19:43:17.883 [main] INFO com.iluwatar.sessionfacade.CartService -- ID: 1
114+
Name: Wireless Mouse
115+
Price: $25.99
116+
Description: Ergonomic wireless mouse with USB receiver. successfully added to the cart
117+
19:43:17.910 [main] INFO com.iluwatar.sessionfacade.OrderService -- Client has chosen to order [ID: 1
118+
```
119+
120+
This is a basic example of the Session Facade design pattern. The actual implementation would depend on specific requirements of your application.
121+
122+
## When to Use the Session Facade Pattern in Java
123+
124+
* Use when building complex applications with multiple interacting services, where you want to simplify the interaction between various subsystems.
125+
* Ideal for decoupling complex systems that need to interact but should not be tightly coupled.
126+
* Suitable for applications where you need a single point of entry to interact with multiple backend services, like ecommerce platforms, booking systems, or order management systems.
127+
128+
## Real-World Applications of Server Session Pattern in Java
129+
130+
* Enterprise JavaBeans (EJB)
131+
* Java EE (Jakarta EE) Applications
132+
133+
## Benefits and Trade-offs of Server Session Pattern
134+
135+
136+
* Simplifies client-side logic by providing a single entry point for complex operations across multiple services.
137+
* Decouples components of the application, making them easier to maintain, test, and modify without affecting other parts of the system.
138+
* Improves modularity by isolating the implementation details of subsystems from the client.
139+
* Centralizes business logic in one place, making the code easier to manage and update.
140+
141+
## Trade-offs:
142+
143+
* Potential performance bottleneck: Since all requests pass through the facade, it can become a bottleneck if not optimized.
144+
* Increased complexity: If the facade becomes too large or complex, it could counteract the modularity it aims to achieve.
145+
* Single point of failure: If the facade encounters issues, it could affect the entire system's operation, making it crucial to handle errors and exceptions properly.
146+
147+
## Related Java Design Patterns
148+
149+
* [Facade](https://java-design-patterns.com/patterns/facade/): The Session Facade pattern is a specific application of the more general Facade pattern, which simplifies access to complex subsystems.
150+
* [Command](https://java-design-patterns.com/patterns/command/): Useful for encapsulating requests and passing them to the session facade, which could then manage the execution order.
151+
* [Singleton](https://java-design-patterns.com/patterns/singleton/): Often used to create a single instance of the session facade for managing the entire workflow of a subsystem.
152+
153+
## References and Credits
154+
155+
* [Core J2EE Patterns: Best Practices and Design Strategies](https://amzn.to/4cAbDap)
156+
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
157+
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
92.6 KB
Loading

0 commit comments

Comments
 (0)