Skip to content

Commit 67bc5e3

Browse files
committed
docs: update session facade
1 parent bc42e38 commit 67bc5e3

File tree

4 files changed

+68
-65
lines changed

4 files changed

+68
-65
lines changed

Diff for: session-facade/README.md

+52-41
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,51 @@ description: "Learn how to implement the Session Facade Design Pattern in Java t
55
category: Structural
66
language: en
77
tag:
8-
- Abstraction
8+
- API design
9+
- Abstraction
10+
- Architecture
11+
- Business
12+
- Decoupling
13+
- Enterprise patterns
14+
- Facade
15+
- Layered architecture
16+
- Session management
917
---
1018

1119
## Also known as
1220

13-
* Session Facade
21+
* Remote Facade
1422

1523
## Intent of Session Facade Design Pattern
1624

17-
Abstracting the underlying business object interactions by providing a service layer that exposes only the required interfaces
25+
Provide a simplified interface to a complex subsystem, reducing complexity and coupling between client and business logic in enterprise Java applications.
1826

1927
## Detailed Explanation of Session Facade Pattern with Real-World Examples
2028

2129
Real-world example
2230

23-
> In an e-commerce website, users interact with several subsystems like product catalogs, shopping carts,
24-
> payment services, and order management. The Session Facade pattern provides a simplified, centralized interface for these subsystems,
25-
> 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.
31+
> A real-world analogy for the Session Facade pattern is a hotel concierge service. Guests (clients) don't directly interact with various departments like housekeeping, kitchen, transport services, or maintenance. Instead, they interact with the concierge (the facade), who simplifies these interactions. When a guest requests services like room cleaning, dinner reservations, or taxi bookings, the concierge handles communication with multiple hotel departments behind the scenes, providing a simplified and unified interface to the guest, reducing complexity and enhancing guest satisfaction.
2632
2733
In plain words
2834

29-
> The Session Facade design pattern is an excellent choice for decoupling complex components of the system that need to be interacting frequently.
35+
> Session Facade provides a simplified interface to complex business logic in Java applications, reducing client complexity and minimizing network overhead by encapsulating interactions within a single session component.
3036
3137
Sequence diagram
3238

3339
![Session Facade sequence diagram](./etc/session-facade-sequence-diagram.png)
3440

3541
## Programmatic Example of Session Facade Pattern in Java
3642

37-
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.
43+
The Session Facade pattern is a structural design pattern that provides a simplified interface to complex subsystems, making the system easier for clients to interact with. It is especially useful when a client needs access to multiple underlying services without needing to understand their internal complexities.
3844

39-
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.
45+
In the context of an e-commerce website, consider a scenario where users browse products, manage their shopping carts, place orders, and process payments. Rather than directly interacting with each subsystem individually (such as the cart, order, and payment systems), a client can communicate through a single unified Session Facade interface.
4046

41-
Example Scenario:
42-
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.
47+
### Example Scenario:
48+
49+
In this example, the `ShoppingFacade` class simplifies client interactions with three services: the `CartService`, `OrderService`, and `PaymentService`. The client uses the facade to perform high-level operations like adding products to the cart, placing an order, and choosing a payment method, without needing to know the underlying details.
50+
51+
Here's a simplified Java program demonstrating this pattern:
4352

44-
Here’s a simplified programmatic example:
4553
```java
4654
public class App {
4755
public static void main(String[] args) {
@@ -53,14 +61,15 @@ public class App {
5361
}
5462
```
5563

