2015-01-20 3 views
4

프록시 디자인 패턴을 이해하려고했습니다. 하지만 프록시 디자인 패턴의 사용법을 이해하지 못했습니다. 위키 피 디아에서이 코드 예제를 얻었습니다.프록시 디자인 패턴의 사용

interface Image { 
    public void displayImage(); 
} 

//on System A 
class RealImage implements Image { 

    private String filename = null; 
    /** 
    * Constructor 
    * @param filename 
    */ 
    public RealImage(final String filename) { 
     this.filename = filename; 
     loadImageFromDisk(); 
    } 

    /** 
    * Loads the image from the disk 
    */ 
    private void loadImageFromDisk() { 
     System.out.println("Loading " + filename); 
    } 

    /** 
    * Displays the image 
    */ 
    public void displayImage() { 
     System.out.println("Displaying " + filename); 
    } 

} 

//on System B 
class ProxyImage implements Image { 

    private RealImage image = null; 
    private String filename = null; 
    /** 
    * Constructor 
    * @param filename 
    */ 
    public ProxyImage(final String filename) { 
     this.filename = filename; 
    } 

    /** 
    * Displays the image 
    */ 
    public void displayImage() { 
     if (image == null) { 
      image = new RealImage(filename); 
     } 
     image.displayImage(); 
    } 

} 

class ProxyExample { 

    /** 
    * Test method 
    */ 
    public static void main(String[] args) { 
     final Image IMAGE1 = new ProxyImage("HiRes_10MB_Photo1"); 
     final Image IMAGE2 = new ProxyImage("HiRes_10MB_Photo2"); 

     IMAGE1.displayImage(); // loading necessary 
     IMAGE1.displayImage(); // loading unnecessary 
     IMAGE2.displayImage(); // loading necessary 
     IMAGE2.displayImage(); // loading unnecessary 
     IMAGE1.displayImage(); // loading unnecessary 
    } 

} 

이 예에서는 두 번째로 dispalyImage를로드 할 필요가 없습니다. RealImage 객체에 직접 액세스하는 것조차 가능합니다.

  final Image IMAGE1 = new RealImage("HiRes_10MB_Photo1"); 
      final Image IMAGE2 = new RealImage("HiRes_10MB_Photo2"); 

      IMAGE1.displayImage(); // loading necessary 
      IMAGE1.displayImage(); // loading unnecessary 
      IMAGE2.displayImage(); // loading necessary 
      IMAGE2.displayImage(); // loading unnecessary 
      IMAGE1.displayImage(); // loading unnecessary 

이 패턴에서는 ProxyImage 클래스의 사용법을 이해해야합니다.

+2

어떤 부분을 혼동하고 있습니까? –

+0

여기에서 ProxyImage의 객체 인스턴스를 사용하여 다시로드하지 않고 이미지를 표시합니다. RealImage의 객체 인스턴스도 가지고 있다면 같은 것을 보관할 수 있습니다. 왜 이것이 패턴으로 간주 되는가? – Burusothman

+0

Wikipedia의 패턴 예제를 조심하십시오. 자바에서 가장 많이 사용되는 패턴을 원하는 경우 [케이 HORSTMANN의 객체 지향 설계 및 패턴 (http://horstmann.com/design_and_patterns.html) 좋다. 해당 링크의 페이지에 소스 코드가 있습니다. – Fuhrmanator

답변

8

당신도 알다시피, 나는 당신에게 동의합니다. 내가 많은 것 같아 그들이 프록시 패턴에 사용할 수있는 더 좋은 예입니다. This seems to use the same example but it's explained much better. 그 대신에 봐야합니다.

기본적으로, 모든이 댓글까지 오는 다음 proxy이 예에서 당신을 제공하는 장점이

// create the Image Object only when the image is required to be shown 

합니다. 대표 '대신에'

package proxy; 

/** 
* Image Viewer program 
*/ 
public class ImageViewer { 


    public static void main(String[] args) { 

    // assuming that the user selects a folder that has 3 images  
    //create the 3 images 
    Image highResolutionImage1 = new ImageProxy("sample/veryHighResPhoto1.jpeg"); 
    Image highResolutionImage2 = new ImageProxy("sample/veryHighResPhoto2.jpeg"); 
    Image highResolutionImage3 = new ImageProxy("sample/veryHighResPhoto3.jpeg"); 

    // assume that the user clicks on Image one item in a list 
    // this would cause the program to call showImage() for that image only 
    // note that in this case only image one was loaded into memory 
    highResolutionImage1.showImage(); 

    // consider using the high resolution image object directly 
    Image highResolutionImageNoProxy1 = new HighResolutionImage("sample/veryHighResPhoto1.jpeg"); 
    Image highResolutionImageNoProxy2 = new HighResolutionImage("sample/veryHighResPhoto2.jpeg"); 
    Image highResolutionImageBoProxy3 = new HighResolutionImage("sample/veryHighResPhoto3.jpeg"); 


    // assume that the user selects image two item from images list 
    highResolutionImageNoProxy2.showImage(); 

    // note that in this case all images have been loaded into memory 
    // and not all have been actually displayed 
    // this is a waste of memory resources 

    } 

} 
+1

잘 설명되어 있습니다 ........... thax buddy – Burusothman

0

프록시 수단 '또는 기관은 다른 사람, 또는 그림을 표현하기 : 이미지를 표시하지 않는 경우, 당신은 그것을로드의 벌금을 지불하지 않는다 무엇인가의 가치를 나타내는 데 사용할 수 있습니다. 프록시 디자인 패턴을 서로 게이트, 핸들 및 래퍼라고도합니다.

클라이언트에서 주요 개체의 복잡성을 감싸기위한 래퍼를 만들 때 사용됩니다.

프록시 디자인 패턴의 일부 실제 예 :

1) 은행의 heque 또는 신용 카드는 우리 은행 계좌에 무엇을위한 프록시입니다. 현금 대신 사용할 수 있으며 필요한 경우 현금에 액세스 할 수있는 방법을 제공합니다. 프록시 패턴은 정확히 "보호하고있는 객체에 대한 액세스를 제어하고 관리합니다"입니다.

2) 사이트 액세스가 거의 제한되지 않는 프록시를 사용하는 회사 또는 회사. 프록시는 먼저 연결하려는 호스트를 확인하고, 제한된 사이트 목록에 포함되어 있지 않으면 실제 인터넷에 연결합니다.

관련 문제