빌더 → 계속 짓는 것 쌓아나가는 것
복잡한 객체들을 단계별로 생성할 수 있도록 하는 생성 디자인 패턴입니다. 이 패턴을 사용하면 같은 제작 코드를 사용하여 객체의 다양한 유형들과 표현을 제작할 수 있습니다.
기본 생성자의 단점
- 내가 넣어야할 필드의 순서를 기억해야 한다
- 생성자 내용 중 1개만 넣고 싶다면 오버로딩해서 만들어줘야 함
빌더 패턴의 좋은점
- 넣는 순서가 상관이 없다
- 넣고 싶은 것만 넣을 수 있다
- 가독성이 좋아진다(실수를 줄일 수 있다)
- 즉 기존 생성자의 단점을 극복함
추가 개념
int를 초기화 안 하면(값을 안 넣으면) 0이다 null이 들어가는게 맞다 그래서 (데이터 덩어리 만들 때는 Wrapper클래스를 만드는게 좋다)
//데이터만 들고 있는 애들 데이터 덩어리(클래스 자료형)(이름 정해져 있지 않다) 객체가 아니다 (상태 행위 신경 쓰지말고)
//DB에서는 bean 테이블과 동일한 데이터 타입이면 엔티티(릴레이션이라 함) 혹 VO라고 한다(value object)
bean
엔티티(개체)
엔터티는 데이터베이스 테이블?
실제 DB의 릴레이션과 1대1로 매핑되는 클래스다
릴레이션
관계형 데이터베이스에서 정보를 구분하여 저장하는 기본 단위이다. 한마디로 릴레이션은 DB 테이블이다.
vo(Value Object)
값 객체의 의미를 가진다(값을 표현하는게 목적이다)
연습해보자
원래는
public class Person {
private int id;
private String name;
private int age;
private String email;
public Person(){}
public Person(int id, String name, int age, String email) {
this.id = id;
this.name = name;
this.age = age;
this.email = email;
}
}
public class App {
//Person이라는 애가 있을 때 초기화 안됨
// Person p1 = new Person();
public static void main(String[] args) {
//생성자 불편함? 어느 자리에 무엇이 있는지 알아야한다.
//결국 실수하게 된다.
Person p1 = new Person(
1, "홍길동", 20, "ckddbsdbs123@naver.com"
);
}
}
외부에서는 p1이라 불렀는데 Person나에게는 뭐라 하냐? this로
만약 p2로 불렀다면 여기서도 this로 한다. 만약 extend 부모가 있다면 super()
public class App {
//Person이라는 애가 있을 때 초기화 안됨
// Person p1 = new Person();
public static void main(String[] args) {
//스태틱 이기 때문에 builder사용가능
Person p1 = Person.builder().id(1).name("홍길동").age(20).email("ckddbsdbs123@naver.com");
// 안됨 Person p1 = new Person();
}
}
package ex09;
public class Person {
private int id;
private String name;
private int age;
private String email;
private Person(){}
//app이라는 스태틱 공간, builder 스태틱 공간 main 스태틱 공간이 시작 전에 다 뜬다
public static Person builder(){
return new Person();
}
//세터와 비슷해 보임
public Person id(int id){
//this는 힙이고 주소 그대로 가지고 온다
this.id = id;
return this;
}
//만약 보이드면 세터가 되어 return 안되서 문제가 됨
public Person name(String name){
this.name = name;
return this;
}
public Person age(int age){
this.age = age;
return this;
}
public Person email(String email){
this.email = email;
return this;
}
}
이후 person 게터 만듬
이러면 0이 뜬다 (없으면 null이 들어가는게 맞는데…)
public class App {
public static void main(String[] args) {
//스태틱 이기 때문에 builder사용가능
Person p1 = Person.builder().id(1).name("홍길동").email("ckddbsdbs123@naver.com");
System.out.println(p1.getAge());
}
}
결과는 0
그래서 Integer 사용한다
package ex09;
public class Person {
private Integer id;
private String name;
private Integer age;
private String email;
private Person(){}
public static Person builder(){
return new Person();
}
public Person id(Integer id){
this.id = id;
return this;
}
public Person name(String name){
this.name = name;
return this;
}
public Person age(Integer age){
this.age = age;
return this;
}
public Person email(String email){
this.email = email;
return this;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public String getEmail() {
return email;
}
}
하면 null 나온다
@Builder로 사용가능하다 짱 좋음
Share article