Contenu connexe Similaire à [JWAP-2] DI & Spring Similaire à [JWAP-2] DI & Spring (20) [JWAP-2] DI & Spring22. public class ScoreRepository {
private String url;
private HttpClient client;
public ScoreRepository() {
this.url = “http://localhost:3000/scores”
this.client = new HttpClient(
new MultiThreadedHttpConnectionManager());
}
public int [] getScores() {
GetMethod get = new GetMethod(url);
try {
if (client.executeMethod(get) == HttpStatus.SC_OK) {
return new Gson().fromJson(get.getResponseBodyAsString(),
int [].class);
} else {
return new int [0];
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
get.releaseConnection();
}
}
}
ScoreRepository
24. public class Lecture {
private ScoreRepository scoreRepository;
private Grader grader;
public Lecture(Grader grader) {
this.grader = grader;
this.scoreRepository = new ScoreRepository();
}
public String grade() {
return grader.grade(scoreRepository.getScores());
}
}
Lecture
54. public class Lecture {
private ScoreRepository scoreRepository;
private Grader grader;
public Lecture(Grader grader) {
this.grader = grader;
this.scoreRepository = new ScoreRepository();
}
public String grade() {
return grader.grade(scoreRepository.getScores());
}
}
Lecture
61. public class Lecture {
private ScoreRepository scoreRepository;
private Grader grader;
public Lecture(Grader grader) {
this.grader = grader;
this.scoreRepository = new ScoreRepository();
}
public String grade() {
return grader.grade(scoreRepository.getScores());
}
}
Hidden Dependency
64. @Test
public void starndardGrade() {
Lecture lecture = new Lecture(new StandardGrader());
String grade = lecture.grade();
assertEquals(A:1 B:1 C:1 D:0 F:2 , grade);
}
assertEquals
88. public class Lecture {
private ScoreRepository scoreRepository;
private Grader grader;
public Lecture(Grader grader) {
this.grader = grader;
this.scoreRepository = new ScoreRepository();
}
public String grade() {
return grader.grade(scoreRepository.getScores());
}
}
Tight Coupling
90. public class Lecture {
private ScoreRepository scoreRepository;
private Grader grader;
public Lecture(Grader grader,
ScoreRepository scoreRepository) {
this.grader = grader;
this.scoreRepository = scoreRepository;
}
Loose Coupling
94.
@RunWith(MockitoJUnitRunner.class)
public class LectureTest {
@Mock
private ScoreRepository repository;
@Test
public void starndardGrade() {
when(repository.getScores())
.thenReturn(new int [] {50,20,70,80,90});
Lecture lecture = new Lecture(new StandardGrader(), repository);
String grade = lecture.grade();
assertEquals(A:1 B:1 C:1 D:0 F:2 , grade);
}
97. lecture = new Lecture(new StandardGrader(),
new HttpScoreRepository());
lecture = new Lecture(new PassFailGrader(),
new HttpScoreRepository());
Loose Coupling
= Complex Object Graph
103. public class Score {
private static LectureRegistry lectureFactory = new LectureRegistry();
public static void main(String [] args) {
Lecture lecture = null;
Scanner scanner = new Scanner(System.in);
try {
System.out.print(grader? );
switch(scanner.next()) {
case st:
lecture = lectureFactory.getStandardGradeLecture();
break;
case pf:
lecture = lectureFactory.getPassFailGradeLecture();
break;
default:
return;
}
System.out.println(lecture.grade());
} finally {
scanner.close();
}
}
}
Score class
130. ?xml version=1.0 encoding=UTF-8?
!DOCTYPE beans PUBLIC -//SPRING//DTD BEAN 2.0//EN
http://www.springframework.org/dtd/spring-beans-2.0.dtd
beans
bean id=httpScoreRepository class=com.nhnnext.score.scores.HttpScoreRepository /
/beans
Scores.xml
132. ?xml version=1.0 encoding=UTF-8?
!DOCTYPE beans PUBLIC -//SPRING//DTD BEAN 2.0//EN
http://www.springframework.org/dtd/spring-beans-2.0.dtd
beans
bean id=httpScoreRepository class=com.nhnnext.score.scores.HttpScoreRepository /
bean id=standardGrader class=com.nhnnext.score.lecture.StandardGrader /
bean id=passFailGrader class=com.nhnnext.score.lecture.PassFailGrader /
/beans
Scores.xml
134. ?xml version=1.0 encoding=UTF-8?
!DOCTYPE beans PUBLIC -//SPRING//DTD BEAN 2.0//EN
http://www.springframework.org/dtd/spring-beans-2.0.dtd
beans
bean id=httpScoreRepository class=com.nhnnext.score.scores.HttpScoreRepository /
bean id=standardGrader class=com.nhnnext.score.lecture.StandardGrader /
bean id=passFailGrader class=com.nhnnext.score.lecture.PassFailGrader /
bean id=standardGradeLecture class=com.nhnnext.score.lecture.Lecture
constructor-arg ref=standardGrader /
constructor-arg ref=httpScoreRepository /
/bean
/beans
Scores.xml
136. ?xml version=1.0 encoding=UTF-8?
!DOCTYPE beans PUBLIC -//SPRING//DTD BEAN 2.0//EN
http://www.springframework.org/dtd/spring-beans-2.0.dtd
beans
bean id=httpScoreRepository class=com.nhnnext.score.scores.HttpScoreRepository /
bean id=standardGrader class=com.nhnnext.score.lecture.StandardGrader /
bean id=passFailGrader class=com.nhnnext.score.lecture.PassFailGrader /
bean id=standardGradeLecture class=com.nhnnext.score.lecture.Lecture
constructor-arg ref=standardGrader /
constructor-arg ref=httpScoreRepository /
/bean
bean id=passFailGradeLecture class=com.nhnnext.score.lecture.Lecture
constructor-arg ref=passFailGrader /
constructor-arg ref=httpScoreRepository /
/bean
/beans
Sores.xml
138. Load Application Context
GenericXmlApplicationContext context =
new GenericXmlApplicationContext(classpath:scores.xml”);
Application
Context object
:Lecture
object
:Standard
Grader
object
:PassFail
Grader
object
:HttpScore
Repository
object
:Lecture
httpScore
Repository
passFail
Grader
standard
Grader
standard
Grade
Lecture
passFail
Grade
Lecture
139. Dependency Lookup
lecture = context.getBean(passFailGradeLecture,
Lecture.class);
Application
Context object
:Lecture
object
:Standard
Grader
object
:PassFail
Grader
object
:HttpScore
Repository
object
:Lecture
httpScore
Repository
passFail
Grader
passFail
Grade
Lecture
id
class
156. Constructor Injection
Discussion
bean id=standardGradeLecture
class=com.nhnnext.score.lecture.Lecture
constructor-arg ref=standardGrader /
constructor-arg ref=httpScoreRepository /
/bean
public Lecture(Grader grader, ScoreRepository scoreRepository) {
this.grader = grader;
this.scoreRepository = scoreRepository;
}
157. Setter Injection
Discussion
bean id=standardGradeLecture
class=com.nhnnext.score.lecture.Lecture
property name=grader ref=standardGrader /
property name=scoreRepository
ref=httpScoreRepository /
/bean
public void setGrader(Grader grader) {
this.grader = grader;
}
public void setScoreRepository(ScoreRepository scoreRepository) {
this.scoreRepository = scoreRepository;
}