Proxy Design [프록시 디자인]

  1. Design Object [디자인목적]

    1. 프록시 : 대리인 이라는 뜻(Surrogate)
    2. 특정 객체에 대한 접근을 제어하고자 할 때 사용.
    3. 특정 객체 : 대용량 이미지 로딩 , 특별권한이 필요한 객체 생성, 객체 생성 자체가 복잡한 경우.
  2. Options [대안들]

    1. 실제 객체를 생성하는 부분을 application(client)에서 제어한다면, 소스가 길어지고, 유지보수가 어려울 수 있다.
  3. Benefit [디자인 장점]

    1. proxy를 통해 실제 객체를 생성 및 호출하므로, 객체 제어 가능.

    2. proxy의 종류

      1. remote proxy : 다른주소에 존재, 다른 공간에 존재하는 객체에 대한 로컬표현

        1. 로컬에서 RFC 를 통해 데이터 를 받아올 때, 호출 객체는 피호출 객체의 정보를 알 필요없시 proxy를 통해 데이터를 받아올 수 있다.
      2. virtual proxy : 복잡한 객체를 필요할 때

        1. 객체를 필요할 때만 생성하여, 최적화 수행
      3. protection proxy : 원래 객체에 대한 액세스 권한을 제어할 때

      4. smart reference : 객체 접근 시 피호출 객체에 담기 어려운 추가적인 액션을 수행하도록 작성

        1. 대용량 데이터 호출

        2. 접속횟수 카운트

  4. Structure & Sample Code [구조와 샘플코드]

    1) 이미지를 불러오는 어플리케이션의 proxy

    1. UML

    2. 구조

      -> Image는 client에서 사용될 대용량 이미지 파일 객체
      -> ImageProxy 는 Image를 생성하며, draw 한다.
      -> Application은 Images<List>를 image의 draw를 통해 실행한다.
      -> Client에서 imageProxy를 생성하고, proxy리스트를 Application에 전달하여 draw를 실행.

    3. Proxy Design
      -> client의 Application은 Image를 바로 실행하는 것이 아닌, 필요 할 때, ImageProxy를 통해 Image.draw()를 실

    1)App(client)

public class Client {

    public static void main(String[] args) {
        ImageProxy image = new ImageProxy("test image");     // ImageProxy 1
        ImageProxy image2 = new ImageProxy("second image");  // ImageProxy 2

        List<Image> images = new ArrayList<Image>();         // Image List
        images.add(image);
        images.add(image2);

        Application application = new Application(images);


        System.out.println("Application setup");

        application.draw();
    }

}

2) Application

public class Application{
    private List<Image> images = new ArrayList<Image>();

    public Application(List<Image> images) {
        this.images = images;
    }

    public void draw(){
        for (Image image : images) {
            image.draw();
        }
    }
}

3) Image

public class Image {
    protected String url;

    public Image() {
    }

    public Image(String url) {
        System.out.println("Loading image");
        this.url = url;
    }

    public void draw(){
        System.out.println("Draw image from url "+url);
    }


}

4) ImageProxy -> Image의 proxy

public class ImageProxy extends Image {
    private Image image;

    public ImageProxy(String url) {
        super();
        this.url = url;
    }

    @Override
    public void draw() {
        if(image ==null){
            image = new Image(this.url);    // 만약 Image가 null이면, 생성.
        }
        image.draw();
    }
 }

results matching ""

    No results matching ""