본문 바로가기
객체지향/디자인패턴

데코레이터 패턴

by 정재익 2026. 2. 18.

GOF 디자인 패턴의 구조 패턴중 하나이다.

프록시 패턴과 유사하지만 프록시 패턴을 통해 부가기능을 추가하는 것을 데코레이터 패턴이라고 한다.

 

프록시와 데코레이더는 의도가 다르다. 프록시 패턴은 접근 제어를 위한 대리자 데코레이터는 객체에 추가 책임을 동적으로 추가하는 것이다.

 

기존 코드

public interface Component {
    String operation();
}
@Slf4j
public class DecoratorPatternClient {

    private Component component;

    public DecoratorPatternClient(Component component) {
        this.component = component;
    }

    public void execute() {
        String result = component.operation();
        log.info("result={}", result);
    }
}
@Slf4j
public class RealComponent implements Component {
    @Override
    public String operation() {
      log.info("RealComponent 실행");
      return "data";
    }
}
@Slf4j
public class DecoratorPatternTest {

    @Test
    void noDecorator() {
        Component realComponent = new RealComponent();
        DecoratorPatternClient client = new DecoratorPatternClient(realComponent);
        client.execute();
    }

 

 

부가기능을 추가한다.

@Slf4j
public class MessageDecorator implements Component {
    private Component component;

    public MessageDecorator(Component component) {
        this.component = component;
    }

    @Override
    public String operation() {
        log.info("MessageDecorator 실행");
        String result = component.operation();
        String decoResult = "*****" + result + "*****";
        log.info("MessageDecorator 꾸미기 적용 전={}, 적용 후={}", result, decoResult);
        return decoResult;
    }
}
@Test
void decorator1() {
    Component realComponent = new RealComponent();
    Component messageDecorator = new MessageDecorator(realComponent);
    DecoratorPatternClient client = new DecoratorPatternClient(messageDecorator);
    client.execute();
}
21:14:11.644 [main] INFO com.example.pureproxy.decorator.code.MessageDecorator -- MessageDecorator 실행
21:14:11.653 [main] INFO com.example.pureproxy.decorator.code.RealComponent -- RealComponent 실행
21:14:11.654 [main] INFO com.example.pureproxy.decorator.code.MessageDecorator -- MessageDecorator 꾸미기 적용 전=data, 적용 후=*****data*****
21:14:11.658 [main] INFO com.example.pureproxy.decorator.code.DecoratorPatternClient -- result=*****data*****

 

실행시간을 측정하는 기능도 추가한다. 프록시는 체인이 될 수 있다. 계속 부가기능을 꾸미는 것이다.

 

@Slf4j
public class TimeDecorator implements Component {
    private Component component;

    public TimeDecorator(Component component) {
        this.component = component;
    }

    @Override
    public String operation() {
        log.info("TimeDecorator 실행");
        long startTime = System.currentTimeMillis();
        String result = component.operation();
        long endTime = System.currentTimeMillis();
        long resultTime = endTime - startTime;
        log.info("TimeDecorator 종료 resultTime={}ms", resultTime);
        return result;
    }
}
@Test
void decorator2() {
    Component realComponent = new RealComponent();
    Component messageDecorator = new MessageDecorator(realComponent);
    Component timeDecorator = new TimeDecorator(messageDecorator);
    DecoratorPatternClient client = new DecoratorPatternClient(timeDecorator);
    client.execute();
}
21:17:38.219 [main] INFO com.example.pureproxy.decorator.code.TimeDecorator -- TimeDecorator 실행
21:17:38.225 [main] INFO com.example.pureproxy.decorator.code.MessageDecorator -- MessageDecorator 실행
21:17:38.225 [main] INFO com.example.pureproxy.decorator.code.RealComponent -- RealComponent 실행
21:17:38.225 [main] INFO com.example.pureproxy.decorator.code.MessageDecorator -- MessageDecorator 꾸미기 적용 전=data, 적용 후=*****data*****
21:17:38.229 [main] INFO com.example.pureproxy.decorator.code.TimeDecorator -- TimeDecorator 종료 resultTime=4ms
21:17:38.229 [main] INFO com.example.pureproxy.decorator.code.DecoratorPatternClient -- result=*****data*****

'객체지향 > 디자인패턴' 카테고리의 다른 글

템플릿 콜백 패턴  (0) 2026.02.18
전략 패턴  (0) 2026.02.18
템플릿 메서드 패턴  (0) 2026.02.15
옵저버 패턴  (0) 2026.02.12
프록시 패턴  (0) 2026.01.04