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();
}
κΈ°λ³Έ λ„λ‘œ ν‘œμ‹œ

κΈ°λ³Έ λ„λ‘œ ν‘œμ‹œ
κ΅ν†΅λŸ‰ ν‘œμ‹œ

κΈ°λ³Έ λ„λ‘œ ν‘œμ‹œ
κ΅ν†΅λŸ‰ ν‘œμ‹œ
ꡐ차료 ν‘œμ‹œ