spring core advanced : template method
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package com.example.advanced.trace.template;
|
||||
|
||||
import com.example.advanced.trace.template.code.AbstractTemplate;
|
||||
import com.example.advanced.trace.template.code.SubClassLogic1;
|
||||
import com.example.advanced.trace.template.code.SubClassLogic2;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@Slf4j
|
||||
public class TemplateMethodTest {
|
||||
|
||||
@Test
|
||||
void templateMethodV0() {
|
||||
logic1();
|
||||
logic2();
|
||||
}
|
||||
|
||||
private void logic1() {
|
||||
long startTime = System.currentTimeMillis();
|
||||
// 비즈니스 로직 실행
|
||||
log.info("비즈니스 로직 실행1");
|
||||
long endTime = System.currentTimeMillis();
|
||||
long resultTime = endTime - startTime;
|
||||
log.info("resultTime={}", resultTime);
|
||||
}
|
||||
|
||||
private void logic2() {
|
||||
long startTime = System.currentTimeMillis();
|
||||
// 비즈니스 로직 실행
|
||||
log.info("비즈니스 로직 실행2");
|
||||
long endTime = System.currentTimeMillis();
|
||||
long resultTime = endTime - startTime;
|
||||
log.info("resultTime={}", resultTime);
|
||||
}
|
||||
/**
|
||||
* 템플릿 메서드 패턴 적용
|
||||
*/
|
||||
@Test
|
||||
void templateMethodV1() {
|
||||
AbstractTemplate template1 = new SubClassLogic1();
|
||||
template1.execute();
|
||||
|
||||
AbstractTemplate template2 = new SubClassLogic2();
|
||||
template2.execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
void templateMethodV2() {
|
||||
AbstractTemplate template1 = new AbstractTemplate() {
|
||||
@Override
|
||||
protected void call() {
|
||||
log.info("비즈니스 로직1 실행");
|
||||
}
|
||||
};
|
||||
|
||||
template1.execute();
|
||||
|
||||
AbstractTemplate template2 = new AbstractTemplate() {
|
||||
@Override
|
||||
protected void call() {
|
||||
log.info("비즈니스 로직2 실행");
|
||||
}
|
||||
};
|
||||
|
||||
template2.execute();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.example.advanced.trace.template.code;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public abstract class AbstractTemplate {
|
||||
|
||||
public void execute() {
|
||||
long startTime = System.currentTimeMillis();
|
||||
// 비즈니스 로직 실행
|
||||
call(); // 상속
|
||||
// 비즈니스 로직 종료
|
||||
long endTime = System.currentTimeMillis();
|
||||
long resultTime = endTime - startTime;
|
||||
log.info("resultTime={}", resultTime);
|
||||
}
|
||||
|
||||
protected abstract void call();
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example.advanced.trace.template.code;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class SubClassLogic1 extends AbstractTemplate{
|
||||
|
||||
@Override
|
||||
protected void call() {
|
||||
log.info("비즈니스 로직1 실행");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example.advanced.trace.template.code;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class SubClassLogic2 extends AbstractTemplate{
|
||||
|
||||
@Override
|
||||
protected void call() {
|
||||
log.info("비즈니스 로직2 실행");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user