[JWPA-1]의존성 주입(Dependency injection)

of 43/43
Java Web Programming Advanced Week01 Dependency Injection 의존성 주입 조영호 [email protected]
  • date post

    27-Aug-2014
  • Category

    Software

  • view

    455
  • download

    3

Embed Size (px)

description

의존성 주입(Dependency Injection)의 개념을 설명하기 위해 만든 강의자료입니다. 의존성 주입이 어떤 방식으로 객체간의 커플링을 느슨하게 만드는지를 설명하고 있습니다.

Transcript of [JWPA-1]의존성 주입(Dependency injection)

  • Java
  • Web
  • Programming
  • Advanced
  • Week01
  • Dependency Injection
  • [email protected]
  • Dependency
  • Dependency
  • Dependency in OO
  • Method Call
  • Object A
  • Object B
  • Object A
  • Object B
  • depends on
  • in Code
  • public class MovieLister { public List findAll() { ... } } public class MovieFinder { private MovieLister lister; public void moviesDirectedBy(String title) { List allMovies = lister.findAll(); ... } } Dependency
  • class Lecture Class Standard Grader Dependency C o d e Rev i ew
  • C o d e Rev i ew
  • class Lecture Class Standard Grader Dependency Class PassFail Grader
  • Coupling
  • = Coupling
  • Object A
  • Object B
  • Dependency
  • = Coupling
  • Object A
  • Object B
  • Dependency
  • Coupling
  • OO
  • Goal
  • = Coupling
  • Object A
  • Object B
  • Dependency
  • Tight Coupling?
  • class Lecture Class Standard Grader Dependency P ro bl e m
  • Class Dependency
  • P ro bl e m
  • class Lecture Class Standard Grader Dependency Class PassFail Grader Class
  • P ro bl e m
  • class Lecture Class Standard Grader Dependency Class PassFail Grader Tight Coupling Dependency
  • S o l u t i o n
  • class Lecture Class Standard Grader Class PassFail Grader Dependency Abstraction Loose Coupling
  • S o l u t i o n
  • class Lecture Class Standard Grader Class PassFail Grader Dependency Abstract Class interface Loose Coupling
  • S o l u t i o n
  • class Lecture Dependency Abstract Class interface Loose Coupling
  • Interface? Discussion
  • Behavior Discussion
  • class Lecture grade() Lecture
  • Grade
  • Discussion class class class
  • class Lecture grade() Lecture
  • Grade
  • Discussion class class class
  • class Lecture grade() Lecture
  • Discussion class class class
  • class Lecture grade() Lecture
  • Discussion class class class
  • class Lecture grade() Lecture
  • Discussion class class class
  • class Lecture grade() ,
  • Lecture
  • Discussion class class class
  • class Lecture grade() Lecture
  • Discussion class class class
  • Program to an interface, not implementation
  • class Lecture grade() Java interface Discussion class class class interface
  • Refactoring
  • P ro bl e m
  • class Lecture Class StandardGrader Class PassFailGrader interface Grader Create Dependency Dependency Use Tight Coupling Class Dependency
  • S o l u t i o n
  • class Lecture Class StandardGrader Class PassFailGrader interface Grader Create Dependency Dependency Use Separate Use From Construction
  • S o l u t i o n
  • class Lecture Class StandardGrader Class PassFailGrader interface Grader Create Dependency Dependency Use Client Dependency Use Separate Use From Construction
  • S o l u t i o n
  • class Lecture Class StandardGrader Class PassFailGrader interface Grader Dependency Use Client Dependency Use Separate Use From Construction Create Dependency
  • S o l u t i o n
  • class Lecture Class StandardGrader Class PassFailGrader interface Grader Dependency Use Client Dependency Use Separate Use From Construction Create Dependency inject Dependency
  • Refactoring
  • Lecture lecture = new Lecture(new StandardGrader(), 80, 90, 65); String grade = lecture.grade(); Lecture lecture = new Lecture(new PassFailGrader(), 80, 90, 65); String grade = lecture.grade(); Lecture Client Lecture (Use) Grader (Create) Lecture (Dependency Injection)
  • Program to an interface, not implementation Dependency Injection
  • Lecture lecture = new Lecture(new StandardGrader(), 80, 90, 65); String grade = lecture.grade(); Lecture lecture = new Lecture(new PassFailGrader(), 80, 90, 65); String grade = lecture.grade(); Lecture
  • ?
  • S o l u t i o n
  • class Lecture Class StandardGrader Class PassFailGrader interface Grader Dependency Use Client Dependency Use Dependency Injection Framework inject Dependency
  • Spring
  • @Controller public class ManageBookController { @Autowired private BookstoreService bookstoreService; @Autowired private CategoryService categoryService;
  • Dependency Injection & Interface Discussion Flexibility Testability Reusability
  • Interface & Dynamic Type Language Discussion
  • class Lecture: def __init__(self, grader, scores): self.scores = scores self.grader = grader def grade(self): return self.grader.grade(self.scores) class StandardGrader: def grade(self): ... class PassFailGrader: def grade(self): ... ex) Python