Skip to content

Commit

Permalink
3장: 변하는 것과 변하지 않는 것 - 템플릿 메소드 패턴 적용
Browse files Browse the repository at this point in the history
상속을 통해 확장해서 사용해보자.
하지만, 컴파일 시점에 관계가 결정되기 때문에 굉장히 유연하지 못한 구조가 된다.
  • Loading branch information
kimdahyeee committed Mar 14, 2020
1 parent bc57086 commit d1dc232
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/toby/tobyspring/user/dao/DaoFactory.java
Expand Up @@ -10,7 +10,7 @@
public class DaoFactory {
@Bean
public UserDao userDao() {
UserDao userDao = new UserDao();
UserDao userDao = new UserDaoDeleteAll();
userDao.setDataSource(dataSource());
return userDao;
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/toby/tobyspring/user/dao/UserDao.java
Expand Up @@ -9,7 +9,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
public abstract class UserDao {
private DataSource dataSource;

public void setDataSource(DataSource dataSource) {
Expand Down Expand Up @@ -100,7 +100,7 @@ public void deleteAll() throws SQLException {

try {
connection = dataSource.getConnection();
preparedStatement = connection.prepareStatement("delete from users");
preparedStatement = this.makeStatement(connection);
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw e;
Expand All @@ -119,4 +119,6 @@ public void deleteAll() throws SQLException {
}
}
}

protected abstract PreparedStatement makeStatement(Connection connection) throws SQLException;
}
12 changes: 12 additions & 0 deletions src/main/java/com/toby/tobyspring/user/dao/UserDaoDeleteAll.java
@@ -0,0 +1,12 @@
package com.toby.tobyspring.user.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UserDaoDeleteAll extends UserDao {
@Override
protected PreparedStatement makeStatement(Connection connection) throws SQLException {
return connection.prepareStatement("delete from users");
}
}
9 changes: 1 addition & 8 deletions src/test/java/com/toby/tobyspring/user/dao/UserDaoTest.java
Expand Up @@ -5,15 +5,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import javax.sql.DataSource;
import java.sql.SQLException;
Expand All @@ -33,7 +26,7 @@ public class UserDaoTest {

@BeforeEach
public void setup() {
userDao = new UserDao();
userDao = new UserDaoDeleteAll();
DataSource dataSource = new SingleConnectionDataSource("jdbc:oracle:thin:@localhost:1521:orcl", "dahye", "test", true);
userDao.setDataSource(dataSource);

Expand Down

0 comments on commit d1dc232

Please sign in to comment.