forked from sunday-develop/toby-spring
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deleteAll() 메소드와 같이 add()를 전략 패턴 적용해보자. add()에서는 user라는 부가정보가 필요하고, 해당 오브젝트를 받을 수 있도록 AddStatement를 생성하였다. 하지만, 두 가지 문제가 찜찜하다. 1. dao의 메소드 마다 새로운 statementStrategy 구현 클래스를 만들어야 한다. 2. 부가정보가 있는 경우 이를 위해 오브젝트를 전달받는 생성자와 이를 저장해 둘 인스턴스 변수를 번거롭게 만들어야 한다.
- Loading branch information
1 parent
1eda4e0
commit 8ba9028
Showing
3 changed files
with
29 additions
and
14 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/main/java/com/toby/tobyspring/user/dao/AddStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.toby.tobyspring.user.dao; | ||
|
||
import com.toby.tobyspring.user.domain.User; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
|
||
public class AddStatement implements StatementStrategy { | ||
User user; | ||
|
||
public AddStatement(User user) { | ||
this.user = user; | ||
} | ||
|
||
@Override | ||
public PreparedStatement makePreparedStatement(Connection connection) throws SQLException { | ||
PreparedStatement preparedStatement = connection.prepareStatement("insert into users(id, name, password) values(?, ?, ?)"); | ||
preparedStatement.setString(1, user.getId()); | ||
preparedStatement.setString(2, user.getName()); | ||
preparedStatement.setString(3, user.getPassword()); | ||
|
||
return preparedStatement; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters