spring core proxy : proxy pattern

This commit is contained in:
haerong22
2021-11-13 17:55:12 +09:00
parent 7956f866c6
commit c448b207d5
5 changed files with 91 additions and 0 deletions

View File

@@ -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();
}
}

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}
}

View File

@@ -0,0 +1,5 @@
package com.example.proxy.pureproxy.proxy.code;
public interface Subject {
String operation();
}