Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 자바
- 김영한
- Proxy
- SpringBoot
- pointcut
- jpa
- AOP
- Servlet
- http
- db
- springdatajpa
- java
- Spring Boot
- 알고리즘
- 스프링
- transaction
- spring
- QueryDSL
- Greedy
- JDBC
- kotlin
- 스프링 핵심 기능
- 인프런
- 그리디
- Exception
- JPQL
- Android
- 스프링 핵심 원리
- Thymeleaf
- 백준
Archives
- Today
- Total
개발자되기 프로젝트
[Server] 단일책임 원칙 본문
기존 ApiController는 너무 많은 책임을 가지고 있었다.
단순히 api를 호출하는 기능 외에도,
api요청을 위한 req객체 생성 및 api에서 받은 data를 res객체로 변환하는책임도 가지고 있었다.
유지보수에도 번거로우며, 단일책임 원칙에도 부합하지 않는다.
Factory를 만들어 res, req객체 생성을 위임하도록 하자.
1. ResFactory
api 요청을 위한 req객체, api로부터 받은 json을기반으로 res객체를 만들어 model에 담는다.
public class ResFactory {
private SearchLocationReq searchLocationReq;
private Model model;
private GoogleClient googleClient;
private OdSayClient odSayClient;
@Autowired
public ResFactory(SearchLocationReq searchLocationReq, Model model, GoogleClient googleClient, OdSayClient odSayClient) {
this.searchLocationReq = searchLocationReq;
this.model = model;
this.googleClient = googleClient;
this.odSayClient = odSayClient;
}
public Model getResult(){
//이름 --> 좌표
JSONObject jsonStartResult = googleClient.searchLocation(searchLocationReq.getStart());
JSONObject jsonEndResult = googleClient.searchLocation(searchLocationReq.getEnd());
SearchRouteRes searchRouteRes1;
if (!searchLocationReq.getMiddle().isEmpty()){
JSONObject jsonMiddleResult = googleClient.searchLocation(searchLocationReq.getMiddle());
//시작 -> 경유, 경유 -> 시작 길찾기 req 객체 생성
SearchRouteReq searchRouteReq1 = getSearchRouteReq(jsonStartResult, jsonMiddleResult);
SearchRouteReq searchRouteReq2 = getSearchRouteReq(jsonMiddleResult, jsonEndResult);
searchRouteRes1 = searchRoute3(searchRouteReq1);
SearchRouteRes searchRouteRes2 = searchRoute3(searchRouteReq2);
model.addAttribute("result2", searchRouteRes2);
model.addAttribute("middle", searchLocationReq.getMiddle());
}else {
SearchRouteReq searchRouteReq1 = getSearchRouteReq(jsonStartResult, jsonEndResult);
searchRouteRes1 = searchRoute3(searchRouteReq1);
}
//view에 전달.
model.addAttribute("result1", searchRouteRes1);
model.addAttribute("start", searchLocationReq.getStart());
model.addAttribute("end", searchLocationReq.getEnd());
return model;
}
private SearchRouteReq getSearchRouteReq(JSONObject jsonStartResult, JSONObject jsonEndResult) {
double SX = (double) jsonStartResult.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location").get("lng");
double SY = (double) jsonStartResult.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location").get("lat");
double EX = (double) jsonEndResult.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location").get("lng");
double EY = (double) jsonEndResult.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location").get("lat");
SearchRouteReq searchRouteReq = new SearchRouteReq(String.valueOf(SX), String.valueOf(SY), String.valueOf(EX), String.valueOf(EY));
return searchRouteReq;
}
public SearchRouteRes searchRoute3(@ModelAttribute("searchRouteReq") SearchRouteReq searchRouteReq){
JSONObject jsonResult = odSayClient.searchRoute(searchRouteReq);
SearchRouteRes searchRouteRes = new SearchRouteRes(jsonResult, odSayClient);
ArrayList<Path> pathList = getPathList(searchRouteRes);
//api로 얻은 pathList를 대체함.
searchRouteRes.setPathList(pathList);
return searchRouteRes;
}
private ArrayList<Path> getPathList(SearchRouteRes searchRouteRes) {
//여러 path중 2개만 사용.
ArrayList<Path> pathList = new ArrayList<>();
pathList.add(searchRouteRes.getPathList().get(0));
pathList.add(searchRouteRes.getPathList().get(1));
//2개의 path에 대해 realtime 정보를 가져옴.
for (Path path : pathList) {
for (SubPath subPath : path.getSubPathList()) {
subPath.getStationIdAndRealTimeInfo(odSayClient);
}
}
return pathList;
}
}
2. ApiController
Controller는 controller의 책임만 가지고 있다.
controller 본연의 기능 외에 나머지 기능은 Factory로 위임함.
@Slf4j
@Controller
@RequiredArgsConstructor
public class ApiController {
private final OdSayClient odSayClient;
private final GoogleClient googleClient;
@GetMapping("/")
public String home4(Model model){
model.addAttribute("searchLocationReq", new SearchLocationReq());
return "searchRouteWithRealTimeInfo";
}
@PostMapping("/With-realTimeInfo")
public String searchRoute3(@ModelAttribute SearchLocationReq searchLocationReq, Model model){
ResFactory factory = new ResFactory(searchLocationReq, model, googleClient, odSayClient);
model = factory.getResult();
return searchLocationReq.getMiddle().isEmpty()? "resultPage3-1":"resultPage3";
}
}
3. GitHub: 220201 SRP
'Project > 대중교통 길찾기' 카테고리의 다른 글
[Server] Oday Error 처리2 (0) | 2022.02.04 |
---|---|
[Server] Oday Error 처리 (0) | 2022.02.03 |
[Server] 효율성 개선 (0) | 2022.01.31 |
[Server] 4단계: 실시간 도착정보 연동(버스) (0) | 2022.01.30 |
[Server] 3단계 경유지 검색 추가 (0) | 2022.01.29 |
Comments