Search Blog

Search duranek.blogspot.com

Wednesday, February 8, 2012

Proxy Pattern ( Structural )

Proxy Pattern ( Structural )

1) Proxy class is used to control access to another class

3) Complex bir objeyi temsil eden basit bir obje olabilir proxy.
Mesela o objenin creation'i expensive ise, onu basit bir obje temsil edebilir.
ve eger cok gerekliyse gercekten yaratilabilir. ( Virtual Proxy ) 

4) surrogate pattern olarak da bilinir

5) 
-> The proxy object has the same interface as the target object

-> The proxy holds a reference to the target object and can forward requests to
the target as required (delegation!)

-> In effect, the proxy object has the authority the act on behalf of the client to
interact with the target object

6) Adapter different interface verir, proxy same interface verir. Decorator
enhanced interface verir.

7) Access controlleri yapan proxy. ( Proctection Proxy )

8) 

public interface Graphic {

        // a method used to draw the image
      public void draw();
}

public class Image implements Graphic {

    private byte[] data;

    public Image(String filename) {
        // Load the image
        data = loadImage(filename);
    }
        
    public void draw() {
        // Draw the image
        drawToScreen(data);
    }
}

// late initialization, caching
public class ImageProxy implements Graphic {

    // Variables to hold the concrete image
    private String filename;
    private Image content;

    public ImageProxy(String filename) {
        this.filename = filename;
        content = null;
    }

    // on a draw-request, load the concrete image
    //   if we haven't done it until yet.
    public void draw() {
        if (content == null) {
            content = new Image(filename);
        }
        // Forward to the Concrete image.
        content.draw();
    }
}

9) virtual proxy, firewall proxy, remote proxy, protection proxy etc..

No comments: