
목차
Spring 핵심원리 고급편 - Decorator Pattern 적용
@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 null; } }
|
@Test void decorator2(){ Component realComponent = new RealComponent(); Component messageDecorator = new MessageDecorator(realComponent); Component timeDecorator = new TimeDecorator(messageDecorator); DecoratorPatternClient client = new DecoratorPatternClient(timeDecorator); client.execute(); }
|


Decoratro Pattern 은 항상 내부에 꾸며 줄 대상(Component) 가 존재해야 한다.