작성
·
550
8
강사님께서 말씀주신 component 중복 호출 부분 추상클래스로 빼봤습니다. 피드백 사항 있으면 말씀 부탁 드립니다^^;
Decorator - 추상클래스
public abstract class Decorator implements Component{
private Component component;
public Decorator(Component component) {
this.component = component;
}
@Override
public String operation() {
return component.operation();
}
}
MessageDecorator
public class MessageDecorator extends Decorator{
public MessageDecorator(Component component) {
super(component);
}
@Override
public String operation() {
log.info("MessageDecorator 실행");
//data -> *****data*****
String result = super.operation();
String decoResult = "*****" + result + "*****";
log.info("MessageDecorator 꾸미기 적용 전={}, 적용 후={}", result, decoResult);
return decoResult;
}
}
TimeDecoratior
public class MessageDecorator extends Decorator{
public MessageDecorator(Component component) {
super(component);
}
@Override
public String operation() {
log.info("MessageDecorator 실행");
//data -> *****data*****
String result = super.operation();
String decoResult = "*****" + result + "*****";
log.info("MessageDecorator 꾸미기 적용 전={}, 적용 후={}", result, decoResult);
return decoResult;
}
}
test 코드
@Test
void decorator3() {
Decorator messageDecorator = new MessageDecorator(new RealComponent());
messageDecorator.operation();
System.out.println();
Decorator timeMsgDecorator = new TimeDecorator(new MessageDecorator(new RealComponent()));
timeMsgDecorator.operation();
}