클래스(모델(Medel))의 종류

강9·2023년 11월 27일
0

Java

목록 보기
42/71
post-thumbnail

💡 모델(Model)이란?

객체_Object(현실적)
= 클래스_class(프로그래밍적)
= 모델_Model(소프트웨어공학적)

  • 객체지향프로그래밍에서 클래스를 역할에 따라 부르는 이름
    모델(Model)이라고 한다.
  • 역할에 따라 다양한 모델을 만들어 사용할 수 있다.

🔖 클래스로 만들어 사용하게 될 모델의 종류

시작 클래스DTO, VO ModelDAO ModelUtility Model
시작 클래스(모델)데이터를 담는(이동) 모델데이터를 처리(DB)를 하는 모델도움(Utility)을 주는 모델
mainData Transfer Object
Value Object
Data Access ObjectHelper Object


1️⃣ DTO, VO 클래스

(Data Transfer Object, Value Object)

✅ DTO, VO 클래스가 필요한 이유

▶️ 데이터를 하나로 묶어야 할 경우(데이터를 하나로 수집하는 역할)


😢 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_
}

데이터량이 많을 경우 데이터 이동 시 개별로 이동이 어려움


😀 DTO, VO 클래스 생성

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_
}

✅ DTO, VO의 정보은닉(getter, setter)

📌 참고링크 : ↘︎_객체 접근 권한

상태정보(멤버변수)는 데이터를 넣고 빼야하니 접근제한자를 넣어줘야함

단, 상태정보는 중요하기에 정보은닉이 필요 (private)


2️⃣ DAO 클래스

(Data Access Object)

✅ DAO 클래스가 필요한 이유

▶️ 데이터베이스에 데이터(DTO, VO)를 저장, 수정하거나 검색, 삭제를 하기 위해서 만들어지는 모델(클래스)

  • CRUD 동작가지고 있는 클래스
  • 비즈니스 로직처리하는 클래스

1. 수집된 데이터를 데이터베이스에 저장, 수정

  • 저장 : insert
  • 수정 : update

2. 데이터베이스에 저장된 데이터를 검색, 삭제

  • 검색 : select
  • 삭제 : delete
CRUDSQL
Createinsert
Readselect
Updateupdate
Deletedelete

💡SQL : Structured Query Language (구조적 질의 언어)로, 관계형 데이터베이스 시스템(RDBMS)에서 자료를 관리 및 처리하기 위해 설계된 언어


3️⃣ Utility 클래스

(Helper Object)

✅ Utility 클래스가 필요한 이유

▶️ 반복적으로 사용해야 될 동작(기능)을 별도의 클래스로 만들어 놓고 필요할 때 사용하는 클래스

  • 꼭 필요한 것은 아니고, 필요하면 만들어서 사용하면 된다.
  • 자바에서도 java.util 패키지에 이러한 Utility 클래스가 많이 있다.

▶️ Utility, Helper, Facade (도움을 주는 클래스들)

1) 날짜, 통화, 시간 정보 획득 기능(동작)
2) 인코딩(한글 깨짐) 기능(동작)
3) DAO이외의 별도 기능(동작) -> main 메서드에서 분리해서 코딩

profile
코린이 일기

0개의 댓글