Skip to content

Commit 13d35ec

Browse files
committed
docs: update table module
1 parent d4f8063 commit 13d35ec

File tree

1 file changed

+62
-32
lines changed

1 file changed

+62
-32
lines changed

table-module/README.md

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,33 +71,55 @@ public class UserTableModule {
7171
In the class `App`, we use an instance of the `UserTableModule` to handle user login and registration.
7272

7373
```java
74-
// Create data source and create the user table.
75-
final var dataSource = createDataSource();
76-
createSchema(dataSource);
77-
userTableModule = new UserTableModule(dataSource);
78-
79-
//Initialize two users.
80-
var user1 = new User(1, "123456", "123456");
81-
var user2 = new User(2, "test", "password");
82-
83-
//Login and register using the instance of userTableModule.
84-
userTableModule.registerUser(user1);
85-
userTableModule.login(user1.getUsername(), user1.getPassword());
86-
userTableModule.login(user2.getUsername(), user2.getPassword());
87-
userTableModule.registerUser(user2);
88-
userTableModule.login(user2.getUsername(), user2.getPassword());
89-
90-
deleteSchema(dataSource);
91-
```
92-
93-
The program output:
94-
95-
```java
96-
12:22:13.095 [main] INFO com.iluwatar.tablemodule.UserTableModule - Register successfully!
97-
12:22:13.117 [main] INFO com.iluwatar.tablemodule.UserTableModule - Login successfully!
98-
12:22:13.128 [main] INFO com.iluwatar.tablemodule.UserTableModule - Fail to login!
99-
12:22:13.136 [main] INFO com.iluwatar.tablemodule.UserTableModule - Register successfully!
100-
12:22:13.144 [main] INFO com.iluwatar.tablemodule.UserTableModule - Login successfully!
74+
@Slf4j
75+
public final class App {
76+
77+
private static final String DB_URL = "jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1";
78+
79+
private App() {}
80+
81+
public static void main(final String[] args) throws SQLException {
82+
// Create data source and create the user table.
83+
final var dataSource = createDataSource();
84+
createSchema(dataSource);
85+
var userTableModule = new UserTableModule(dataSource);
86+
87+
// Initialize two users.
88+
var user1 = new User(1, "123456", "123456");
89+
var user2 = new User(2, "test", "password");
90+
91+
// Login and register using the instance of userTableModule.
92+
userTableModule.registerUser(user1);
93+
userTableModule.login(user1.getUsername(), user1.getPassword());
94+
userTableModule.login(user2.getUsername(), user2.getPassword());
95+
userTableModule.registerUser(user2);
96+
userTableModule.login(user2.getUsername(), user2.getPassword());
97+
98+
deleteSchema(dataSource);
99+
}
100+
101+
private static void deleteSchema(final DataSource dataSource)
102+
throws SQLException {
103+
try (var connection = dataSource.getConnection();
104+
var statement = connection.createStatement()) {
105+
statement.execute(UserTableModule.DELETE_SCHEMA_SQL);
106+
}
107+
}
108+
109+
private static void createSchema(final DataSource dataSource)
110+
throws SQLException {
111+
try (var connection = dataSource.getConnection();
112+
var statement = connection.createStatement()) {
113+
statement.execute(UserTableModule.CREATE_SCHEMA_SQL);
114+
}
115+
}
116+
117+
private static DataSource createDataSource() {
118+
var dataSource = new JdbcDataSource();
119+
dataSource.setURL(DB_URL);
120+
return dataSource;
121+
}
122+
}
101123
```
102124

103125
In this example, the `UserTableModule` class is responsible for encapsulating all interactions with the users table in the database. This includes the logic for logging in and registering users. The User class represents the user entity with attributes such as `id`, `username`, and `password`.
@@ -107,18 +129,28 @@ In this example, the `UserTableModule` class is responsible for encapsulating al
107129
3. **User Login**: The `login` method manages the logic for authenticating a user based on their username and password.
108130
4. **Application Flow**: The `App` class demonstrates how to use the `UserTableModule` to register and log in users. The data source is created, the schema is set up, and users are registered and logged in with appropriate feedback.
109131

110-
This example shows how the Table Module pattern centralizes database operations for the `users` table, making the application more modular and easier to maintain.
132+
The program output:
111133

112-
## Class diagram
134+
```
135+
13:59:36.417 [main] INFO com.iluwatar.tablemodule.UserTableModule -- Register successfully!
136+
13:59:36.426 [main] INFO com.iluwatar.tablemodule.UserTableModule -- Login successfully!
137+
13:59:36.426 [main] INFO com.iluwatar.tablemodule.UserTableModule -- Fail to login!
138+
13:59:36.426 [main] INFO com.iluwatar.tablemodule.UserTableModule -- Register successfully!
139+
13:59:36.427 [main] INFO com.iluwatar.tablemodule.UserTableModule -- Login successfully!
140+
```
113141

114-
![Table Module](./etc/table-module.urm.png "Table Module")
142+
This example shows how the Table Module pattern centralizes database operations for the `users` table, making the application more modular and easier to maintain.
115143

116144
## Applicability
117145

118146
* Use when you need to manage data access logic for a database table in a centralized module.
119147
* Ideal for applications that interact heavily with database tables and require encapsulation of database queries.
120148
* Suitable for systems where the database schema may evolve over time, as the changes can be managed within the table module.
121149

150+
## Tutorials
151+
152+
* [Architecture patterns: Domain model and friends (Inviqa)](https://inviqa.com/blog/architecture-patterns-domain-model-and-friends)
153+
122154
## Known Uses
123155

124156
* In enterprise applications where multiple modules need to interact with the same database tables.
@@ -152,5 +184,3 @@ Trade-offs:
152184
* [Core J2EE Patterns: Best Practices and Design Strategies](https://amzn.to/4cAbDap)
153185
* [Java Persistence with Hibernate](https://amzn.to/44tP1ox)
154186
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
155-
* [Architecture patterns: Domain model and friends - Inviqa](https://inviqa.com/blog/architecture-patterns-domain-model-and-friends)
156-
* [Table Module Pattern](http://wiki3.cosc.canterbury.ac.nz/index.php/Table_module_pattern)

0 commit comments

Comments
 (0)