Decorator Pattern
ꡬ쑰μ ν¨ν΄μ€μ νλλ‘ κ°μ²΄μ κ²°ν©μ ν΅ν΄ κΈ°λ₯μ λμ μΌλ‘ μ μ°νκ² νμ₯ν¨
ν΄λμ€λ κ°μ²΄λ₯Ό μ‘°ν©ν΄ λ ν° κ΅¬μ‘°λ₯Ό λ§λλ ν¨ν΄μ΄λ€.
μλ₯Ό λ€λ©΄ μλ‘λ€λ₯Έ μΈν°νμ΄μ€λ₯Ό μ§λ 2κ°μ κ°μ²΄λ₯Ό λ¬Άμ΄ λ¨μΌ μΈν°νμ΄μ€λ‘ μ 곡νλμ§ νλ λ°©μμ΄λ€.
μλ μμλ κΈ°λ³Έ λλ‘λ₯Ό νμνλ κΈ°λ₯μ ν΄λμ€λ₯Ό λ°μ½λ μ΄μ ν¨ν΄μΌλ‘ ꡬμ±νλ λ°©μμ΄λ€.
public class DisplayRoad {
public void draw() {
System.out.println("κΈ°λ³Έ λλ‘ νμ");
}
}
public class DisplayWithRoad extends DisplayRoad {
public void draw() {
super.draw();
drawTraffic();
}
public void drawTraffic() {
System.out.println("κ΅ν΅λ νμ");
}
}
public class DisplayWithRoadTraffic extends DisplayWithRoad {
public void draw() {
super.draw();
crossing();
}
public void crossing() {
System.out.println("κ΅μ°¨λ£ νμ");
}
}
public void decorator() {
DisplayRoad road = new DisplayRoad();
road.draw();
DisplayRoad roadWith = new DisplayWithRoad();
roadWith.draw();
DisplayRoad roadWithTraffic = new DisplayWithRoadTraffic();
roadWithTraffic.draw();
}
κΈ°λ³Έ λλ‘ νμ
κΈ°λ³Έ λλ‘ νμ
κ΅ν΅λ νμ
κΈ°λ³Έ λλ‘ νμ
κ΅ν΅λ νμ
κ΅μ°¨λ£ νμ
β Template Method Proxy Pattern β