spring core proxy : proxy pattern
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package com.example.proxy.pureproxy.proxy;
|
||||
|
||||
import com.example.proxy.pureproxy.proxy.code.CacheProxy;
|
||||
import com.example.proxy.pureproxy.proxy.code.ProxyPatternClient;
|
||||
import com.example.proxy.pureproxy.proxy.code.RealSubject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ProxyPatternTest {
|
||||
|
||||
@Test
|
||||
void noProxyTest() {
|
||||
RealSubject realSubject = new RealSubject();
|
||||
ProxyPatternClient client = new ProxyPatternClient(realSubject);
|
||||
client.execute();
|
||||
client.execute();
|
||||
client.execute();
|
||||
}
|
||||
|
||||
@Test
|
||||
void cacheProxyTest() {
|
||||
RealSubject realSubject = new RealSubject();
|
||||
CacheProxy cacheProxy = new CacheProxy(realSubject);
|
||||
ProxyPatternClient client = new ProxyPatternClient(cacheProxy);
|
||||
client.execute();
|
||||
client.execute();
|
||||
client.execute();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.example.proxy.pureproxy.proxy.code;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class CacheProxy implements Subject {
|
||||
|
||||
private final Subject target;
|
||||
private String cacheValue;
|
||||
|
||||
public CacheProxy(Subject target) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String operation() {
|
||||
log.info("프록시 호출!");
|
||||
if (cacheValue == null) {
|
||||
cacheValue = target.operation();
|
||||
}
|
||||
return cacheValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.proxy.pureproxy.proxy.code;
|
||||
|
||||
public class ProxyPatternClient {
|
||||
|
||||
private final Subject subject;
|
||||
|
||||
public ProxyPatternClient(Subject subject) {
|
||||
this.subject = subject;
|
||||
}
|
||||
|
||||
public void execute() {
|
||||
subject.operation();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.example.proxy.pureproxy.proxy.code;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class RealSubject implements Subject {
|
||||
@Override
|
||||
public String operation() {
|
||||
log.info("실제 객체 호출");
|
||||
sleep(1000);
|
||||
return "data";
|
||||
}
|
||||
|
||||
private void sleep(int millis) {
|
||||
try {
|
||||
Thread.sleep(millis);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.example.proxy.pureproxy.proxy.code;
|
||||
|
||||
public interface Subject {
|
||||
String operation();
|
||||
}
|
||||
Reference in New Issue
Block a user