밑의 코드에서 ???부분이 아무클래스나 수용되게하고싶습니다
정확히말하면 생성자의 인자를 보면 ??? dto로 되있는대 제가만든 3가지정도의 클래스
(ReviewDTO, QADTO등등)가 올수있습니다. 거기서 받은 클래스를 밑의 ???에 전부 적용시키고싶은거죠. 약간 c++의 템플릿비슷한 기능을 구현하고싶습니다.
저는 약간 생성자부분을
public CommunityDAO(Object ob) {
if(ob.getClass().getSimpleName().equals("ReviewDTO")){
}elseif(ob.getClass().getSimpleName().equals("QADTO")){
smc = SQLMapConfig.getSqlMapInstance();
}
이런식으로 변형시켜 각각의 이프문안에 무엇을 넣어서 해볼까 도전했는대 좀안되더라구요 ㅠ
==========================================================
package community.dao;
import java.sql.SQLException;
import java.util.List;
import com.ibatis.sqlmap.client.SqlMapClient;
import community.dto.FaqDTO;
import community.dto.ReviewDTO;
import community.dto.StartEnd;
import iba.SQLMapConfig;
public class CommunityDAO {
SqlMapClient smc;
public CommunityDAO(??? dto) {
smc = SQLMapConfig.getSqlMapInstance();
}
public List<???> selectAll(){
List<???> list = null;
try {
list = smc.queryForList("faq.selectAll");
} catch (SQLException e) {
e.printStackTrace();
}
return list;
} //게시판에 모든 글 보여주는
public boolean insert(??? dto){
try {
smc.insert("faq.insert", dto);
return true;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
} // 사용자가 글작성
public boolean delete(int no){
return false;
} // 사용자가 글 작성
public FaqDTO selectTitle(int no){
??? dto;
try {
dto = (FaqDTO) smc.queryForObject("faq.select", no);
return dto;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public List<???> selectPage(StartEnd se){
List<???> list = null;
try {
list = smc.queryForList("faq.selectPage", se);
return list;
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
밑의 DTO를 보면 각클래스에서 선언한 변수, 메소드의 인자가 다른대 이런경우에도 알려주신방법을 쓸수있나요??
-----------------------------3개의 DTO클래스입니당--------------------------------------
package community.dto;
import java.sql.Date;
public class FaqDTO {
int no;
String title;
Date gdate;
String contents;
String writer;
String visible;
String password;
String category;
public FaqDTO() {}
public FaqDTO(int no, String title, Date gdate, String contents, String writer, String visible, String password,
String category) {
this.no = no;
this.title = title;
this.gdate = gdate;
this.contents = contents;
this.writer = writer;
this.visible = visible;
this.password = password;
this.category = category;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getGdate() {
return gdate;
}
public void setGdate(Date gdate) {
this.gdate = gdate;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getVisible() {
return visible;
}
public void setVisible(String visible) {
this.visible = visible;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
----------------------------------------------------------------------------------------
package community.dto;
import java.sql.Date;
public class NoticeDTO {
int no;
String title;
Date gdate;
String contents;
public NoticeDTO() {}
public NoticeDTO(int no, String title, Date gdate, String contents) {
this.no = no;
this.title = title;
this.gdate = gdate;
this.contents = contents;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getGdate() {
return gdate;
}
public void setGdate(Date gdate) {
this.gdate = gdate;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
}
-------------------------------------------------------------------------------
package community.dto;
import java.sql.Date;
public class ReviewDTO {
int no;
String title;
Date gdate;
String contents;
String writer;
String image;
public ReviewDTO() {}
public ReviewDTO(int no, String title, Date gdate, String contents, String writer, String image) {
this.no = no;
this.title = title;
this.gdate = gdate;
this.contents = contents;
this.writer = writer;
this.image = image;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getGdate() {
return gdate;
}
public void setGdate(Date gdate) {
this.gdate = gdate;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
--------------------------------------------------------------------------------------------
자바에서 지원 되는 제네릭과 인터페이스를 사용하면 가능 합니다.
위에 수정으로 질문다시했는대 ㅠㅠ 밑에부분만 봐주실수있나요?
프로그램 설계에 따라 공통 모듈이 구성 되긴 하지만 DAO를 굳이 공통화 할 필요는 없어 보입니다.
공통 개발이 주요한 경우는 동일한 요구사항의 기능을 추상화 할 수 있을 때 입니다.
보통 서비스 코드는 요구 사항 변경이 빈번해서 추상화 하기 어려운 부분이 있습니다.
제 개인적인 판단으로는 CommunityDAO가 제공하는 메서드를 다양하게 구성하는게 어떨까요?
그러면 될거같네요! 감사합니다`