Skip to content

Commit a33b324

Browse files
committed
docs: update client session
1 parent f2753b9 commit a33b324

File tree

2 files changed

+64
-37
lines changed

2 files changed

+64
-37
lines changed

client-session/README.md

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ title: Client Session
33
category: Behavioral
44
language: en
55
tags:
6+
- Client-server
67
- Session management
8+
- State tracking
79
- Web development
810
---
911

1012
## Also known as
1113

12-
* User session
14+
* User Session
1315

1416
## Intent
1517

@@ -19,60 +21,86 @@ The Client Session design pattern aims to maintain a user's state and data acros
1921

2022
Real-World Example
2123

22-
> You're looking to create a data management app allowing users to send requests to the server to modify and make changes to data stored on their devices. These requests are small and the data is individual to each user, negating the need for a large scale database implementation. Using the client session pattern, you are able to handle multiple concurrent requests, load balancing clients across different servers with ease due to servers remaining stateless. You also remove the need to store session IDs on the server side due to clients providing all the information that a server needs to perform their process.
24+
> A real-world example of the Client Session pattern is a library membership system. When a member logs in, the system starts a session to track their borrowing activities. This session holds data such as the member's ID, current borrowed books, due dates, and any fines. As the member browses the catalog, borrows books, or returns them, the session maintains this stateful information, ensuring the member's interactions are consistent and personalized until they log out or the session expires. This approach helps the library system manage user-specific data efficiently across multiple interactions, providing a seamless and personalized experience for the members.
2325
2426
In Plain words
2527

26-
> Instead of storing information about the current client and the information being accessed on the server, it is maintained client side only. Client has to send session data with each request to the server and has to send an updated state back to the client, which is stored on the clients machine. The server doesn't have to store the client information. ([ref](https://dzone.com/articles/practical-php-patterns/practical-php-patterns-client))
28+
> The Client Session pattern manages user-specific data across multiple requests within a web application to maintain continuity and personalization.
29+
30+
Wikipedia says
31+
32+
> The client-server model on Wikipedia describes a system where client devices request services and resources from centralized servers. This model is crucial in web applications where client sessions are used to manage user-specific data across multiple requests. For example, when a bank customer accesses online banking services, their login credentials and session state are managed by the web server to maintain continuity of their interactions.
2733
2834
**Programmatic Example**
2935

30-
Here is the sample code to describe the client-session pattern. In the below code we are first creating an instance of the Server. This server instance will then be used to get Session objects for two clients. As you can see from the code below the Session object can be used to store any relevant information that are required by the server to process the client request. These session objects will then be passed on with every Request to the server. The Request will have the Session object that stores the relevant client details along with the required data for processing the request. The session information in every request helps the server identify the client and process the request accordingly.
36+
The Client Session design pattern is a behavioral design pattern that maintains a user's state and data across multiple requests within a web application, ensuring a continuous and personalized user experience. This pattern is commonly used in web applications where user-specific data needs to be managed across multiple requests.
3137

32-
```java
33-
public class App {
38+
In the given code, we have a `Server` class and a `Session` class. The `Server` class represents the server that processes incoming requests and assigns sessions to clients. The `Session` class represents a session that is assigned to a client.
3439

35-
public static void main(String[] args) {
36-
var server = new Server("localhost", 8080);
37-
var session1 = server.getSession("Session1");
38-
var session2 = server.getSession("Session2");
39-
var request1 = new Request("Data1", session1);
40-
var request2 = new Request("Data2", session2);
41-
server.process(request1);
42-
server.process(request2);
43-
}
40+
Here's a programmatic example of the Client Session design pattern using the given code:
41+
42+
```java
43+
// The Server class represents the server that processes incoming requests and assigns sessions to clients.
44+
public class Server {
45+
private String host;
46+
private int port;
47+
48+
public Server(String host, int port) {
49+
this.host = host;
50+
this.port = port;
51+
}
52+
53+
// Other methods...
54+
55+
// This method returns a new session for a client.
56+
public Session getSession(String name) {
57+
return new Session(name, "ClientName");
58+
}
59+
60+
// This method processes a request from a client.
61+
public void process(Request request) {
62+
// Process the request...
63+
}
4464
}
4565

46-
@Data
47-
@AllArgsConstructor
66+
// The Session class represents a session that is assigned to a client.
4867
public class Session {
68+
private String id;
69+
private String clientName;
4970

50-
/**
51-
* Session id.
52-
*/
53-
private String id;
54-
55-
/**
56-
* Client name.
57-
*/
58-
private String clientName;
71+
public Session(String id, String clientName) {
72+
this.id = id;
73+
this.clientName = clientName;
74+
}
5975

76+
// Other methods...
6077
}
78+
```
6179

62-
@Data
63-
@AllArgsConstructor
64-
public class Request {
65-
66-
private String data;
67-
68-
private Session session;
80+
In the `main` method, we create an instance of `Server`, create two sessions for two different clients, and then pass these sessions to the server in the request along with the data. The server is then able to interpret the client based on the session associated with it.
6981

82+
```java
83+
public class App {
84+
public static void main(String[] args) {
85+
var server = new Server("localhost", 8080);
86+
var session1 = server.getSession("Session1");
87+
var session2 = server.getSession("Session2");
88+
var request1 = new Request("Data1", session1);
89+
var request2 = new Request("Data2", session2);
90+
server.process(request1);
91+
server.process(request2);
92+
}
7093
}
7194
```
7295

73-
## Architecture Diagram
96+
In this example, the `Server` class is responsible for creating and managing sessions for clients, and the `Session` class represents the client's session. The `Request` class represents a request from a client, which includes the client's session and data. The server processes the request based on the client's session.
7497

75-
![alt text](./etc/session_state_pattern.png "Session State Pattern")
98+
Running the program produces the following console output:
99+
100+
```
101+
19:28:49.152 [main] INFO com.iluwatar.client.session.Server -- Processing Request with client: Session1 data: Data1
102+
19:28:49.154 [main] INFO com.iluwatar.client.session.Server -- Processing Request with client: Session2 data: Data2
103+
```
76104

77105
## Applicability
78106

@@ -110,7 +138,6 @@ Trade-offs:
110138

111139
## Credits
112140

113-
* [DZone - Practical PHP patterns](https://dzone.com/articles/practical-php-patterns/practical-php-patterns-client)
114-
* [Client Session State Design Pattern - Ram N Java](https://www.youtube.com/watch?v=ycOSj9g41pc)
115141
* [Professional Java for Web Applications](https://amzn.to/4aazY59)
116142
* [Securing Web Applications with Spring Security](https://amzn.to/3PCCEA1)
143+
* [Client Session State Design Pattern: Explained Simply (Ram N Java)](https://www.youtube.com/watch?v=ycOSj9g41pc)
-49 KB
Binary file not shown.

0 commit comments

Comments
 (0)