Browser docs

Table Module

Intent

Table Module organizes domain logic with one class per table in the database, and a single instance of a class contains the various procedures that will act on the data.

Explanation

Real world example

When dealing with a user system, we need some operations on the user table. We can use the table module pattern in this scenario. We can create a class named UserTableModule and initialize a instance of that class to handle the business logic for all rows in the user table.

In plain words

A single instance that handles the business logic for all rows in a database table or view.

Programmatic Example

In the example of the user system, we need to deal with the domain logic of user login and user registration. We can use the table module pattern and create an instance of the class UserTableModule to handle the business logic for all rows in the user table.

Here is the basic User entity.

 1@Setter
 2@Getter
 3@ToString
 4@EqualsAndHashCode
 5@AllArgsConstructor
 6public class User {
 7  private int id;
 8  private String username;
 9  private String password;
10}

Here is the UserTableModule class.

 1public class UserTableModule {
 2  private final DataSource dataSource;
 3  private Connection connection = null;
 4  private ResultSet resultSet = null;
 5  private PreparedStatement preparedStatement = null;
 6
 7  public UserTableModule(final DataSource userDataSource) {
 8    this.dataSource = userDataSource;
 9  }
10  
11  /**
12   * Login using username and password.
13   *
14   * @param username the username of a user
15   * @param password the password of a user
16   * @return the execution result of the method
17   * @throws SQLException if any error
18   */
19  public int login(final String username, final String password) throws SQLException {
20  		// Method implementation.
21
22  }
23
24  /**
25   * Register a new user.
26   *
27   * @param user a user instance
28   * @return the execution result of the method
29   * @throws SQLException if any error
30   */
31  public int registerUser(final User user) throws SQLException {
32  		// Method implementation.
33  }
34}

In the class App, we use an instance of the UserTableModule to handle user login and registration.

 1// Create data source and create the user table.
 2final var dataSource = createDataSource();
 3createSchema(dataSource);
 4userTableModule = new UserTableModule(dataSource);
 5
 6//Initialize two users.
 7var user1 = new User(1, "123456", "123456");
 8var user2 = new User(2, "test", "password");
 9
10//Login and register using the instance of userTableModule.
11userTableModule.registerUser(user1);
12userTableModule.login(user1.getUsername(), user1.getPassword());
13userTableModule.login(user2.getUsername(), user2.getPassword());
14userTableModule.registerUser(user2);
15userTableModule.login(user2.getUsername(), user2.getPassword());
16
17deleteSchema(dataSource);

The program output:

112:22:13.095 [main] INFO com.iluwatar.tablemodule.UserTableModule - Register successfully!
212:22:13.117 [main] INFO com.iluwatar.tablemodule.UserTableModule - Login successfully!
312:22:13.128 [main] INFO com.iluwatar.tablemodule.UserTableModule - Fail to login!
412:22:13.136 [main] INFO com.iluwatar.tablemodule.UserTableModule - Register successfully!
512:22:13.144 [main] INFO com.iluwatar.tablemodule.UserTableModule - Login successfully!

Class diagram

Applicability

Use the Table Module Pattern when

  • Domain logic is simple and data is in tabular form.
  • The application only uses a few shared common table-oriented data structures.

Credits