Proxy Pattern
-structual pattern의 하나로, 다른 class의 기능을 담당하는 또다른 class
-outer world에 본래 객체의 기능전달을 위한 interface를 가진 객체를 만든다.
ProxyImage는 RealImage를 field로 가진다.(original object)
ProxyImage는 Image 와 공통의 interface를 구현함으로써 RealImage의 존재를 가리고 기능을 대신 제공한다.
client 오직 proxy와 대화하며 loadFromDisk와 같은 부차적인 작업에 신경쓸 필요가 없다.
ProxyImage.java
public class ProxyImage implements Image {
private String fileName;
RealImage realImage;
public ProxyImage(String fileName){
this.fileName = fileName;
}
@Override
public void display() {
if(realImage==null){
realImage=new RealImage(fileName);
}
realImage.display();
}
}
RealImage.java
public class RealImage implements Image {
private String fileName;
RealImage(String fileName){
this.fileName=fileName;
loadFromDisk(fileName);
}
private void loadFromDisk(String fileName) {
Log.d(TAG, "Loading :" +fileName);
}
@Override
public void display() {
Log.d(TAG, "display :" +fileName);
}
}
Image.java
public interface Image {
public static final String TAG = "Image";
void display();
}
결과 :
07-20 14:42:45.132 13017 13017 D Image : Loading :SimpleFile.png
07-20 14:42:45.133 13017 13017 D Image : display :SimpleFile.png
07-20 14:42:45.133 13017 13017 D Image : display :SimpleFile.png
출처 :
https://www.tutorialspoint.com/design_pattern/proxy_pattern.htm
'pattern' 카테고리의 다른 글
| Command pattern (0) | 2018.07.20 |
|---|---|
| Chain of Responsibility Pattern (0) | 2018.07.20 |
| Flyweight pattern (0) | 2018.07.20 |
| Facade Pattern (0) | 2018.07.20 |
| Decorator pattern (0) | 2018.07.20 |