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: session-facade/README.md
+52-41
Original file line number
Diff line number
Diff line change
@@ -5,43 +5,51 @@ description: "Learn how to implement the Session Facade Design Pattern in Java t
5
5
category: Structural
6
6
language: en
7
7
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
9
17
---
10
18
11
19
## Also known as
12
20
13
-
*Session Facade
21
+
*Remote Facade
14
22
15
23
## Intent of Session Facade Design Pattern
16
24
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.
18
26
19
27
## Detailed Explanation of Session Facade Pattern with Real-World Examples
20
28
21
29
Real-world example
22
30
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.
26
32
27
33
In plain words
28
34
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.
## Programmatic Example of Session Facade Pattern in Java
36
42
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.
38
44
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.
40
46
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:
43
52
44
-
Here’s a simplified programmatic example:
45
53
```java
46
54
publicclassApp {
47
55
publicstaticvoidmain(String[] args) {
@@ -53,14 +61,15 @@ public class App {
53
61
}
54
62
```
55
63
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:
productCatalog.put(1, newProduct(1, "Wireless Mouse", 25.99, "Ergonomic wireless mouse with USB receiver."));
@@ -70,42 +79,45 @@ public class ShoppingFacade {
70
79
orderService =newOrderService(cart);
71
80
paymentService =newPaymentService();
72
81
}
73
-
82
+
74
83
publicMap<Integer, Product>getCart() {
75
84
returnthis.cartService.getCart();
76
85
}
77
-
86
+
78
87
publicvoidaddToCart(intproductId) {
79
88
this.cartService.addToCart(productId);
80
89
}
81
90
82
-
91
+
83
92
publicvoidremoveFromCart(intproductId) {
84
93
this.cartService.removeFromCart(productId);
85
94
}
86
-
95
+
87
96
publicvoidorder() {
88
97
this.orderService.order();
89
98
}
90
-
99
+
91
100
publicBooleanisPaymentRequired() {
92
101
double total =this.orderService.getTotal();
93
-
if (total==0.0) {
102
+
if (total==0.0) {
94
103
LOGGER.info("No payment required");
95
104
returnfalse;
96
105
}
97
106
returntrue;
98
107
}
99
-
108
+
100
109
publicvoidprocessPayment(Stringmethod) {
101
110
Boolean isPaymentRequired = isPaymentRequired();
102
111
if (Boolean.TRUE.equals(isPaymentRequired)) {
103
112
paymentService.selectPaymentMethod(method);
104
113
}
105
114
}
115
+
}
106
116
```
107
117
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:
109
121
110
122
```
111
123
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
115
127
19:43:17.910 [main] INFO com.iluwatar.sessionfacade.OrderService -- Client has chosen to order [ID: 1
116
128
```
117
129
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.
119
131
120
132
## When to Use the Session Facade Pattern in Java
121
133
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.
125
137
126
138
## Real-World Applications of Server Session Pattern in Java
127
139
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.
130
142
131
143
## Benefits and Trade-offs of Server Session Pattern
132
144
145
+
Benefits:
133
146
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.
138
150
139
-
## Trade-offs:
151
+
Trade-offs:
140
152
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.
144
155
145
156
## Related Java Design Patterns
146
157
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.
150
160
151
161
## References and Credits
152
162
153
163
*[Core J2EE Patterns: Best Practices and Design Strategies](https://amzn.to/4cAbDap)
154
-
* [DesignPatterns:Elements of ReusableObject-OrientedSoftware](https://amzn.to/3w0pvKI)
155
164
*[Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
165
+
*[Real World Java EE Patterns-Rethinking Best Practices](https://amzn.to/3EvkzS8)
0 commit comments