1 / 16

고급자바프로그래밍 (Advanced Java Programming)

고급자바프로그래밍 (Advanced Java Programming). 강원대학교 컴퓨터학부 2012 년 가을학기 담당교수 정충교. 1.2 Dao 의 분리. 관심사의 분리 미래의 변화에 대비 변화는 대개 한 가지 관심사에서 일어남 관심사의 분리 (Separation of Concerns) 관심이 같은 것 끼리는 하나의 객체 안으로 또는 친한 객체들로 모이게 관심이 다른 것은 가능한 따로 떨어져 서로 영향을 주지 않도록. UserDao 의 관심 사항. DB 연결 얻기

arnold
Download Presentation

고급자바프로그래밍 (Advanced Java Programming)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 고급자바프로그래밍(Advanced Java Programming) 강원대학교컴퓨터학부 2012년 가을학기 담당교수정충교

  2. 1.2 Dao의 분리 • 관심사의 분리 • 미래의 변화에 대비 • 변화는 대개 한 가지 관심사에서 일어남 • 관심사의 분리 (Separation of Concerns) • 관심이 같은 것 끼리는 하나의 객체 안으로 또는 친한 객체들로 모이게 • 관심이 다른 것은 가능한 따로 떨어져 서로 영향을 주지 않도록

  3. UserDao의 관심 사항 • DB 연결 얻기 • SQL 문장을 담은 Statement를 만들고 실행 • 공유자원 반환 • 중복되고 여기 저기 흩어져 있는 DB 연결 코드를 분리, 추출 • 리스트 1-4 • spring30-1.2.2 • 리팩토링

  4. DB 커넥션 만들기의 독립 • DB의 변화에 대처하도록 UserDao를 확장 • 템플릿 메소드 패턴 • 리스트 1-5 • spring30-1.2.3

  5. DB 커넥션 만들기의 독립 • DB의 변화에 대처하도록 UserDao를 확장 • 팩토리메소드 패턴

  6. 문제점 • 다른 상속이 필요할 수 있음 (자바에서는 다중상속이 허용되지 않음) • 슈퍼클래스와서브클래스는 상호 결합이 강하다. • DB 커넥션을 얻는 코드를 다른 종류의 Dao에는 적용할 수 없다. • 디자인 패턴 (design pattern) - 교재 71쪽 참고

  7. 1.3 Dao의 확장 • 1.3.1 클래스의 분리 • 리스트 1-6, 리스트 1-7 • spring30-1.3.1 • UserDao abstract class일 필요 없음, NUserDaoDUserDao불필요 • 문제점 • SimpleConnectionMaker로 타입이 고정됨

  8. 문제점 • UserDao에 특정 ConnectionMaker가 고정적으로 지정되어 있음 public class UserDao { private SimpleConnectionMakersimpleConnectionMaker; public UserDao() { this.simpleConnectionMaker = new SimpleConnectionMaker(); } public void add(User user) throws ClassNotFoundException, SQLException { Connection c = this.simpleConnectionMaker.getConnection(); PreparedStatementps = c.prepareStatement( ... );

  9. 1.3. 2 인터페이스의 도입 • 리스트 1-8, 리스트 1-9, 리스트 1-10

  10. 1.3.3 관계설정 책임의 분리 • 클래스 사이의 불필요한 의존관계

  11. 1.3.3 관계설정 책임의 분리 • 오브젝트 사이의 관계

  12. 1.3.3 관계설정 책임의 분리 • 관계설정 책임을 담당하는 클라이언트 userDaoTest추가 • 특정 ConnectionMaker이름이 들어 있지 않은 “범용” UserDao가 됨 • 어떤 ConnectionMaker를 사용할지 클라이언트가 결정해줌 • 리스트 1-11, 리스트 1-12, 리스트 1-13 • spring30-1.3.3

  13. 1.3.4 원칙과 패턴 • 개방 폐쇄 원칙 (Open/Closed Principle) • 확장에는 열려 있어야하고 변경에는 닫혀 있어야함 • 높은 응집도와 낮은 결합도 (High coherence and low coupling)

  14. 1.3.4 원칙과 패턴 • 전략 패턴 (strategy pattern) (http://en.wikipedia.org/wiki/Strategy_pattern)

More Related