Skip to content

Recommendation SOLID #10

@jfcofer

Description

@jfcofer

On src/Getway/MySqlConnectorGetway.java

The provided code presents a scenario where the Interface Segregation Principle (ISP) can be applied to improve its design. It manages the connection to a SQLite database (SQLightConnection) and performs CRUD operations on a specific table (mysqlInfo). It interacts directly with the MysqlConnector class, which appears to be designed to manage MySQL connection information, but is being used here to store SQLite connection information.

package Getway;

public interface ConnectorRepository {

    void save(MysqlConnector connector);

    void view(MysqlConnector connector);
}

package Gateway;

import database.DBProperties;
import database.SQLightConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SQLiteConnectorGateway implements ConnectorRepository {

    private final SQLightConnection sqlightConnection;
    private final Connection con;
    private PreparedStatement pst;
    private ResultSet rs;

    public SQLiteConnectorGateway() {
        sqlightConnection = new SQLightConnection();
        con = sqlightConnection.sqliteConnection();
    }

    @Override
    public void save(MysqlConnector connector) {
        try {
            pst = con.prepareStatement("insert into mysqlInfo values(?,?,?,?,?)");
            pst.setInt(1, 1);
            pst.setString(2, connector.hostName);
            pst.setString(3, connector.portName);
            pst.setString(4, connector.userName);
            pst.setString(5, connector.password);
            pst.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(SQLiteConnectorGateway.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void view(MysqlConnector connector) {
        try {
            pst = con.prepareStatement("select * from mysqlInfo where Id=1");
            rs = pst.executeQuery();
            while(rs.next()){
                connector.hostName = rs.getString(2);
                connector.portName = rs.getString(3);
                connector.userName = rs.getString(4);
                connector.password = rs.getString(5);
            }
        } catch (SQLException ex) {
            Logger.getLogger(SQLiteConnectorGateway.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

ConnectorRepository (Interface):

Defines a clear contract for data persistence operations related to connection information (save and view).

SQLiteConnectorGateway:

Implements ConnectorRepository, handling exclusively CRUD operations related to connection information in a SQLite database.

Uses the SQLightConnection class to establish the connection to SQLite independently.

By implementing these changes, we separate concerns more effectively: ConnectorRepository focuses on data persistence operations, while specific gateway classes (SQLiteConnectorGateway) handle database-specific interactions. This approach adheres better to the Interface Segregation Principle and improves the overall design and maintainability of the codebase.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions