6_oHji
6_oHji
ν™˜μ˜ν•©λ‹ˆλ‹€☺
GitHub   Gmail
전체 방문자
였늘
μ–΄μ œ
  • λΆ„λ₯˜ 전체보기 (62)
    • πŸ’‘ DEEP DIVE (17)
      • PATTERN (17)
    • πŸ“ 끄적끄적 (4)
    • 🌈 EXPERIENCE (3)
    • πŸ’» PROJECT (3)
    • πŸ“’ STUDY (34)
      • ALGORITHM (1)
      • JAVA (5)
      • FRONT-END (7)
      • BACK-END (10)
      • SPRING (7)
      • NETWORK (3)
      • CLOUD (1)

λΈ”λ‘œκ·Έ 메뉴

  • ν™ˆ
  • νƒœκ·Έ

곡지사항

인기 κΈ€

졜근 λŒ“κΈ€

졜근 κΈ€

ν‹°μŠ€ν† λ¦¬

κ°œμΈμ •λ³΄

  • ν‹°μŠ€ν† λ¦¬ ν™ˆ
  • μŠ€ν† λ¦¬
  • 포럼
  • 둜그인
hELLO Β· Designed By μ •μƒμš°.
6_oHji
6_oHji
Deep Dive Into Design Patterns - λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄
πŸ’‘ DEEP DIVE/PATTERN

Deep Dive Into Design Patterns - λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄

2024. 5. 27. 23:58

πŸ’‘ λ°μ½”λ ˆμ΄ν„°(Decorator) νŒ¨ν„΄μ΄λž€?

λŒ€μƒ 객체에 λŒ€ν•œ κΈ°λŠ₯ ν™•μž₯μ΄λ‚˜ 변경이 ν•„μš”ν•  λ•Œ 객체의 κ²°ν•©(μƒˆλ‘œμš΄ 행동을 ν¬ν•¨ν•œ 특수 래퍼 객체듀 내에 μΆ”κ°€)을 톡해 μ„œλΈŒν΄λž˜μ‹± λŒ€μ‹  μ“Έ 수 μžˆλŠ” ꡬ쑰적 λ””μžμΈ νŒ¨ν„΄μž…λ‹ˆλ‹€.

 

λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄μ„ μ΄μš©ν•˜λ©΄ ν•„μš”ν•œ μΆ”κ°€ κΈ°λŠ₯의 쑰합을 λŸ°νƒ€μž„μ—μ„œ λ™μ μœΌλ‘œ 생성할 수 μžˆμŠ΅λ‹ˆλ‹€. λŒ€μƒ 객체λ₯Ό μƒˆλ‘œμš΄ 행동듀을 ν¬ν•¨ν•œ λ°μ½”λ ˆμ΄ν„° 객체에 λ„£μ–΄μ„œ 행동듀을 ν•΄λ‹Ή μž₯μ‹μž κ°μ²΄λ§ˆλ‹€ μ—°κ²°μ‹œν‚΅λ‹ˆλ‹€. μ„œλΈŒν΄λž˜μŠ€λ‘œ ꡬ성할 λ•Œ 보닀 훨씬 μœ μ—°ν•˜κ²Œ κΈ°λŠ₯을 ν™•μž₯ ν•  수 있고, κΈ°λŠ₯을 κ΅¬ν˜„ν•˜λŠ” ν΄λž˜μŠ€λ“€μ„ λΆ„λ¦¬ν•¨μœΌλ‘œμ¨ μˆ˜μ •μ΄ μš©μ΄ν•΄μ§‘λ‹ˆλ‹€.

 

πŸ’‘ 클래슀 λ‹€μ΄μ–΄κ·Έλž¨μœΌλ‘œ λ³Έ λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄

  1. Component : 원본 객체와 μž₯μ‹λœ 객체 λͺ¨λ‘λ₯Ό λ¬ΆλŠ” μ—­ν• .
  2. Concrete Component : 원본 객체. (λ°μ½”λ ˆμ΄νŒ… ν•  객체 = λž˜ν•‘λ˜λŠ” 객체)
  3. Base Decorator : μΆ”μƒν™”λœ λ°μ½”λ ˆμ΄ν„° 클래슀. 원본 객체λ₯Ό ν•©μ„±(composition)ν•œ wrappee ν•„λ“œμ™€ μΈν„°νŽ˜μ΄μŠ€μ˜ κ΅¬ν˜„ λ©”μ†Œλ“œλ₯Ό κ°€μ§€κ³  μžˆμŠ΅λ‹ˆλ‹€.
  4. Concrete Decorator : ꡬ체적인 λ°μ½”λ ˆμ΄ν„° 클래슀. λΆ€λͺ¨ ν΄λž˜μŠ€κ°€ 감싸고 μžˆλŠ” ν•˜λ‚˜μ˜ Componentλ₯Ό ν˜ΈμΆœν•˜λ©΄μ„œ 호좜 μ „/ν›„λ‘œ 뢀가적인 λ‘œμ§μ„ μΆ”κ°€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

 

πŸ’‘ λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄ κ΅¬ν˜„

// 원본 객체와 μž₯μ‹λœ 객체 λͺ¨λ‘λ₯Ό λ¬ΆλŠ” μΈν„°νŽ˜μ΄μŠ€
interface IComponent {
    void operation();
}

// μž₯식될 원본 객체
class ConcreteComponent implements IComponent {
    public void operation() {
    }
}

// μž₯μ‹μž 좔상 클래슀
abstract class Decorator implements IComponent {
    IComponent wrappee; // 원본 객체λ₯Ό composition

    Decorator(IComponent component) {
        this.wrappee = component;
    }

    public void operation() {
        wrappee.operation(); // μœ„μž„
    }
}

// μž₯μ‹μž 클래슀
class ComponentDecorator1 extends Decorator {

    ComponentDecorator1(IComponent component) {
        super(component);
    }

    public void operation() {
        super.operation(); // 원본 객체λ₯Ό μƒμœ„ 클래슀의 μœ„μž„μ„ 톡해 μ‹€ν–‰ν•˜κ³ 
        extraOperation(); // μž₯식 클래슀만의 λ©”μ†Œλ“œλ₯Ό μ‹€ν–‰ν•œλ‹€.
    }

    void extraOperation() {
    }
}

class ComponentDecorator2 extends Decorator {

    ComponentDecorator2(IComponent component) {
        super(component);
    }

    public void operation() {
        super.operation(); // 원본 객체λ₯Ό μƒμœ„ 클래슀의 μœ„μž„μ„ 톡해 μ‹€ν–‰ν•˜κ³ 
        extraOperation(); // μž₯식 클래슀만의 λ©”μ†Œλ“œλ₯Ό μ‹€ν–‰ν•œλ‹€.
    }

    void extraOperation() {
    }
}
public class Client {
    public static void main(String[] args) {
        // 1. 원본 객체 생성
        IComponent obj = new ConcreteComponent();

        // 2. μž₯식 1 ν•˜κΈ°
        IComponent deco1 = new ComponentDecorator1(obj);
        deco1.operation(); // μž₯μ‹λœ 객체의 μž₯μ‹λœ κΈ°λŠ₯ μ‹€ν–‰

        // 3. μž₯식 2 ν•˜κΈ°
        IComponent deco2 = new ComponentDecorator2(obj);
        deco2.operation(); // μž₯μ‹λœ 객체의 μž₯μ‹λœ κΈ°λŠ₯ μ‹€ν–‰

        // 4. μž₯식 1 + 2 ν•˜κΈ°
        IComponent deco3 = new ComponentDecorator1(new ComponentDecorator2(obj));
    }
}

 

πŸ’‘ 마무리

λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄μ€

  1. 객체 μ±…μž„κ³Ό 행동이 λ™μ μœΌλ‘œ 상황에 따라 λ‹€μ–‘ν•œ κΈ°λŠ₯이 λΉˆλ²ˆν•˜κ²Œ μΆ”κ°€/μ‚­μ œλ  λ•Œ
  2. 객체의 결합을 톡해 κΈ°λŠ₯이 생성될 수 μžˆμ„ λ•Œ
  3. 객체λ₯Ό μ‚¬μš©ν•˜λŠ” μ½”λ“œλ₯Ό μ†μƒμ‹œν‚€μ§€ μ•Šκ³  λŸ°νƒ€μž„μ— 객체에 μΆ”κ°€ λ™μž‘μ„ ν• λ‹Ήν•  수 μžˆμ–΄μ•Ό ν•  λ•Œ
  4. 상속을 톡해 μ„œλΈŒν΄λž˜μ‹±μœΌλ‘œ 객체의 λ™μž‘μ„ ν™•μž₯ν•˜λŠ” 것이 μ–΄μƒ‰ν•˜κ±°λ‚˜ λΆˆκ°€λŠ₯ ν•  λ•Œ

