Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

Decorator Pattern 3 - chain 본문

인프런/[인프런] 스프링 핵심 원리 - 고급

Decorator Pattern 3 - chain

Seung__ 2021. 12. 28. 23:16

1. 실행 시간을 측정하는 Decorator


  • Decorator는 하나 추가하여 시간을 측정하는 기능을 추가해보자.

 

2. TimeDecorator


  • TimeDecorator는 timeDecorator가 호출된 시간과 다음 Component가 종료된 시간을 측정.
import lombok.extern.slf4j.Slf4j;

@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;
    }
}

 

3. Test


@Test
void decorator2(){
    Component realComponent = new RealComponent();
    Component messageDecorator = new MessageDecorator(realComponent);
    Component timeDecorator = new TimeDecorator(messageDecorator);
    DecoratorPatternClient client = new DecoratorPatternClient(timeDecorator);

    client.execute();

}
23:13:01.512 [main] INFO hello.proxy.pureproxy.decorator.code.TimeDecorator - TimeDecorator 실행
23:13:01.514 [main] INFO hello.proxy.pureproxy.decorator.code.MessageDecorator - MessageDecorator 실행
23:13:01.514 [main] INFO hello.proxy.pureproxy.decorator.code.RealComponent - Real Component 실행
23:13:01.520 [main] INFO hello.proxy.pureproxy.decorator.code.MessageDecorator - MessageDecorator 꾸미기 적용 : data --> ***data***
23:13:01.523 [main] INFO hello.proxy.pureproxy.decorator.code.TimeDecorator - TimeDecorator 종료, resultTime =  8 ms
23:13:01.523 [main] INFO hello.proxy.pureproxy.decorator.code.DecoratorPatternClient - result=***data***

 

4. 정리


Client 코드의 변경 없이 proxy의 추가로 기능을 추가했다. -> Decorator 특징

 

 

5. GitHub : 211228 DecoratorPattern 3


 

GitHub - bsh6463/Spring_Advanced_2

Contribute to bsh6463/Spring_Advanced_2 development by creating an account on GitHub.

github.com

'인프런 > [인프런] 스프링 핵심 원리 - 고급' 카테고리의 다른 글

Proxy Pattern & Decorator Pattern  (0) 2021.12.28
Decorator Pattern 3 - chain  (0) 2021.12.28
Decorator Pattern 2  (0) 2021.12.28
Decorator Pattern - 1  (0) 2021.12.28
Proxy Pattern - 예제코드 2  (0) 2021.11.23
Comments