Builder Pattern ๐
์์ฑ์์ ๋ค์ด๊ฐ ๋งค๊ฐ ๋ณ์๊ฐ ๋ง๋ ์ ๋ ์ฐจ๋ก๋๋ก ๋งค๊ฐ ๋ณ์๋ฅผ ๋ฐ์๋ค์ด๊ณ ๋ชจ๋ ๋งค๊ฐ ๋ณ์๋ฅผ ๋ฐ์ ๋ค,
๋ณ์๋ค์ ํตํฉํด์ ํ ๋ฒ์ ์ฌ์ฉ
Why Use Builder Pattern?
//์กฐ๊ฑด : ์ด ๊ฐ์ฒด๋ ํ๋ฒ ์์ฑ๋๋ฉด ์ฝ๊ธฐ(Read)๋ง ๊ฐ๋ฅ
public class PersonInfo {
private String name;
private Integer age;
private String favoriteColor;
private String favoriteAnimal;
private Integer favoriteNumber;
public PersonInfo(String name, Integer age, String favoriteColor, String
favoriteAnimal, Integer favoriteNumber) {
super();
this.name = name;
this.age = age;
this.favoriteColor = favoriteColor;
this.favoriteAnimal = favoriteAnimal;
this.favoriteNumber = favoriteNumber;
}
public String getPersonInfo() {
String personInfo = String.format(
"name:%s, age:%d, favoriteColor:%s,
favoriteAnimal:%s, favoriteNumber:%d",
name, age, favoriteColor, favoriteAnimal, favoriteNumber);
return personInfo;
}
}
์์ ์กฐ๊ฑด์ ๊ณ ๋ คํ๋ฉด setter ๋ฉ์๋๋ ์กด์ฌ ๋ถ๊ฐ๋ฅ,
์์ฑ์๋ฅผ ํตํด์๋ง ๋ฐ์ดํฐ๋ฅผ ์
๋ ฅ๋ฐ์ ์ ์์ต๋๋ค.
์ด ๊ณผ์ ์์ ์์ฑ์๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅ๋ฐ์ ๋ ๋ค์ํ ์์ธ๐ค๋ค์ด ๋ฐ์ํฉ๋๋ค.
๋ค์ํ ์์ธ๋ค์ ์ ๋ถ ๊ณ ๋ คํด์ ์๋ก์ด ์์ฑ์๋ฅผ ๋ง๋ค๊ณ ํด๊ฒฐํ ์ ์์ต๋๋ค.
๋งค๋ฒ ์ฌ์ฉ์๊ฐ ์ฒดํฌํด์ผ ๋๋ ์ํฉ์ด ์ค๊ธฐ ๋๋ฌธ์
๋๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ Builder Pattern์ ์ฌ์ฉํฉ๋๋ค. ๐
Implement Builder Pattern
public class PersonInfoBuilder {
private String name;
private Integer age;
private String favoriteColor;
private String favoriteAnimal;
private Integer favoriteNumber;
public PersonInfoBuilder setName(String name) {
this.name = name;
return this;
}
public PersonInfoBuilder setAge(Integer age) {
this.age = age;
return this;
}
public PersonInfoBuilder setFavoriteColor(String favoriteColor) {
this.favoriteColor = favoriteColor;
return this;
}
public PersonInfoBuilder setFavoriteAnimal(String favoriteAnimal) {
this.favoriteAnimal = favoriteAnimal;
return this;
}
public PersonInfoBuilder setFavoriteNumber(Integer favoriteNumber) {
this.favoriteNumber = favoriteNumber;
return this;
}
public PersonInfo build() {
PersonInfo personInfo = new PersonInfo(name, age, favoriteColor,
favoriteAnimal, favoriteNumber);
return personInfo;
}
}
public class BuilderPattern {
public static void main(String[] args) {
PersonInfoBuilder personInfoBuilder = new PersonInfoBuilder();
PersonInfo result = personInfoBuilder.setName("Hong").setAge(24).
setFavoriteAnimal("cat").setFavoriteColor("black").build();
System.out.println(result.getPersonInfo());
}
}
์ ์ ๋์ํฉ๋๋ค.
// ์ ์ ์ค์ฒฉ ํด๋์ค ํํ(Static Nested Class)
public class PersonInfo {
private String name;
private Integer age;
private String favoriteColor;
private String favoriteAnimal;
private Integer favoriteNumber;
private PersonInfo(String name, Integer age, String favoriteColor, String favoriteAnimal,
Integer favoriteNumber) {
super();
this.name = name;
this.age = age;
this.favoriteColor = favoriteColor;
this.favoriteAnimal = favoriteAnimal;
this.favoriteNumber = favoriteNumber;
}
public String getPersonInfo() {
String personInfo = String.format("name:%s, age:%d, favoriteColor:%s,
favoriteAnimal:%s, favoriteNumber:%d",
name, age, favoriteColor, favoriteAnimal, favoriteNumber);
return personInfo;
}
public static class PersonInfoBuilder {
/* ๋ฐ์ ์๋ class์ ๋ณ์์ ๋ฉ์๋ ์ค์ static์ด ๋ถ์ ๊ฒ๋ค์ ์ฌ์ฉ ๊ฐ๋ฅ
*
* */
private String name;
private Integer age;
private String favoriteColor;
private String favoriteAnimal;
private Integer favoriteNumber;
public PersonInfoBuilder setName(String name) {
this.name = name;
return this;
}
public PersonInfoBuilder setAge(Integer age) {
this.age = age;
return this;
}
public PersonInfoBuilder setFavoriteColor(String favoriteColor) {
this.favoriteColor = favoriteColor;
return this;
}
public PersonInfoBuilder setFavoriteAnimal(String favoriteAnimal) {
this.favoriteAnimal = favoriteAnimal;
return this;
}
public PersonInfoBuilder setFavoriteNumber(Integer favoriteNumber) {
this.favoriteNumber = favoriteNumber;
return this;
}
public PersonInfo build() {
PersonInfo personInfo = new PersonInfo(name, age, favoriteColor,
favoriteAnimal,
favoriteNumber);
return personInfo;
}
}
}
public class BuilderPattern {
public static void main(String[] args) {
// PersonInfo class์ ๊ฐ์ฒด๊ฐ ์์ด๋ PersonInfoBuilder class์ ๊ฐ์ฒด ์์ฑ์ด ๊ฐ๋ฅ
PersonInfo.PersonInfoBuilder personInfoBuilder = new PersonInfo.PersonInfoBuilder();
PersonInfo result = personInfoBuilder.setAge(23).setName("Hong").
setFavoriteColor("black")
.setFavoriteAnimal("cat").build();
System.out.println(result.getPersonInfo());
}
}
์ด ๊ฒฝ์ฐ์๋ ์ ์ ๋์ํฉ๋๋ค.
Closing Remarks
์ดํด๋ฅผ ๋๊ธฐ ์ํด ๋ค์ด์ด๊ทธ๋จ์ ์ฒจ๋ถํ๊ฒ ์ต๋๋ค.