Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 그리디
- http
- db
- 스프링 핵심 기능
- Servlet
- 백준
- jpa
- transaction
- java
- kotlin
- pointcut
- JDBC
- 인프런
- QueryDSL
- Exception
- spring
- 알고리즘
- springdatajpa
- 스프링 핵심 원리
- AOP
- JPQL
- 자바
- Proxy
- 스프링
- 김영한
- Spring Boot
- SpringBoot
- Thymeleaf
- Android
- Greedy
Archives
- Today
- Total
개발자되기 프로젝트
구체 클래스 기반 프록시 - 예제 본문
인터페이스 없이 구체클래스를 기반으로 프록시를 적용할 수 있을까??
1. ConcreteLogic, ConcreteClient
@Slf4j
public class ConcreteLogic {
public String operation(){
log.info("ConcreteLogic 실행");
return "data";
}
}
@Slf4j
public class ConcreteClient {
private ConcreteLogic concreteLogic;
public ConcreteClient(ConcreteLogic concreteLogic) {
this.concreteLogic = concreteLogic;
}
public void execute(){
concreteLogic.operation();
}
}
2. 의존관계
3. 테스트
public class ConcreteProxyTest {
@Test
void noProxy(){
ConcreteLogic concreteLogic = new ConcreteLogic();
ConcreteClient client = new ConcreteClient(concreteLogic);
client.execute();
}
}
4. 클래스 기반 프록시 도입
- 지금까지 인터페이스를 기반으로 프록시를 도입했다.
- 그런데 자바의 다형성은 인터페이스를 구현하든, 아니면 클래스를 상속하든
- 상위 타입만 맞으면 다형성이 적용된다.
- 쉽게 이야기해서 인터페이스가 없어도 프록시를 만들수 있다는 뜻이다.
- 그래서 이번에는 인터페이스가 아니라 클래스를 기반으로 상속을 받아서 프록시를 만들어보자.
5. TimeProxy
- TimeProxy는 시간을 측정하는 부가기능을 제공함. 그리고 인터페이스가 아니라,
구체 클레스인 ConcreteLogic을 상속받아서 만든다. - client입장에서는 proxy, concreteClass 구분 못함.
@Slf4j
public class TimeProxy extends ConcreteLogic{
private ConcreteLogic concreteLogic;
public TimeProxy(ConcreteLogic concreteLogic) {
this.concreteLogic = concreteLogic;
}
@Override
public String operation() {
log.info("TimeDecorator 실행");
long startTime = System.currentTimeMillis();
String result = concreteLogic.operation();
long endTime = System.currentTimeMillis();
long resultTime = endTime - startTime;
log.info("TimeDecorator 종료, resultTime = {} ms", resultTime);
return result;
}
}
6. Test
- client는 timeProxy를 의존하고, timeProxy는 concreteLogic을 의존함.
@Test
void addProxy(){
ConcreteLogic concreteLogic = new ConcreteLogic();
TimeProxy timeProxy = new TimeProxy(concreteLogic);
ConcreteClient client = new ConcreteClient(timeProxy);
client.execute();
}
- 여기서 핵심은 ConcreteClient 의 생성자에 concreteLogic 이 아니라
- timeProxy 를 주입하는 부분.
- ConcreteClient 는 ConcreteLogic 을 의존하는데,
- 다형성에 의해 ConcreteLogic 에 concreteLogic 도 들어갈 수 있고, timeProxy 도 들어갈 수 있다.
TimeDecorator 실행
ConcreteLogic 실행
TimeDecorator 종료, resultTime = 0 ms
7. 정리
- 인터페이스가 없어도 클래스 기반의 프록시가 잘 적용된 것을 확인할 수 있다.
- 자바 언어에서 다형성은 인터페이스나 클래스를 구분하지 않고 모두 적용된다.
- 해당 타입과 그 타입의 하위 타입은 모두 다형성의 대상이 된다.
8. GitHub : 211229 Concrete Class기반 Proxy
GitHub - bsh6463/Spring_Advanced_2
Contribute to bsh6463/Spring_Advanced_2 development by creating an account on GitHub.
github.com
'인프런 > [인프런] 스프링 핵심 원리 - 고급' 카테고리의 다른 글
인터페이스기반 프록시와 클래스 기반 프록시 (0) | 2021.12.30 |
---|---|
구체 클래스 기반 프록시 - 적용 (0) | 2021.12.30 |
Interface기반 Proxy - 적용 (0) | 2021.12.29 |
Proxy Pattern & Decorator Pattern (0) | 2021.12.28 |
Decorator Pattern 3 - chain (0) | 2021.12.28 |
Comments