에 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

 

λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄μ„ μ‚¬μš©ν•˜λ©΄ λŸ°νƒ€μž„μ— κ°μ²΄λ“€μ—μ„œλΆ€ν„° μ±…μž„λ“€μ„ μΆ”κ°€ν•˜κ±°λ‚˜ μ œκ±°ν•  수 있고, 객체λ₯Ό μ—¬λŸ¬ λ°μ½”λ ˆμ΄ν„°λ‘œ λž˜ν•‘ν•˜μ—¬ μ—¬λŸ¬ 행동듀을 ν•©μ„±ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

 

각 μž₯μ‹μž ν΄λž˜μŠ€λ§ˆλ‹€ 고유의 μ±…μž„μ„ κ°€μ§€κ³ (SRP μ€€μˆ˜), ν΄λΌμ΄μ–ΈνŠΈ μ½”λ“œ μˆ˜μ •μ—†μ΄ κΈ°λŠ₯ ν™•μž₯이 ν•„μš”ν•˜λ©΄ μž₯μ‹μž 클래슀λ₯Ό μΆ”κ°€ν•˜λ©΄ λ©λ‹ˆλ‹€.(OCP μ€€μˆ˜) λ˜ν•œ, κ΅¬ν˜„μ²΄κ°€ μ•„λ‹Œ μΈν„°νŽ˜μ΄μŠ€λ₯Ό λ°”λΌλ³΄λŠ” ꡬ쑰둜, DIPλ₯Ό λ§Œμ‘±ν•©λ‹ˆλ‹€.

 

 

 

λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄

/ λ””μžμΈ νŒ¨ν„΄λ“€ / ꡬ쑰 νŒ¨ν„΄ λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄ λ‹€μŒ μ΄λ¦„μœΌλ‘œλ„ λΆˆλ¦½λ‹ˆλ‹€: 래퍼(Wrapper), Decorator μ˜λ„ λ°μ½”λ ˆμ΄ν„°λŠ” 객체듀을 μƒˆλ‘œμš΄ 행동듀을 ν¬ν•¨ν•œ 특수 래퍼 객체듀 내에 λ„£μ–΄μ„œ μœ„ 행동듀을

refactoring.guru

 

πŸ’  λ°μ½”λ ˆμ΄ν„°(Decorator) νŒ¨ν„΄ - μ™„λ²½ λ§ˆμŠ€ν„°ν•˜κΈ°

Decorator Pattern λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄(Decorator Pattern)은 λŒ€μƒ 객체에 λŒ€ν•œ κΈ°λŠ₯ ν™•μž₯μ΄λ‚˜ 변경이 ν•„μš”ν• λ•Œ 객체의 결합을 톡해 μ„œλΈŒν΄λž˜μ‹± λŒ€μ‹  μ“Έμˆ˜ μžˆλŠ” μœ μ—°ν•œ λŒ€μ•ˆ ꡬ쑰 νŒ¨ν„΄μ΄λ‹€. Decorator을 ν•΄μ„ν•˜

inpa.tistory.com

 

λ°˜μ‘ν˜•
μ €μž‘μžν‘œμ‹œ λΉ„μ˜λ¦¬ λ³€κ²½κΈˆμ§€ (μƒˆμ°½μ—΄λ¦Ό)
    'πŸ’‘ DEEP DIVE/PATTERN' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
    • Deep Dive Into Design Patterns - μ»€λ§¨λ“œ νŒ¨ν„΄
    • Deep Dive Into Design Patterns - μ „λž΅ νŒ¨ν„΄
    • Deep Dive Into Design Patterns - μƒνƒœ νŒ¨ν„΄
    • Deep Dive Into Design Patterns - ν…œν”Œλ¦Ώ λ©”μ„œλ“œ νŒ¨ν„΄
    6_oHji
    6_oHji
    끄적끄적

    ν‹°μŠ€ν† λ¦¬νˆ΄λ°”