56-
The `ShoppingFacade` acts as an intermediary that facilitates interaction between different services promoting low coupling between these services.
64+
The `ShoppingFacade` serves as a centralized point of interaction for various shopping-related operations, thereby reducing direct coupling between client code and individual subsystem services:
65+
5766
```java
5867
public class ShoppingFacade {
59-
68+
6069
private final CartService cartService;
6170
private final OrderService orderService;
6271
private final PaymentService paymentService;
63-
72+
6473
public ShoppingFacade() {
6574
Map<Integer, Product> productCatalog = new HashMap<>();
6675
productCatalog.put(1, new Product(1, "Wireless Mouse", 25.99, "Ergonomic wireless mouse with USB receiver."));
@@ -70,42 +79,45 @@ public class ShoppingFacade {
7079
orderService = new OrderService(cart);
7180
paymentService = new PaymentService();
7281
}
73-
82+
7483
public Map<Integer, Product> getCart() {
7584
return this.cartService.getCart();
7685
}
77-
86+
7887
public void addToCart(int productId) {
7988
this.cartService.addToCart(productId);
8089
}
8190

82-
91+
8392
public void removeFromCart(int productId) {
8493
this.cartService.removeFromCart(productId);
8594
}
86-
95+
8796
public void order() {
8897
this.orderService.order();
8998
}
90-
99+
91100
public Boolean isPaymentRequired() {
92101
double total = this.orderService.getTotal();
93-
if (total == 0.0) {
102+
if (total==0.0) {
94103
LOGGER.info("No payment required");
95104
return false;
96105
}
97106
return true;
98107
}
99-
108+
100109
public void processPayment(String method) {
101110
Boolean isPaymentRequired = isPaymentRequired();
102111
if (Boolean.TRUE.equals(isPaymentRequired)) {
103112
paymentService.selectPaymentMethod(method);
104113
}
105114
}
115+
}
106116
```
107117

108-
Console output for starting the `App` class's `main` method:
118+
### Console Output
119+
120+
When running the provided example (App.main()), the output might look similar to:
109121

110122
```
111123
19:43:17.883 [main] INFO com.iluwatar.sessionfacade.CartService -- ID: 1
@@ -115,41 +127,40 @@ Description: Ergonomic wireless mouse with USB receiver. successfully added to t
115127
19:43:17.910 [main] INFO com.iluwatar.sessionfacade.OrderService -- Client has chosen to order [ID: 1
116128
```
117129

118-
This is a basic example of the Session Facade design pattern. The actual implementation would depend on specific requirements of your application.
130+
This simplified example demonstrates the essence of the Session Facade pattern. Your actual implementation may vary based on the specific needs of your application.
119131

120132
## When to Use the Session Facade Pattern in Java
121133

122-
* Use when building complex applications with multiple interacting services, where you want to simplify the interaction between various subsystems.
123-
* Ideal for decoupling complex systems that need to interact but should not be tightly coupled.
124-
* 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.
134+
* When dealing with complex enterprise applications containing multiple business objects.
135+
* To provide simplified API calls to clients, hiding the underlying complexity.
136+
* When seeking improved performance and reduced network calls between clients and servers.
125137

126138
## Real-World Applications of Server Session Pattern in Java
127139

128-
* Enterprise JavaBeans (EJB)
129-
* Java EE (Jakarta EE) Applications
140+
* Java EE applications utilizing Enterprise JavaBeans (EJB) as session facades to encapsulate business logic.
141+
* Spring-based applications using services as session facades to simplify interactions between controllers and repositories.
130142

131143
## Benefits and Trade-offs of Server Session Pattern
132144

145+
Benefits:
133146

134-
* Simplifies client-side logic by providing a single entry point for complex operations across multiple services.
135-
* Decouples components of the application, making them easier to maintain, test, and modify without affecting other parts of the system.
136-
* Improves modularity by isolating the implementation details of subsystems from the client.
137-
* Centralizes business logic in one place, making the code easier to manage and update.
147+
* Reduces complexity by providing a simpler interface to a subsystem.
148+
* Improves performance by minimizing network traffic and reducing remote calls.
149+
* Enhances modularity and maintainability by clearly separating business logic and client interactions.
138150

139-
## Trade-offs:
151+
Trade-offs:
140152

