spring core proxy : decorator pattern
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package com.example.proxy.pureproxy.decorator;
|
||||
|
||||
import com.example.proxy.pureproxy.decorator.code.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@Slf4j
|
||||
public class DecoratorPatternTest {
|
||||
|
||||
@Test
|
||||
void noDecorator() {
|
||||
Component component = new RealComponent();
|
||||
DecoratorPatternClient client = new DecoratorPatternClient(component);
|
||||
client.execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
void decorate1() {
|
||||
Component realComponent = new RealComponent();
|
||||
Component messageDecorator = new MessageDecorator(realComponent);
|
||||
DecoratorPatternClient client = new DecoratorPatternClient(messageDecorator);
|
||||
client.execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
void decorate2() {
|
||||
Component realComponent = new RealComponent();
|
||||
Component messageDecorator = new MessageDecorator(realComponent);
|
||||
Component timeDecorator = new TimeDecorator(messageDecorator);
|
||||
DecoratorPatternClient client = new DecoratorPatternClient(timeDecorator);
|
||||
client.execute();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.example.proxy.pureproxy.decorator.code;
|
||||
|
||||
public interface Component {
|
||||
String operation();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.example.proxy.pureproxy.decorator.code;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class DecoratorPatternClient {
|
||||
|
||||
private final Component component;
|
||||
|
||||
public DecoratorPatternClient(Component component) {
|
||||
this.component = component;
|
||||
}
|
||||
|
||||
public void execute() {
|
||||
String result = component.operation();
|
||||
log.info("result={}", result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.example.proxy.pureproxy.decorator.code;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class MessageDecorator implements Component {
|
||||
|
||||
private final 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example.proxy.pureproxy.decorator.code;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class RealComponent implements Component {
|
||||
@Override
|
||||
public String operation() {
|
||||
log.info("RealComponent 실행!");
|
||||
return "data";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.example.proxy.pureproxy.decorator.code;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class TimeDecorator implements Component {
|
||||
|
||||
private final 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={}", resultTime);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user