Java
 Web
 
 
Programming
 Advanced
 
 
Week02
 
Spring
Framework
 
조영호
 
snatcher@nhn.com
 
https://github.com/eternity-next/score-spring.git
score-client-01
 
지난
 시간
 
class
Lecture
Class
StandardGrader
Class
PassFailGrader
interface
Grader
Dependency
Use
Client
Dependency
Use
Create
Dependency
inject
Dependency
오늘
 
class
Lecture
Class
StandardGrader
Class
PassFailGrader
interface
GraderUse
Client
Use
Create
inject
class
ScoreRepositoryUse
오늘
 
class
Lecture
interface
Grader
class
ScoreRepository Score
Server
2) HTTP
1) score 조회
3) JSON
4) score 전달
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
 
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
 
Problem
Testability
Transitive Dependency
 
Hidden Dependency
 
Transitive Dependency
 
B CA
Object A
  Object C
 Object B
 
Object A
  Object B
 depends on
 
Object C
 Object B
  depends on
 
Object A
  Object C
 depends on
 
class
Lecture
interface
Grader
class
ScoreRepository Score
Server
LectureTest
Transitive Dependency
 
LectureTest
  Score Server
 depends on
 
class
Lecture
interface
Grader
class
ScoreRepository Score
Server
LectureTest
Server Down
 
LectureTest
  Score Server
 depends on
 
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
 
Tight Coupling
class
Lecture
interface
Grader
class
ScoreRepository Score
Server
LectureTest
Test Fail
 
LectureTest

[JWAP-2] DI & Spring