스프링에서 빌더 사용하는 방법은 아주 간단하다!
바로 lombok annotation을 사용하면 된다.
@Builder만 생성자에 붙여준다면 손쉽게 만들 수 있다.
바로 아래 코드처럼 말이다.
@Builder
public Community(String name, String introduce, String communityImg, Integer headCount, Category category, List<String> days, String time, Type type) {
this.name = name;
this.introduce = introduce;
this.communityImg = communityImg;
this.headCount = headCount;
this.category = category;
this.days = days;
this.time = time;
this.type = type;
}
이는 객체를 생성하는 코드를 더 깔끔하게 보이도록 만든다.
community = Community.builder()
.name(reqDto.getName())
.introduce(reqDto.getIntroduce())
.communityImg(imgUrl)
.headCount(reqDto.getHeadCount())
.category(reqDto.getCategory())
.days(reqDto.getDay())
.time(reqDto.getTime())
.type(Type.mapPost)
.build();