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: client-session/README.md
+64-37Lines changed: 64 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -3,13 +3,15 @@ title: Client Session
3
3
category: Behavioral
4
4
language: en
5
5
tags:
6
+
- Client-server
6
7
- Session management
8
+
- State tracking
7
9
- Web development
8
10
---
9
11
10
12
## Also known as
11
13
12
-
* User session
14
+
* User Session
13
15
14
16
## Intent
15
17
@@ -19,60 +21,86 @@ The Client Session design pattern aims to maintain a user's state and data acros
19
21
20
22
Real-World Example
21
23
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.
23
25
24
26
In Plain words
25
27
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.
27
33
28
34
**Programmatic Example**
29
35
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.
31
37
32
-
```java
33
-
publicclassApp {
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.
34
39
35
-
publicstaticvoidmain(String[] args) {
36
-
var server =newServer("localhost", 8080);
37
-
var session1 = server.getSession("Session1");
38
-
var session2 = server.getSession("Session2");
39
-
var request1 =newRequest("Data1", session1);
40
-
var request2 =newRequest("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
+
publicclassServer {
45
+
privateString host;
46
+
privateint port;
47
+
48
+
publicServer(Stringhost, intport) {
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
+
publicSessiongetSession(Stringname) {
57
+
returnnewSession(name, "ClientName");
58
+
}
59
+
60
+
// This method processes a request from a client.
61
+
publicvoidprocess(Requestrequest) {
62
+
// Process the request...
63
+
}
44
64
}
45
65
46
-
@Data
47
-
@AllArgsConstructor
66
+
// The Session class represents a session that is assigned to a client.
48
67
publicclassSession {
68
+
privateString id;
69
+
privateString clientName;
49
70
50
-
/**
51
-
* Session id.
52
-
*/
53
-
privateString id;
54
-
55
-
/**
56
-
* Client name.
57
-
*/
58
-
privateString clientName;
71
+
publicSession(Stringid, StringclientName) {
72
+
this.id = id;
73
+
this.clientName = clientName;
74
+
}
59
75
76
+
// Other methods...
60
77
}
78
+
```
61
79
62
-
@Data
63
-
@AllArgsConstructor
64
-
publicclassRequest {
65
-
66
-
privateString data;
67
-
68
-
privateSession 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.
69
81
82
+
```java
83
+
publicclassApp {
84
+
publicstaticvoidmain(String[] args) {
85
+
var server =newServer("localhost", 8080);
86
+
var session1 = server.getSession("Session1");
87
+
var session2 = server.getSession("Session2");
88
+
var request1 =newRequest("Data1", session1);
89
+
var request2 =newRequest("Data2", session2);
90
+
server.process(request1);
91
+
server.process(request2);
92
+
}
70
93
}
71
94
```
72
95
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.
74
97
75
-

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
0 commit comments