데이터를 하나로 묶어서 (담아) 이동시킬 때 사용하기 위해 만들어지는 Model.
import com.example.object.Car;
import java.util.Scanner;
public class CarTest {
// Car에 관련된 시작 클래스. ∵시작 메서드인 main이 있는 Class이므로
public static void main(String[] args){
Scanner input = new Scanner(System.in); // Scanner 역시 기본 라이브러리에 있는 Utility Model
Car car = new Car();
System.out.print("자동차 일련번호 입력: ");
car.carSn = input.nextInt();
input.nextLine(); // 버퍼 비우기
System.out.print("자동차 이름 입력: ");
car.carName = input.nextLine();
System.out.print("자동차 가격 입력: ");
car.carPrice = input.nextInt();
input.nextLine(); // 버퍼 비우기
System.out.print("자동차 소유자 입력: ");
car.carOwner = input.nextLine();
System.out.print("자동차 연식 입력: ");
car.carYear = input.nextInt();
input.nextLine(); // 버퍼 비우기
System.out.print("자동차 타입 입력: ");
car.carType = input.nextLine();
input.close(); // 스트림 닫기
printCarInfoUsingCarDTO(car);
}
public static void printCarInfoUsingCarDTO(Car car){
System.out.println( "Car{" +
"carSn=" + car.carSn +
", carName='" + car.carName + '\'' +
", carPrice=" + car.carPrice +
", carOwner='" + car.carOwner + '\'' +
", carYear=" + car.carYear +
", carType='" + car.carType + '\'' +
'}');
}
public static void printCarInfoUsingCarArgs(int carSn, String carName, int carPrice, String carOwner, int carYear, String carType){
System.out.println( "Car{" +
"carSn=" + carSn +
", carName='" + carName + '\'' +
", carPrice=" + carPrice +
", carOwner='" + carOwner + '\'' +
", carYear=" + carYear +
", carType='" + carType + '\'' +
'}');
}
}
Car 객체를 만들어서 여러 정보를 하나로 묶어 printCarInfoUsingCarDTO 메서드로 전달하는 방식이다. 이렇게 객체를 묶어서 전달하는 것은, 하나하나 인자를 전달하는 것보다 더 깔끔하고 효율적인 방식이다.UserDTO라는 객체로 사용자 정보를 담아 가져오는 방식은 여러 정보를 한번에 전달할 수 있어 효율적이다.DTO와 VO는 객체를 설계할 때 매우 유용한 디자인 패턴으로, DTO는 데이터를 전달하는 역할을 하고, VO는 변하지 않는 데이터를 나타내는 역할을 하여 각자의 목적에 맞게 구분해서 사용하는 것이 좋다.