인터페이스의 도입
- 중간에 추상적인 연결고리를 만든다.
- 인터페이스를 이용
- 인터페이스는 어떤 일을 하겠다는 기능만 정의 해놓은 것으로, 인터페이스를 구현한 클래스가 세부 기능을 결정한다.
public interface Connectionmaker {
public Connection make connection() throws ClassNotFoundException,SQLException;
}
public class MysqlConnectionMaker implements ConnectionMaker {
public Connection makeConnection() throws ClassNotFoundException,SQLEception {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://localhost/DBname","ID","PW");
return c;
}
}
public class MariadbConnectionMaker implements ConnectionMaker {
public Connection makeConnection() throws ClassNotFOoundException,SQLEception {
Class.forName("org.mariadb.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mariadb://localhost/DBname","ID","PW");
return c;
}
}
public class UserDao {
private SimpleConnectionMaker simpleConnectionMaker;
public UserDao() {
simpleConnectionMaker = new MysqlConnectionMaker();
}
public void add(user user) throws ClassNotFoundException, SQLException {
Connection c = simpleConnectionMaker.makeNewConnection();
PreparedStatement ps = c.prepareStatement("insert into users(id,name,password)values(?,?,?)");
ps.setString(1,user.getId());
ps.setString(2,user.getName());
ps.setString(3,user.getPassword());
ps.executeUpdate();
ps.close();
c.close();
}
public User get(String id) throws ClassNotFoundException,SQLException {
Connection c = simpleConnectionMaker.makeNewConnection();
PreparedStatement ps = c.prepareStatment("select * from users where id = ?");
ps.setString(1,id);
ResultSet rs = ps.executeQuery();
rs.next();
User user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
rs.close();
ps.close();
c.close();
return user;
}
}
- UserDao 클래스에 전용 클래스를 기술해야한다 --> 전용 클래스 수정시 내부 클래스도 수정해야하는 단점 여전히 가짐
- 확장성 불가
관계설정 책임의 분리
- 그렇다면 다른 부분에서 관계설정을 하게끔 해준다면 UserDao 클래스의 수정은 필요없어진다.
- 생성자를 이용하여 분리
public class UserDao {
private ConnectionMaker connectionMaker;
public UserDao(ConnectionMaker connectionMaker) {
this.connectionMaker = connectionMaker;
}
public void add(user user) throws ClassNotFoundException, SQLException {
Connection c = connectionMaker.makeNewConnection();
PreparedStatement ps = c.prepareStatement("insert into users(id,name,password)values(?,?,?)");
ps.setString(1,user.getId());
ps.setString(2,user.getName());
ps.setString(3,user.getPassword());
ps.executeUpdate();
ps.close();
c.close();
}
public User get(String id) throws ClassNotFoundException,SQLException {
Connection c = connectionMaker.makeNewConnection();
PreparedStatement ps = c.prepareStatment("select * from users where id = ?");
ps.setString(1,id);
ResultSet rs = ps.executeQuery();
rs.next();
User user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
rs.close();
ps.close();
c.close();
return user;
}
}
public class UserDaoTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
ConnectionMaker connectionMaker = new MysqlConnectionMaker();
UserDao dao = new UserDao(connectionMaker);
}
}