티스토리 뷰

Servlet JSP

서비스 클래스 구현하기

Programmers 2021. 5. 19. 15:55
반응형

목표: 서비스 클래스의 큰 틀을 구현한다.

 

클래스 생성

클래스를 생성하면서 package를 추가한다.

코드:

 - NoticeService.java

package com.newlecture.web.service;

import java.util.List;

import com.newlecture.web.entity.Notice;

public class NoticeService {
	public List<Notice> getNoticeList(){	
		return getNoticeList("title", "", 1);
	}

	public List<Notice> getNoticeList(int page){		
		return getNoticeList("title", "", page);
	}

	public List<Notice> getNoticeList(String field, String query, int page){	
		return null;
	}
	
	public int getNoticeCount() {	
		return getNoticeCount("title", "");
	}
	
	public int getNoticeCount(String field, String query) {	
		return 0;
	}
	
	public Notice getNotice(int id) {	
		return null;
	}
	
	public Notice getNextNotice(int id) {	
		return null;
	}

	public Notice getPrevNotice(int id) {
		return null;
	}
}

필요한 모든 함수들을 NoticeService클래스에 구현

모든 함수들을 다~ 구현할 필요는 없다.
그리고 같은 이름의 함수들을 재 호출을 통해서 구현하면 훨씬 간결하고 바른 코드가 된다.

예시)

	public List<Notice> getNoticeList(){	
		return getNoticeList("title", "", 1);
	}

	public List<Notice> getNoticeList(int page){		
		return getNoticeList("title", "", page);
	}

	public List<Notice> getNoticeList(String field, String query, int page){	
		return null;
	}

*중요: 여기서 위의 getNoticeList 세 개 중 맨 아래의 함수만 구현하고 나머지는 기본값을 넣어 반환하면 된다!

 

결과:
일단은 큰 틀만 구현했다. 만약 모든 함수들이 구현된다면 Controller에서는 가져다 쓰기만 하면 된다.

반응형
댓글
공지사항