I'm learning about SRP and had to reduce the responsibility of a customer object, which contained too much info. So I turned that into just a Pojo and pulled out the database logic. I'm trying to find the best design for this setup and am split between making the DatabaseManager class be a parent class, or a singleton that can return the single connection object. This system will be used to insert and delete customers from the database. I don't want my domain objects/DAOs to worry about the connection details. Is the design I currently have a strong follower of OOP design principles?
public class DatabaseManager {
private Connection conn;
private static DatabaseManager managerInstance = new DatabaseManager();
private DatabaseManager() {
}
public static DatabaseManager getInstance() {
return managerInstance;
}
/**
* contains connection details
*
* @throws SQLException
*/
public void connect() throws SQLException {
System.out.println("Established Database Connection...");
conn = DriverManager.getConnection("Some/Database/URL");
}
public Connection getConnectionObject() {
return conn;
}
public void disconnect() throws SQLException {
conn.close();
System.out.println("Disconnected from Database...");
}
}
Here is the Customer Object:
public class Customer {
private int id;
private String name;
private boolean active;
public Customer(int id, String name, String department, boolean working) {
super();
this.id = id;
this.name = name;
this.department = department;
this.working = working;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", department="
+ department + ", working=" + working + "]";
}
}
The customer DAO:
public class CustomerDAO {
public CustomerDAO() {
}
public void addCustomer(Customer Customer) throws SQLException {
DatabaseManager.getInstance().getConnectionObject().prepareStatement("some sql... ");
}
public void removeCustomer(Customer Customer) throws SQLException {
DatabaseManager.getInstance().getConnectionObject().prepareStatement("some sql... ");
// implementation details avoided
}
}