diff --git a/oop-in-spring/practice/design-pattern/proxyPattern/Image.java b/oop-in-spring/practice/design-pattern/proxyPattern/Image.java new file mode 100644 index 000000000..f5f4927f3 --- /dev/null +++ b/oop-in-spring/practice/design-pattern/proxyPattern/Image.java @@ -0,0 +1,5 @@ +package proxyPattern; + +public interface Image { + void draw(); +} diff --git a/oop-in-spring/practice/design-pattern/proxyPattern/ListUI.java b/oop-in-spring/practice/design-pattern/proxyPattern/ListUI.java new file mode 100644 index 000000000..1d2406c46 --- /dev/null +++ b/oop-in-spring/practice/design-pattern/proxyPattern/ListUI.java @@ -0,0 +1,19 @@ +package proxyPattern; + +import java.util.List; + +public class ListUI { + private List images; + + public ListUI(List images) { + this.images = images; + } + + public void onScroll(int start, int end) { + // 스크롤시, 화면에 표시되는 이미지를 표시 + for(int i = start; i <= end; i++) { + Image image = images.get(i); + image.draw(); + } + } +} diff --git a/oop-in-spring/practice/design-pattern/proxyPattern/ProxyImage.java b/oop-in-spring/practice/design-pattern/proxyPattern/ProxyImage.java new file mode 100644 index 000000000..dee059b71 --- /dev/null +++ b/oop-in-spring/practice/design-pattern/proxyPattern/ProxyImage.java @@ -0,0 +1,21 @@ +package proxyPattern; + +public class ProxyImage implements Image { + private String path; + private RealImage image; + + public ProxyImage(String path) { + this.path = path; + } + + @Override + public void draw() { + if(image == null) { + // 최초 접근시 객체 생성 + image = new RealImage(path); + } + + // RealImage 객체에 위임 + image.draw(); + } +} diff --git a/oop-in-spring/practice/design-pattern/proxyPattern/README.md b/oop-in-spring/practice/design-pattern/proxyPattern/README.md new file mode 100644 index 000000000..2bf2a04ed --- /dev/null +++ b/oop-in-spring/practice/design-pattern/proxyPattern/README.md @@ -0,0 +1,18 @@ +# 프록시 패턴 +- ListUI 변경 없이 이미지 로딩 방식을 교체할 수 있도록 해주는 패턴 +- 실제 객체를 대신하는 프로식 객체를 사용해서 실제 객체의 생성이나 접근 등을 제어할 수 있도록 해주는 패턴 + +## 프록시 패턴의 종류 +1. 가상 프록시 +- 필요한 순간에 실제 객체를 생성해주는 프록시 +2. 보호 프록시 +- 실제 객체에 대한 접근을 제어하는 프록시로서, 접근 권한이 있는 경우에만 실제 객체의 메서드를 실행하는 방식으로 구현한다. +3. 원격 프록시 +- 자바의 RMI(Remote Method Invocation)처럼 다른 프로세스에 존재하는 객체에 접근할 때 사용되는 프록시 +- 내부적으로 IPC이나 TCP 통신을 이용해서 다른 프로세스의 객체를 실행하게 된다. + +## 프록시 패턴을 적용할 떄 고려할 점 +- 프로식를 구현할 때 고려할 점은 `실제 객체를 누가 생성할 것이냐`에 대한 것이다. +- 가상 프록시는 `필요한 순간`에 실제 객체를 생성하는 경우가 많다. 따라서, 가상 프록시에서 실제 생성할 객체의 타입을 사용하게 된다. +- 반면, 보호 프록시는 `보호 프록시 객체를 생성할 때` 실제 객체를 전달하면 되므로, 실제 객체의 타입을 알 필요 없이 추상 타입을 사용하면 된다. +