Swift μ½λκ° λ©μΈμ΄μ§λ§, λ¬Έλ²μ μ°¨μ΄λ‘ μΈν΄ μ€λͺ μ λ μ ν©ν μ μλ Java μ½λλ ν¬ν¨λμ΄ μμ΅λλ€.
λ±μ₯ λ°°κ²½
λ§μ½ νλ‘νΌν°κ° λ§κ³ μμ±ν λ κΌ νμνμ§ μμ νλ‘νΌν°, μ¦ μ νμ νλ‘νΌν°κ° μ‘΄μ¬νλ€κ³ ν λ κ°μ²΄λ₯Ό μ΄λ»κ² μμ±ν΄μΌ ν κΉ? μ΄κΈ°μλ μμ±μλ₯Ό μ€λ²λ‘λ©νμ¬ λ€μν λ§€κ° λ³μμ λμν μ μλ μ μΈ΅μ μμ±μ ν¨ν΄μ΄λΌλ λ°©μμΌλ‘ ν΄κ²°νμλ€.
μ μΈ΅μ μμ±μ ν¨ν΄
μ½λ μμ
class Hamburger {
// νμ νλ‘νΌν°
private int bun;
private int patty;
// μ νμ νλ‘νΌν°
private int cheese = 0;
private int lettuce = 0;
private int tomato = 0;
private int bacon = 0;
public Hamburger(int bun, int patty, int cheese, int lettuce, int tomato, int bacon) {
this.bun = bun;
this.patty = patty;
this.cheese = cheese;
this.lettuce = lettuce;
this.tomato = tomato;
this.bacon = bacon;
}
public Hamburger(int bun, int patty, int cheese, int lettuce, int tomato) {
this.bun = bun;
this.patty = patty;
this.cheese = cheese;
this.lettuce = lettuce;
this.tomato = tomato;
}
public Hamburger(int bun, int patty, int cheese, int lettuce) {
this.bun = bun;
this.patty = patty;
this.cheese = cheese;
this.lettuce = lettuce;
}
public Hamburger(int bun, int patty, int cheese) {
this.bun = bun;
this.patty = patty;
this.cheese = cheese;
}
...
}
public static void main(String[] args) {
// λͺ¨λ μ¬λ£κ° μλ νλ²κ±°
Hamburger hamburger1 = new Hamburger(2, 1, 2, 4, 6, 8);
// λΉ΅κ³Ό ν¨ν° μΉμ¦λ§ μλ νλ²κ±°
Hamburger hamburger2 = new Hamburger(2, 1, 1);
// λΉ΅κ³Ό ν¨ν° λ² μ΄μ»¨λ§ μλ νλ²κ±°
Hamburger hamburger3 = new Hamburger(2, 0, 0, 0, 0, 6); // 0μ κ΅³μ΄ λ£μ΄λ λμ§ μλ λ°μ΄ν°
}
μ μΈ΅μ μμ±μ ν¨ν΄μ μμ±μμ νμν 맀κ°λ³μκ° λ§μ κ²½μ° λͺ λ²μ§Έ μΈμκ° μ΄λ€ νλ‘νΌν°μ ν΄λΉνλμ§ μκΈ° μ΄λ ΅κ³ , μΈμ μμμ λ°λΌμΌ νκΈ° λλ¬Έμ μ νμ νλ‘νΌν°μ ν΄λΉνλ 맀κ°λ³μμ μ λ£μ΄λ λ λ°μ΄ν°λ₯Ό μ΅μ§λ‘ λ£μ΄μΌ νλ μν©μ΄ μ°μΆλλ€.
μλ°λΉμ¦ ν¨ν΄
μ½λ μμ
class Hamburger {
// νμ νλ‘νΌν°
private int bun;
private int patty;
// μ νμ νλ‘νΌν°
private int cheese = 0;
private int lettuce = 0;
private int tomato = 0;
private int bacon = 0;
public Hamburger() {}
public void setBun(int bun) {
this.bun = bun;
}
public void setPatty(int patty) {
this.patty = patty;
}
public void setCheese(int cheese) {
this.cheese = cheese;
}
public void setLettuce(int lettuce) {
this.lettuce = lettuce;
}
public void setTomato(int tomato) {
this.tomato = tomato;
}
public void setBacon(int bacon) {
this.bacon = bacon;
}
}
public static void main(String[] args) {
// λͺ¨λ μ¬λ£κ° μλ νλ²κ±°
Hamburger hamburger1 = new Hamburger();
hamburger1.setBun(2);
hamburger1.setPatty(1);
hamburger1.setCheese(2);
hamburger1.setLettuce(4);
hamburger1.setTomato(6);
hamburger1.setBacon(8);
// λΉ΅κ³Ό ν¨ν° μΉμ¦λ§ μλ νλ²κ±°
Hamburger hamburger2 = new Hamburger();
hamburger2.setBun(2);
hamburger2.setPatty(1);
hamburger2.setCheese(2);
// λΉ΅κ³Ό ν¨ν° λ² μ΄μ»¨λ§ μλ νλ²κ±°
Hamburger hamburger3 = new Hamburger();
hamburger3.setBun(2);
hamburger2.setPatty(1);
hamburger3.setBacon(8);
}
μ μΈ΅μ μμ±μ ν¨ν΄μ λ¬Έμ μ μ ν΄κ²°νκΈ° μν΄ Setterλ₯Ό μ¬μ©ν μλ°λΉμ¦(Java Beans) ν¨ν΄μ΄ λ±μ₯νμλ€. μ΄λ¬ν ν¨ν΄μ΄ λ±μ₯νλ©΄μ κ°λ μ± λ¬Έμ κ° ν΄κ²°λκ³ , setterλ₯Ό ν΅ν΄μ μνλ μ νμ νλ‘νΌν°μλ§ κ°μ λ£μ΄μ€ μ μκ² λμλ€.
νμ§λ§, νμ νλ‘νΌν°μ κ° μ€μ μ λν λΆλΆμ΄ 보μ₯λμ§ μκ³ , setter λ©μλκ° μΈλΆμ μΌλ‘ 곡κ°λμ΄ μμ΄ κ°μ²΄μ λΆλ³μ±μ 보μ₯νμ§ λͺ»νλ€. λν, κ°μ²΄κ° νμν λͺ¨λ λ°μ΄ν°λ₯Ό λ°κΈ° μ μ κ°μ²΄λ₯Ό μ¬μ©νκ² λλ κ²μ λ§μ μ μλ€.
λΉλ ν¨ν΄
λΉλ ν¨ν΄μ μ μΈ΅μ μμ±μ ν¨ν΄κ³Ό μλ°λΉμ¦ ν¨ν΄μ λ¬Έμ μ μ ν΄κ²°νκΈ° μν΄ λ³λμ Builer κ°μ²΄λ₯Ό μμ±νκ³ λ©μλ 체μ΄λμ νμ©νμ¬ μνλ μ νμ νλ‘νΌν°μ κ°μ ν λΉ ν λ§μ§λ§μ build λ©μλλ₯Ό ν΅ν΄ κ°μ²΄λ₯Ό μμ±νλ ν¨ν΄μ΄λ€.
μ½λ μμ
class HamburgerBuilder {
// νμ νλ‘νΌν°
private var bun: Int
private var patty: Int
// μ νμ νλ‘νΌν°
private var cheese: Int = 0
private var lettuce: Int = 0
private var tomato: Int = 0
private var bacon: Int = 0
init(_ bun: Int, _ patty: Int) {
self.bun = bun
self.patty = patty
}
func cheese(_ cheese: Int) -> HamburgerBuilder {
self.cheese = cheese
return self
}
func lettuce(_ lettuce: Int) -> HamburgerBuilder {
self.lettuce = lettuce
return self
}
func tomato(_ tomato: Int) -> HamburgerBuilder {
self.tomato = tomato
return self
}
func bacon(_ bacon: Int) -> HamburgerBuilder {
self.bacon = bacon
return self
}
func build() -> Hamburger {
return Hamburger(bun, patty, cheese, lettuce, tomato, bacon)
}
}
let hamburger = HamburgerBuilder(2, 1).cheese(2).bacon(1).build()
μ₯μ
μ΄κΈ°ν λ©μλμ 맀κ°λ³μκ° λ§μ κ²½μ°μλ κ°λ μ±μ ν΄μΉμ§ μκ³ , νμ νλ‘νΌν°μ μ νμ νλ‘νΌν°λ₯Ό λΆλ¦¬ν μ μλ€.
λν, νλ‘νΌν° κ° μ€μ μ μν λ©μλκ° κ³΅κ°λμ΄ μμ΄λ μ΄λκΉμ§λ Builder κ°μ²΄μ νλ‘νΌν°λ₯Ό μ‘°μνλ κ²μ΄μ§ μ€μ κ°μ²΄μ νλ‘νΌν°λ₯Ό μ‘°μνλ κ²μ΄ μλλ―λ‘ μ€μ κ°μ²΄μ λΆλ³μ±μ 보μ₯ν μ μλ€.
λ§μ§λ§μΌλ‘, Builerμ build λ©μλκ° μ€νλκΈ° μ κΉμ§λ μ€μ κ°μ²΄κ° μμ±λλ κ²μ΄ μλκΈ° λλ¬Έμ μνλ κ°μ λ€ μ€μ ν΄ μ£ΌκΈ° μ κΉμ§ κ°μ²΄ μμ±μ μ§μ°μν¬ μ μλ€.
λ¨μ
μ€μ κ°μ²΄λ₯Ό μν Builder κ°μ²΄λ₯Ό νλμ© λ§λ€μ΄μΌ νλ―λ‘ κ΄λ¦¬ν΄μΌ ν ν΄λμ€κ° λ§μμ§κ³ 볡μ‘μ±μ΄ μ¦κ°ν μ μλ€.
λν, Builder κ°μ²΄μ μ½λκ° μ₯ν©νκΈ° λλ¬Έμ ν΄λμ€μ νλ‘νΌν° κ°μκ° 4κ°λ³΄λ€ μ κ±°λ κ°μμ λν λ³κ²½ κ°λ₯μ±μ΄ μλ κ²½μ°μλ μμ±μλ μ μ ν©ν 리 λ©μλλ₯Ό μ΄μ©νλ κ²μ΄ λ μ’λ€.
π Swiftμμμ λΉλ ν¨ν΄
Swiftμμλ 맀κ°λ³μ λ μ΄λΈμ΄λ λν΄νΈ 맀κ°λ³μμ μ‘΄μ¬λ‘ μΈν΄ λΉλ ν¨ν΄μ νμμ±μ΄ λ€λ₯Έ μΈμ΄λ€μ λΉν΄ λ¨μ΄μ§λ νΈμ΄λ€.
'CS > λμμΈ ν¨ν΄' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[GoF λμμΈ ν¨ν΄] - μ±κΈν€ ν¨ν΄ (0) | 2024.12.02 |
---|---|
[GoF λμμΈ ν¨ν΄] - νλ‘ν νμ ν¨ν΄ (0) | 2024.11.29 |
[GoF λμμΈ ν¨ν΄] - μΆμ ν©ν 리 λ©μλ ν¨ν΄ (2) | 2024.11.26 |
[GoF λμμΈ ν¨ν΄] - ν©ν 리 λ©μλ ν¨ν΄ (1) | 2024.11.25 |
[GoF λμμΈ ν¨ν΄] - λμμΈ ν¨ν΄μ΄λ? (1) | 2024.11.22 |
λκΈ