โ๏ธ ์๋ฅผ ๋ค์ด ์คํ๋ฒ ์ค ์๋ฃ ์ฃผ๋ฌธ์ ๋ค ์ ์๋ค.
์คํ๋ฒ ์ค์์ ์๋ฃ๋ฅผ ์ฃผ๋ฌธํ ๋ ์ผ๋ฐ ์๋ฉ๋ฆฌ์นด๋ ธ ์ด์ธ์๋ ์ท ์ถ๊ฐ, ์ฐ์ ์ข ๋ฅ ๋ณ๊ฒฝ, ์๋ฝ ์ถ๊ฐ, ์ผ์ ์ ์กฐ์ ๋ฑ ์ฌ๋๋ง๋ค ์ฌ๋ฌ ์ต์ ์ ์ ํํ ์ ์๋ค. ์ด๋ ์ฌ๋์ ์ท์ ์ถ๊ฐํด๋ฌ๋ผ๊ณ ํ ์ ์๊ณ , ๋ ์ด๋ค ์ฌ๋์ ์ฐ์ ๋ฅผ ์คํธ๋ฐํฌ๋ก ๋ฐ๊ฟ๋ฌ๋ผ๊ณ ํ ์๋ ์๋ค. ์ด์ฒ๋ผ ์ ํ์ ์ต์ ๋ค์ ๋ณด๋ค ์ ์ฐํ๊ฒ ๋ฐ์ ๋ค์ํ ํ์ ์ ์ธ์คํด์ค๋ฅผ ์์ฑํ ์ ์์ด, ํด๋์ค์ ์ ํ์ ๋งค๊ฐ๋ณ์๊ฐ ๋ง์ ์ํฉ์์ ์ ์ฉํ๊ฒ ์ฌ์ฉํ๊ธฐ ์ํด ๋น๋ ํจํด์ ์ฌ์ฉํ๋ค.
๋น๋ ํจํด ์ ์ฉ X ,์์ฑ์ ์ค๋ฒ๋ก๋ฉ๋ง ์ฌ์ฉ
class Coffee {
// ํ์
private String type; // ์: "์๋ฉ๋ฆฌ์นด๋
ธ", "๋ผ๋ผ" ๋ฑ
private int size; // ์: 1 (Short), 2 (Tall), 3 (Grande)
// ์ ํ
private boolean extraShot; // ์ท ์ถ๊ฐ ์ฌ๋ถ
private String milk; // ์ฐ์ ์ข
๋ฅ (์: "๊ธฐ๋ณธ์ฐ์ ", "์คํธ๋ฐํฌ", "๋์ ")
private boolean noIce; // ์ผ์ ์ ์ธ ์ฌ๋ถ
private String syrup; // ์๋ฝ ์ข
๋ฅ (์: "๋ฐ๋๋ผ", "ํค์ด์ฆ๋", "์์")
// ๋ชจ๋ ์ต์
์ ์ง์ ํ๋ ์์ฑ์
public Coffee(String type, int size, boolean extraShot, String milk, boolean noIce, String syrup) {
this.type = type;
this.size = size;
this.extraShot = extraShot;
this.milk = milk;
this.noIce = noIce;
this.syrup = syrup;
}
// ์๋ฝ ์ต์
์์ด (๊ธฐ๋ณธ๊ฐ "์์"์ผ๋ก ์ค์ )
public Coffee(String type, int size, boolean extraShot, String milk, boolean noIce) {
this(type, size, extraShot, milk, noIce, "์์");
}
// ์ผ์ ์ต์
์์ด (๊ธฐ๋ณธ noIce = false, ์๋ฝ "์์")
public Coffee(String type, int size, boolean extraShot, String milk) {
this(type, size, extraShot, milk, false, "์์");
}
// ์ฐ์ ์ต์
์์ด (๊ธฐ๋ณธ milk = "๊ธฐ๋ณธ์ฐ์ ", noIce = false, ์๋ฝ "์์")
public Coffee(String type, int size, boolean extraShot) {
this(type, size, extraShot, "๊ธฐ๋ณธ์ฐ์ ", false, "์์");
}
@Override
public String toString() {
return String.format("์ปคํผ ํ์
: %s, ์ฌ์ด์ฆ: %d, ์ท ์ถ๊ฐ: %s, ์ฐ์ : %s, ์ผ์ ์ ์ธ: %s, ์๋ฝ: %s",
type, size, extraShot ? "์" : "์๋์ค", milk, noIce ? "์" : "์๋์ค", syrup);
}
public static void main(String[] args) {
// ๋ชจ๋ ์ต์
์ด ์ง์ ๋ ์ปคํผ ์ฃผ๋ฌธ
Coffee coffee1 = new Coffee("์๋ฉ๋ฆฌ์นด๋
ธ", 3, true, "์คํธ๋ฐํฌ", true, "๋ฐ๋๋ผ");
// ์ท ์ถ๊ฐ๋ง ์ง์ ํ ๊ธฐ๋ณธ ์ปคํผ ์ฃผ๋ฌธ
Coffee coffee2 = new Coffee("์๋ฉ๋ฆฌ์นด๋
ธ", 2, true);
// ์ฐ์ ๋ณ๊ฒฝ๊ณผ ์ผ์ ์ ์ธ ์ต์
๋ง ์ถ๊ฐํ ์ปคํผ ์ฃผ๋ฌธ
Coffee coffee3 = new Coffee("๋ผ๋ผ", 2, false, "๋์ ", true);
System.out.println(coffee1);
System.out.println(coffee2);
System.out.println(coffee3);
}
}
๋น๋ ํจํด์ ์ด๋ฌํ ๋ฌธ์ ๋ค์ ํด๊ฒฐํ๊ธฐ ์ํด ๋ณ๋์ Builder ํด๋์ค๋ฅผ ๋ง๋ค์ด ๋ฉ์๋๋ฅผ ํตํด step-by-step ์ผ๋ก ๊ฐ์ ์ ๋ ฅ๋ฐ์ ํ์ ์ต์ข ์ ์ผ๋ก build() ๋ฉ์๋๋กย ํ๋์ ์ธ์คํด์ค๋ฅผ ์์ฑํ์ฌ ๋ฆฌํดํ๋ ํจํด
public static void main(String[] args) {
// ์์ฑ์ ๋ฐฉ์
Coffee coffee1 = new Coffee("์๋ฉ๋ฆฌ์นด๋
ธ", 3, true, "์คํธ๋ฐํฌ", true, "๋ฐ๋๋ผ");
// ๋น๋ ๋ฐฉ์
Coffee coffee2 = new Coffee.Builder("๋ผ๋ผ", 2)
.extraShot(true)
.milk("๋์ ")
.noIce(true)
.build();
}
public class Coffee {
private String type; // ์: "์๋ฉ๋ฆฌ์นด๋
ธ", "๋ผ๋ผ" ๋ฑ
private int size; // ์: 1 (Short), 2 (Tall), 3 (Grande)
private boolean extraShot; // ์ท ์ถ๊ฐ ์ฌ๋ถ
private String milk; // ์ฐ์ ์ข
๋ฅ (์: "๊ธฐ๋ณธ์ฐ์ ", "์คํธ๋ฐํฌ", "๋์ ")
private boolean noIce; // ์ผ์ ์ ์ธ ์ฌ๋ถ
private String syrup; // ์๋ฝ ์ข
๋ฅ (์: "๋ฐ๋๋ผ", "ํค์ด์ฆ๋", "์์")
public static Builder builder() {
return new Builder();
}
// private ์์ฑ์ (๋น๋๋ฅผ ํตํด์๋ง ๊ฐ์ฒด ์์ฑ)
private Coffee(Builder builder) {
this.type = builder.type;
this.size = builder.size;
this.extraShot = builder.extraShot;
this.milk = builder.milk;
this.noIce = builder.noIce;
this.syrup = builder.syrup;
}
// ๋ด๋ถ ๋น๋ ํด๋์ค
public static class Builder {
private String type;
private int size;
private boolean extraShot;
private String milk;
private boolean noIce;
private String syrup;
public Builder type(String type) {
this.type = type;
return this;
}
public Builder size(int size) {
this.size = size;
return this;
}
public Builder extraShot(boolean extraShot) {
this.extraShot = extraShot;
return this;
}
public Builder milk(String milk) {
this.milk = milk;
return this;
}
public Builder noIce(boolean noIce) {
this.noIce = noIce;
return this;
}
public Builder syrup(String syrup) {
this.syrup = syrup;
return this;
}
public Coffee build() {
return new Coffee(this);
}
}
public String getType() {
return type;
}
public int getSize() {
return size;
}
public boolean isExtraShot() {
return extraShot;
}
public String getMilk() {
return milk;
}
public boolean isNoIce() {
return noIce;
}
public String getSyrup() {
return syrup;
}
@Override
public String toString() {
return String.format("์ปคํผ [ํ์
=%s, ์ฌ์ด์ฆ=%d, ์ท ์ถ๊ฐ=%s, ์ฐ์ =%s, ์ผ์ ์ ์ธ=%s, ์๋ฝ=%s]",
type, size, extraShot ? "์" : "์๋์ค", milk, noIce ? "์" : "์๋์ค", syrup);
}
}
public static void main(String[] args) {
Coffee coffee1 = new Coffee("์๋ฉ๋ฆฌ์นด๋
ธ", 3, true, "์คํธ๋ฐํฌ", true, "๋ฐ๋๋ผ");
// ๋น๋ ๋ฐฉ์์ผ๋ก ๋ง๋ ์ปคํผ ๊ฐ์ฒด
Coffee coffee2 = Coffee.builder()
.type("๋ผ๋ผ")
.size(2)
.extraShot(true)
.milk("๋์ ")
.noIce(true)
.syrup("ํค์ด์ฆ๋")
.build();
}
๋น๋ ํจํด์ ์์์ ์ผ๋ก ๊ตฌํํ ๋๋ ์ค์ ํ๊ณ ์ ํ๋ ๊ฐ ํ๋์ ๋ํด ๋ฉ์๋๋ฅผ ํ๋์ฉ ๋ง๋ค์ด์ค์ผํ๋ค. ๋ฐ๋ผ์ ๊ฐ๋ฐ์๊ฐ ์ข๋ ํธํ๊ฒ ๋น๋ ํจํด์ ์ด์ฉํ๊ธฐ ์ํค Lombok์์๋ ๋ณ๋์ ์ด๋ ธํ ์ด์ ์ ์ง์ํ๋ค.
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;
@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@ToString
public class Coffee {
private String type; // ์: "์๋ฉ๋ฆฌ์นด๋
ธ", "๋ผ๋ผ" ๋ฑ
private int size; // ์: 1 (Short), 2 (Tall), 3 (Grande)
private boolean extraShot; // ์ท ์ถ๊ฐ ์ฌ๋ถ
private String milk; // ์ฐ์ ์ข
๋ฅ (์: "๊ธฐ๋ณธ์ฐ์ ", "์คํธ๋ฐํฌ", "๋์ ")
private boolean noIce; // ์ผ์ ์ ์ธ ์ฌ๋ถ
private String syrup; // ์๋ฝ ์ข
๋ฅ (์: "๋ฐ๋๋ผ", "ํค์ด์ฆ๋", "์์")
}
public static void main(String[] args) {
Coffee coffee2 = Coffee.builder()
.type("๋ผ๋ผ")
.size(2)
.extraShot(true)
.milk("๋์ ")
.noIce(true)
.syrup("ํค์ด์ฆ๋")
.build();
System.out.println(coffee2);
}
@AllArgsConstructor(access = AccessLevel.PRIVATE) : @Builder์ ํจ๊ป ์ฌ์ฉ๋ ๋ ์ฃผ๋ก ์ ์ธ๋๋ฉฐ, ์ ์ฒด ํ๋๋ฅผ ์ธ์๋ก ๋ฐ๋ ์์ฑ์๋ฅผ ์๋์ผ๋ก ๋ง๋ค๋, ์ด๋ฅผ private ์์ฑ์๋ก ์ ํํ๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์ธ๋ถ์์ ์์ฑ์๋ฅผ ์ง์ ํธ์ถํ ์ ์๊ณ , Builder๋ฅผ ํตํด์๋ง ๊ฐ์ฒด๋ฅผ ์์ฑํ๋๋ก ์ ๋ํ๋ค.๐ค
@NoArgsConstructor(access = AccessLevel.PROTECTED)๋ ๋น๋ ํจํด์ ๊ผญ ํ์ํ ๊ฑด ์๋์ง๋ง, JPA๋ ์ํฐํฐ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ๋ ์ ์ ํตํด ์์ฑํ๊ธฐ ๋๋ฌธ์ ๊ธฐ๋ณธ ์์ฑ์๊ฐ ๋ฐ๋์ ํ์ํ๋ค.
ํ์ง๋ง ์๋ฌด๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๊ฒ ํ๋ฉด ์ค๊ณ์ ๋ฌธ์ ๊ฐ ์๊ธธ ์ ์์ผ๋ฏ๋ก,
์์ฑ์์ ์ ๊ทผ ๋ฒ์๋ฅผprotected๋ก ์ ํํด ์ธ๋ถ์์๋ ์ง์ ์์ฑํ์ง ๋ชปํ๊ฒ ๋ง๋๋ค.
์ฐธ๊ณ :