Builder Interface
package me.hsnam.builder;
import java.time.LocalDate;
public interface PhotoBuilder {
PhotoBuilder name(String name);
PhotoBuilder createDate(LocalDate createDate);
PhotoBuilder updateDate(LocalDate updateDate);
PhotoBuilder description(String description);
PhotoBuilder photoInfo(String name, String description);
PhotoBuilder etcInfo(String etc, int view);
Photo build();
}
package me.hsnam.builder;
import java.time.LocalDate;
public class DefaultPhotoBuilder implements PhotoBuilder{
private String name;
private LocalDate createdDate;
private LocalDate updateDate;
private String description;
private String etc;
private int view;
@Override
public PhotoBuilder name(String name) {
this.name = name;
return this;
}
@Override
public PhotoBuilder createDate(LocalDate createdDate) {
this.createdDate = createdDate;
return this;
}
@Override
public PhotoBuilder updateDate(LocalDate updateDate) {
this.updateDate = updateDate;
return this;
}
@Override
public PhotoBuilder description(String description) {
this.description = description;
return this;
}
@Override
public PhotoBuilder photoInfo(String name, String description) {
this.name = name;
this.description = description;
return this;
}
@Override
public PhotoBuilder etcInfo(String etc, int view) {
this.etc = etc;
this.view = view;
return this;
}
@Override
public Photo build() {
return new Photo(name, createdDate, updateDate, description, etc, view);
}
}
package me.hsnam.builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.time.LocalDate;
@Getter
@Setter
@ToString
public class Photo {
private String name;
private LocalDate createdDate;
private LocalDate updateDate;
private String description;
private String etc;
private int view;
public Photo(String name, LocalDate createdDate, LocalDate updateDate, String description, String etc, int view) {
this.name = name;
this.createdDate = createdDate;
this.updateDate = updateDate;
this.description = description;
this.etc = etc;
this.view = view;
}
}
package builder;
import me.hsnam.builder.DefaultPhotoBuilder;
import me.hsnam.builder.Photo;
import me.hsnam.builder.PhotoBuilder;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
public class BuilderTest {
@Test
@DisplayName("Builder 패턴 테스트")
public void builderTest() {
PhotoBuilder photoBuilder = new DefaultPhotoBuilder();
Photo photo = photoBuilder
.name("test")
.createDate(LocalDate.of(2022, 02, 14))
.updateDate(LocalDate.of(2022, 02, 15))
.etcInfo("info", 2)
.build();
System.out.println(photo);
}
}