///////
Search

Stack 알고리즘, Spring 전략 패턴_장서현

알고리즘 기초

프로그래머스: 올바른 괄호

TDD로 풀기
@Test @DisplayName("올바른 괄호 테스트 통과") void bracket() { CorrectBracket correctBracket = new CorrectBracket(); assertTrue(correctBracket.solution("()()")); assertTrue(correctBracket.solution("(())()")); assertFalse(correctBracket.solution(")()(")); assertFalse(correctBracket.solution("(()(")); assertFalse(correctBracket.solution( ("((((((((((((((((((((((((((((((((((((((((((((((((()))))))))))))))))))))))))))))))))))))))))))))))))))))"))); }
Java
복사
Stack을 사용하지 않는 code(1)
Stack을 사용하지 않는 code(2)
⇒ Stack을 사용지 않으면 시간초과로 실패!
Stack을 사용한 code
⇒ Stack을 사용하면 연산 속도가 빠르다.

토비의 스프링 3장

try/catch/finally 코드의 문제점

Strategy 패턴: interface를 의존하게 해서 구현체를 필요에 따라 선택할 수 있게 설계하는 디자인 패턴
interface StatementStrategy
public interface StatementStrategy { PreparedStatement makePreparedStatement(Connection con) throws SQLException; }
Java
복사
class DeleteAllStrategy: 인터페이스 구현
public class DeleteAllStrategy implements StatementStrategy { @Override public PreparedStatement makePreparedStatement(Connection con) throws SQLException { return con.prepareStatement("DELETE FROM `likelion-db`.users"); } }
Java
복사
⇒ Connection을 받은 후 쿼리를 작성해서 PreparedStatement를 리턴
DeleteAllStrategy 사용하기
public void deleteAll() { Connection con = null; PreparedStatement ps = null; try { con = connectionMaker.makeConnection(); ps = new DeleteAllStrategy().makePreparedStatement(con); ps.executeUpdate(); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } catch (SQLException e) { throw new RuntimeException(e); } finally { if (ps != null) { try { ps.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (con != null) { try { con.close(); } catch (SQLException e) { throw new RuntimeException(e); } } } }
Java
복사
공통로직 jdbcContextWithStatementStrategy 분리
public void jdbcContextWithStatementStrategy(StatementStrategy stmt) throws SQLException, ClassNotFoundException { Connection con = null; PreparedStatement ps = null; try { con = connectionMaker.makeConnection(); ps = stmt.makePreparedStatement(con); ps.executeUpdate(); } catch (SQLException | ClassNotFoundException e) { throw e; } finally { if (ps != null) { try { ps.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (con != null) { try { con.close(); } catch (SQLException e) { throw new RuntimeException(e); } } } }
Java
복사
매개변수로 StatementStrategy를 받는다.
executeUpdate()를 사용하는 모든 곳에서 사용할 수 있다.
왜냐하면 PreparedStatement만 바뀌기 때문에 StatementStrategy의 구현체만 바꿔주면 이렇게 기나긴 로직을 계속 재사용할 수 있는 것!
class AddStrategy: 인터페이스 구현
public class AddStrategy implements StatementStrategy { private User user; public AddStrategy(User user) { this.user = user; } @Override public PreparedStatement makePreparedStatement(Connection con) throws SQLException { PreparedStatement ps = con.prepareStatement("INSERT INTO users(id, name, password) values(?,?,?)"); ps.setString(1, user.getId()); ps.setString(2, user.getName()); ps.setString(3, user.getPassword()); return ps; } }
Java
복사
AddStrategy & jdbcContextWithStatementStrategy 사용하기
public void add(User user) throws ClassNotFoundException, SQLException { AddStrategy addStrategy = new AddStrategy(user); jdbcContextWithStatementStrategy(addStrategy); }
Java
복사