141-
* Potential performance bottleneck: Since all requests pass through the facade, it can become a bottleneck if not optimized.
142-
* Increased complexity: If the facade becomes too large or complex, it could counteract the modularity it aims to achieve.
143-
* 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.
153+
* Can introduce additional layers that might increase initial complexity.
154+
* Risk of creating overly broad facades that violate single responsibility principles.
144155

145156
## Related Java Design Patterns
146157

147-
* [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.
148-
* [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.
149-
* [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.
158+
* [Data Transfer Object (DTO)](https://java-design-patterns.com/patterns/data-transfer-object/): Often used together, Session Facade simplifies data transfer by utilizing DTOs to encapsulate data passed between client and server.
159+
* [Facade](https://java-design-patterns.com/patterns/facade/): Session Facade is a specialized version of the Facade pattern, applied specifically in enterprise systems to manage business logic and remote interactions.
150160

151161
## References and Credits
152162

153163
* [Core J2EE Patterns: Best Practices and Design Strategies](https://amzn.to/4cAbDap)
154-
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
155164
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
165+
* [Real World Java EE Patterns-Rethinking Best Practices](https://amzn.to/3EvkzS8)
166+
* [Remote Facade (Martin Fowler)](https://martinfowler.com/eaaCatalog/remoteFacade.html)

Diff for: session-facade/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
<artifactId>junit-jupiter-engine</artifactId>
5151
<scope>test</scope>
5252
</dependency>
53+
<dependency>
54+
<groupId>org.junit.jupiter</groupId>
55+
<artifactId>junit-jupiter-params</artifactId>
56+
<scope>test</scope>
57+
</dependency>
5358
<dependency>
5459
<groupId>org.mockito</groupId>
5560
<artifactId>mockito-core</artifactId>

Diff for: session-facade/src/test/java/com/iluwatar/sessionfacade/AppTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2828

2929
/** The type App test. */
30-
public class AppTest {
30+
class AppTest {
3131

3232
/** Should execute application without exception. */
3333
@org.junit.jupiter.api.Test

Diff for: session-facade/src/test/java/com/iluwatar/sessionfacade/PaymentServiceTest.java

+10-23
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
import static org.mockito.Mockito.*;
2828

2929
import org.junit.jupiter.api.BeforeEach;
30-
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.params.ParameterizedTest;
31+
import org.junit.jupiter.params.provider.CsvSource;
3132
import org.slf4j.Logger;
3233

3334
/** The type Payment service test. */
3435
class PaymentServiceTest {
3536
private PaymentService paymentService;
36-
private OrderService orderService;
3737
private Logger mockLogger;
3838

3939
/** Sets up. */
@@ -44,27 +44,14 @@ void setUp() {
4444
paymentService.LOGGER = mockLogger;
4545
}
4646

47-
/** Test select cash payment method. */
48-
@Test
49-
void testSelectCashPaymentMethod() {
50-
String method = "cash";
47+
@ParameterizedTest
48+
@CsvSource({
49+
"cash, Client have chosen cash payment option",
50+
"credit, Client have chosen credit card payment option",
51+
"cheque, Unspecified payment method type"
52+
})
53+
void testSelectPaymentMethod(String method, String expectedLogMessage) {
5154
paymentService.selectPaymentMethod(method);
52-
verify(mockLogger).info("Client have chosen cash payment option");
53-
}
54-
55-
/** Test select credit card payment method. */
56-
@Test
57-
void testSelectCreditCardPaymentMethod() {
58-
String method = "credit";
59-
paymentService.selectPaymentMethod(method);
60-
verify(mockLogger).info("Client have chosen credit card payment option");
61-
}
62-
63-
/** Test select unspecified payment method. */
64-
@Test
65-
void testSelectUnspecifiedPaymentMethod() {
66-
String method = "cheque";
67-
paymentService.selectPaymentMethod(method);
68-
verify(mockLogger).info("Unspecified payment method type");
55+
verify(mockLogger).info(expectedLogMessage);
6956
}
7057
}

0 commit comments

Comments
 (0)