Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions oop-in-spring/practice/design-pattern/proxyPattern/Image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package proxyPattern;

public interface Image {
void draw();
}
19 changes: 19 additions & 0 deletions oop-in-spring/practice/design-pattern/proxyPattern/ListUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package proxyPattern;

import java.util.List;

public class ListUI {
private List<Image> images;

public ListUI(List<Image> 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();
}
}
}
21 changes: 21 additions & 0 deletions oop-in-spring/practice/design-pattern/proxyPattern/ProxyImage.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
18 changes: 18 additions & 0 deletions oop-in-spring/practice/design-pattern/proxyPattern/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 프록시 패턴
- ListUI 변경 없이 이미지 로딩 방식을 교체할 수 있도록 해주는 패턴
- 실제 객체를 대신하는 프로식 객체를 사용해서 실제 객체의 생성이나 접근 등을 제어할 수 있도록 해주는 패턴

## 프록시 패턴의 종류
1. 가상 프록시
- 필요한 순간에 실제 객체를 생성해주는 프록시
2. 보호 프록시
- 실제 객체에 대한 접근을 제어하는 프록시로서, 접근 권한이 있는 경우에만 실제 객체의 메서드를 실행하는 방식으로 구현한다.
3. 원격 프록시
- 자바의 RMI(Remote Method Invocation)처럼 다른 프로세스에 존재하는 객체에 접근할 때 사용되는 프록시
- 내부적으로 IPC이나 TCP 통신을 이용해서 다른 프로세스의 객체를 실행하게 된다.

## 프록시 패턴을 적용할 떄 고려할 점
- 프로식를 구현할 때 고려할 점은 `실제 객체를 누가 생성할 것이냐`에 대한 것이다.
- 가상 프록시는 `필요한 순간`에 실제 객체를 생성하는 경우가 많다. 따라서, 가상 프록시에서 실제 생성할 객체의 타입을 사용하게 된다.
- 반면, 보호 프록시는 `보호 프록시 객체를 생성할 때` 실제 객체를 전달하면 되므로, 실제 객체의 타입을 알 필요 없이 추상 타입을 사용하면 된다.