[Gofμ λμμΈν¨ν΄] λΉλ ν¨ν΄
π λΉλ ν¨ν΄μ΄λ?
- λ€μν ꡬμ±μ μΈμ€ν΄μ€λ€μ λμΌν κ³Όμ μ κ±°μ³ μμ±ν μ μλλ‘ νλ ν¨ν΄
π λΉλ ν¨ν΄μ νμμ±?
- 볡μ‘ν κ°μ²΄λ₯Ό μμ±νλ ν΄λμ€μ νννλ ν΄λμ€λ₯Ό λΆλ¦¬ν΄μ λμΌν μ μ°¨μμλ μλ‘ λ€λ₯Έ ννμ μμ±νλ λ°©λ²μ μ 곡
- μμ±ν΄μΌ νλ κ°μ²΄κ° optional ν μμ±μ λ§μ΄ κ°μ§ λ λ μ’μ
- νμμλ νλΌλ―Έν°λ€μ λν΄μ μΌμΌμ΄ null κ°μ λκΈ°μ§ μμλ λ¨
- Optional ν μΈμκ° λ§μμ§λ©΄ νμ κ³Ό μμμ λν κ΄λ¦¬κ° μ΄λ €μ΄λ° μ΄λ₯Ό ν΄κ²°ν΄μ€
π λΉλ ν¨ν΄ ꡬννκΈ°
public class Lecture {
String startTime;
String endTime;
String day;
String professorName;
String title;
String content;
String bookName;
String classRoom;}
λλ©μΈμ μμ κ°λ€. λ§μΌ κ°μμ μ± μ΄ νμνμ§ μκ±°λ, μ¨λΌμΈ μμ μ΄λΌμ classRoom μ΄ μ‘΄μ¬νμ§ μλλ€λ©΄ ν΄λΉ κ°λ€μ λͺ¨λ nullμ΄ λμ΄μΌ νλ€. λ°λΌμ μ μΈ΅μ μΈ μμ±μ ν¨ν΄μ μ΄μ©ν΄ μꡬμ¬νμ λ§μ‘±μν¬ μλ μλ€.
μ μΈ΅μ μΈ μμ±μ ν¨ν΄
- μ€λ²λ‘λ©μ ν΅ν΄μ μμ±μλ₯Ό μ¬λ¬κ° λ§λλ κ²
public Lecture(String startTime,
String endTime,
String day,
String professorName,
String title,
String content
) {
this.startTime = startTime;
this.endTime = endTime;
this.day = day;
this.professorName = professorName;
this.title = title;
this.content = content;
}
public Lecture(String startTime,
String endTime,
String day,
String professorName,
String title,
String content,
String bookName,
String classRoom) {
this.startTime = startTime;
this.endTime = endTime;
this.day = day;
this.professorName = professorName;
this.title = title;
this.content = content;
this.bookName = bookName;
this.classRoom = classRoom;
}
μμ κ°μ΄ Optional ν νλΌλ―Έν°λ€μ ν΄κ²°ν μ μλ€. κ·Έλ¬λ, μ΄μ κ°μ΄ μμ±μλ₯Ό μ μνκ² λλ€λ©΄ λ§€λ² μλ‘μ΄ μμ±μλ₯Ό λ§λ€κ±°λ null κ°μ μΌμΌμ΄ λ겨μ€μΌ νλ λ¬Έμ κ° μλ€. λν μΆν μ½λλ₯Ό λ³Ό λ μ΄λ€ νλμ κ°μ λ£μλμ§ νλμ μμ보기 μ΄λ ΅λ€. κ·Έλ λ€λ©΄ μΌμΌμ΄ λͺ¨λ setter λ₯Ό μ΄μ©νλ©΄ λμ§ μμκΉ?
μλ° λΉ ν¨ν΄
Lecture lecture = new Lecture();
lecture.setTitle("νκ΅ λ¬Ένμ¬");
lecture.setContent("νκ΅ λ¬Ένμ¬μ λν΄μ μ¨λΌμΈμΌλ‘ λ°°μλλ€.");
lecture.setBookName("νκ΅ λ¬Ένμ¬ II");
lecture.setProfessorName("κΉμ² μ");
μ΄λ μΌμΌμ΄ Null μ λκ²¨μ£Όμ§ μμλ λκ³ , μ νμ μΌλ‘ νλΌλ―Έν°λ₯Ό λκ²¨μ€ μ μκΈ°μ μ΄λ μ λμ λ¬Έμ λ ν΄κ²°λλ€.
κ·Έλ¬λ, ν¨μ νΈμΆμ΄ νλΌλ―Έν°μ κ°μλ§νΌ μ΄λ£¨μ΄μ§κ³ νλ²μΌλ‘ μμ±ν μ μλ€.
λν, immutable νμ§ μκΈ° λλ¬Έμ μΌκ΄μ±μ΄ κΉ¨μ§ μ°λ €κ° μ‘΄μ¬νλ€.
λΉλ ν¨ν΄
public interface LectureBuilder {
LectureBuilder startTime(String statTime);
LectureBuilder endTime(String endTime);
LectureBuilder day(String day);
LectureBuilder professorName(String professorName);
LectureBuilder title(String title);
LectureBuilder content(String content);
LectureBuilder bookName(String bookName);
LectureBuilder classRoom(String classRoom);
Lecture getInstance();
}
μμ λ€μ΄μ΄κ·Έλ¨μμ Builder μ ν΄λΉνλ μΈν°νμ΄μ€λ₯Ό μ μνλ€. μ΄λ κ°μ²΄λ₯Ό μμ±νκΈ° μν step μ μ μνλ κ²μ΄λ€.
λ°λΌμ λͺ¨λ λ©μλλ€μ λ°ν νμ μ΄ builder μΈ κ²μ΄λ€. builder μ λ°ννμ¬ κ³μν΄μ νλΌλ―Έν°μ κ°μ λ°λ κ²μ΄λ€.
public class DefaultLectureBuilder implements LectureBuilder{
String startTime;
String endTime;
String day;
String professorName;
String title;
String content;
String bookName;
String classRoom;
@Override
public LectureBuilder startTime(String statTime) {
this.startTime = statTime;
return this;
}
@Override
public LectureBuilder endTime(String endTime) {
this.endTime = endTime;
return this;
}
@Override
public LectureBuilder day(String day) {
this.day = day;
return this;
}
@Override
public LectureBuilder professorName(String professorName) {
this.professorName = professorName;
return this;
}
@Override
public LectureBuilder title(String title) {
this.title = title;
return this;
}
@Override
public LectureBuilder content(String content) {
this.content = content;
return this;
}
@Override
public LectureBuilder bookName(String bookName) {
this.bookName = bookName;
return this;
}
@Override
public LectureBuilder classRoom(String classRoom) {
this.classRoom = classRoom;
return this;
}
@Override
public Lecture getInstance() {
return new Lecture(startTime, endTime, day, professorName, title, content, bookName, classRoom
);
}
}
μμ λ€μ΄μ΄κ·Έλ¨μμ ConcreteBuilder μ ν΄λΉνλ€.
μ΄λ₯Ό λ°νμΌλ‘ κ°μ²΄λ₯Ό μμ±ν΄λ³΄μ.
Lecture first = new DefaultLectureBuilder().title("νκ΅ λ¬Ένμ¬μ μ΄ν΄")
.bookName("νκ΅ λ¬Ένμ¬ II")
.content("νκ΅μ λ¬Ένμ¬μ λν΄μ μ¨λΌμΈμΌλ‘ λ°°μλλ€.")
.professorName("κΉμ² μ")
.day("μ")
.startTime("13")
.endTime("15")
.getInstance();
μμ κ°μ΄ κ°μ²΄λ₯Ό μμ±ν΄μ£Όλ©΄ λλ€. . μ μ΄μ©νμ¬ κ³μ μ΄μ μ μλ μ΄μ λ λͺ¨λ μΈμλ€μ λ°νκ°μ΄ builder μ΄κΈ° λλ¬Έμ΄λ€.
λ§μ§λ§μλ getInstance λ©μλλ₯Ό νΈμΆνμ¬ κ°μ²΄λ₯Ό μμ±νλ€.
μμ λ€μ΄μ΄κ·Έλ¨μ ν΄λΉνλ Director λ₯Ό μ΄μ©νλ©΄ ν΄λΌμ΄μΈνΈ μ½λλ₯Ό μ€μΌ μ μλ€.
public class LectureDirector{
private LectureBuilder lectureBuilder;
public LectureDirector(LectureBuilder lectureBuilder){
this.lectureBuilder = lectureBuilder;
}
public Lecture communicativeEnglish(){
return lectureBuilder.professorName("κΉλ―Έμ")
.startTime("13")
.endTime("15")
.bookName("Communicative English part I")
.title("κ΅μ μμ΄")
.content("νμ κ΅μ μμ΄ μμ
μ
λλ€.")
.getInstance();
}
}
LectureDirector lectureDirector = new LectureDirector(new DefaultLectureBuilder());
Lecture newLecture = lectureDirector.communicativeEnglish();
π λΉλ ν¨ν΄μμ νμλ‘ μ λ ₯λ°μμΌ νλ νλΌλ―Έν° μ§μ νκΈ°
public DefaultLectureBuilder(String startTime,
String endTime,
String professorName){
this.startTime = startTime;
this.endTime = endTime;
this.professorName = professorName;
}
νμλ‘ μ λ ₯λ°μμΌ νλ νλΌλ―Έν°λ€μ λΉλμ μμ±μμ 미리 λ£μ΄λλ€. μ΄ν μ νμ μΌλ‘ μ λ ₯λ°λ νλΌλ―Έν°λ€μ λν΄μλ§ λΉλ ν¨ν΄μ μ μ©νλ€. μ΄λ μμ λμΌνλ μλ΅νκ² λ€.
π λΉλ ν¨ν΄μ μ₯μ /λ¨μ
μ₯μ
- κ°λ μ±μ΄ μ’λ€
- κ°μ²΄μ μΌκ΄μ±μ κΉ¨μ§ μμ μ μλ€
- μ μ°μ±μ ν보ν μ μλ€
- λΆλ³μ±μ ν보ν μ μλ€
- μμ°¨μ μΈ λ¨κ³λ₯Ό κ°μ ν μ μλ€.
//1
LectureBuilder title(String title);
//2
SecondLectureBuilder title(String title);
1λ²μ΄ μλ 2λ²μ²λΌ μλ‘μ΄ λΉλλ₯Ό λ°νκ°μΌλ‘ μ§μ νκ² λλ©΄ ν΄λΉ λΉλμ μ‘΄μ¬νλ λ©μλλ₯Ό μ¬μ©νλλ‘ κ°μ ν μ μλ€.
μ¦, κ°μ²΄μ title μ μ§μ νκ³ λμλ λ€λ₯Έ λΉλλ‘ λμ΄κ°λλ‘ νλ κ²μ΄λ€.
λ¨μ
- μΈμ€ν΄μ€λ₯Ό λ§λ€μ΄μ μ¬μ©ν΄μΌ νλ€
- λλ ν 리 κ΅¬μ‘°κ° λ³΅μ‘ν΄μ§ μ μλ€.
π μ€νλ§μμμ λΉλ ν¨ν΄
@Builder
@Builder
public class BuildMe {
private String username;
private int age;
}
μμ κ°μ΄ Builder μ΄λ Έν μ΄μ μ λΆμ΄λ©΄ μλμ κ°μ΄ μ»΄νμΌ μμ μ΄λ Έν μ΄μ μ΄ λ°μ΄νΈμ½λλ₯Ό λ°κΏμ€λ€.
public class BuildMe {
private String username;
private int age;
BuildMe(String username, int age) {
this.username = username;
this.age = age;
}
public static BuildMe.BuildMeBuilder builder() {
return new BuildMe.BuildMeBuilder();
}
public static class BuildMeBuilder {
private String username;
private int age;
BuildMeBuilder() {
}
public BuildMe.BuildMeBuilder username(String username) {
this.username = username;
return this;
}
public BuildMe.BuildMeBuilder age(int age) {
this.age = age;
return this;
public BuildMe build() {
return new BuildMe(this.username, this.age);
}
public String toString() {
return "BuildMe.BuildMeBuilder(username=" + this.username + ", age=" + this.age + ")";
}
}
}
π μ°Έκ³
https://dev-youngjun.tistory.com/197
λΉλ ν¨ν΄(Builder Pattern)
1. λΉλ ν¨ν΄μ΄λ? μμ±κ³Ό κ΄λ ¨λ λμμΈ ν¨ν΄μΌλ‘, λμΌν νλ‘μΈμ€λ₯Ό κ±°μ³ λ€μν ꡬμ±μ μΈμ€ν΄μ€λ₯Ό λ§λλ λ°©λ² 1-1. μ μ(Definition) GoF λμμΈ ν¨ν΄ μ€ μμ± ν¨ν΄μ ν΄λΉνλ€. λΉλ ν¨ν΄μ
dev-youngjun.tistory.com
https://velog.io/@park2348190/Lombok-Builder%EC%9D%98-%EB%8F%99%EC%9E%91-%EC%9B%90%EB%A6%AC
Lombok @Builderμ λμ μ리
보μΌλ¬νλ μ΄νΈ λ©μλ(getter/setter, constructor λ±)λ₯Ό μ§μ μμ±νμ§ μμλ λμ μμ±ν΄μ£Όλ Lombokλ₯Ό μ΅κ·Όμ λ§μ΄ νμ©νκ³ μλ€. κ·Έλλ§ setter λ©μλκ°μ κ²½μ°λ κ°μ λ³κ²½μν€λ λ©μλλ κ·Έ λͺ©
velog.io