객체_Object(현실적)
= 클래스_class(프로그래밍적)
= 모델_Model(소프트웨어공학적)
모델(Model)
이라고 한다.시작 클래스 | DTO, VO Model | DAO Model | Utility Model |
---|---|---|---|
시작 클래스(모델) | 데이터를 담는(이동) 모델 | 데이터를 처리(DB)를 하는 모델 | 도움(Utility)을 주는 모델 |
main | Data Transfer Object Value Object | Data Access Object | Helper Object |
✅ DTO, VO 클래스가 필요한 이유
▶️ 데이터를 하나로 묶어야 할 경우(데이터를 하나로 수집하는 역할)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("자동차 일련번호 : ");
int carSn = sc.nextInt();
System.out.print("자동차 이름 : ");
String carName = sc.nextLine();
sc.nextLine();
System.out.print("자동차 가격 : ");
int carPrice = sc.nextInt();
sc.nextLine();
System.out.print("자동차 소유자 : ");
String carOwner = sc.nextLine();
sc.nextLine();
System.out.print("자동차 연식 : ");
int carYear = sc.nextInt();
sc.nextLine();
System.out.print("자동차 타입 : "); // G(휘발유), D(경유)
String carType = sc.nextLine();
carInfoPrint(carSn,carName,carPrice,carOwner,carYear,carType);
} // main_
public static void carInfoPrint(int carSn, String carName, int carPrice, String carOwner, int carYear, String carType) {
System.out.println(car.carSn+"\t"+car.carName+"\t"+car.carPrice+"\t"+car.carOwner+"\t"+car.carYear+"\t"+car.carType);
} // carInfoPrint_
}
데이터량이 많을 경우 데이터 이동 시 개별로 이동이 어려움
public class CarDTO {
public int carSn;
public String carName;
public int carPrice;
public String carOwner;
public int carYear;
public String carType;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("자동차 일련번호 : ");
int carSn = sc.nextInt();
System.out.print("자동차 이름 : ");
String carName = sc.nextLine();
sc.nextLine();
System.out.print("자동차 가격 : ");
int carPrice = sc.nextInt();
sc.nextLine();
System.out.print("자동차 소유자 : ");
String carOwner = sc.nextLine();
sc.nextLine();
System.out.print("자동차 연식 : ");
int carYear = sc.nextInt();
sc.nextLine();
System.out.print("자동차 타입 : "); // G(휘발유), D(경유)
String carType = sc.nextLine();
CarDTO car = new CarDTO(); // 데이터를 묶어주는 작업
car.carSn=carSn;
car.carName=carName;
car.carPrice=carPrice;
car.carOwner=carOwner;
car.carYear=carYear;
car.carType=carType;
carInfoPrint(car);
} // main_
public static void carInfoPrint(CarDTO car) {
System.out.println(car.carSn+"\t"+car.carName+"\t"+car.carPrice+"\t"+car.carOwner+"\t"+car.carYear+"\t"+car.carType);
} // carInfoPrint_
}
📌 참고링크 : ↘︎_객체 접근 권한
상태정보(멤버변수)는 데이터를 넣고 빼야하니 접근제한자를 넣어줘야함
단, 상태정보는 중요하기에 정보은닉이 필요 (private)
✅ DAO 클래스가 필요한 이유
▶️ 데이터베이스에 데이터(DTO, VO)를 저장, 수정하거나 검색, 삭제를 하기 위해서 만들어지는 모델(클래스)
- CRUD 동작을 가지고 있는 클래스
- 비즈니스 로직을 처리하는 클래스
저장
: insert수정
: update검색
: select삭제
: deleteCRUD | SQL |
---|---|
Create | insert |
Read | select |
Update | update |
Delete | delete |
💡SQL
: Structured Query Language (구조적 질의 언어)로, 관계형 데이터베이스 시스템(RDBMS)에서 자료를 관리 및 처리하기 위해 설계된 언어
✅ Utility 클래스가 필요한 이유
▶️ 반복적으로 사용해야 될 동작(기능)을 별도의 클래스로 만들어 놓고 필요할 때 사용하는 클래스
- 꼭 필요한 것은 아니고, 필요하면 만들어서 사용하면 된다.
- 자바에서도
java.util
패키지에 이러한 Utility 클래스가 많이 있다.
1) 날짜, 통화, 시간 정보 획득 기능(동작)
2) 인코딩(한글 깨짐) 기능(동작)
3) DAO이외의 별도 기능(동작) -> main 메서드에서 분리해서 코딩