DEV Community

Prashant Mishra
Prashant Mishra

Posted on

Proxy

Proxy is one of the structural design patterns, It is used to create a surrogate or placeholder object, which is used to control the access of original object.
It acts as an intermediary adding extra level of control, and can perform extra actions before and after delegating request to real object.

Key Concepts:
Proxy Object: Represents the real object and controls access to it.
Real Object (Subject): The actual object that does the work.
Client: The entity that interacts with the proxy instead of the real object directly.

Lets understand this taking an example of Image.

//Object interface
public interface Image{
    public void display();
}


//Real object
public class RealImage implements Image {
    private String file;

    public RealImage(String fileName){
        this.file = fileName;
        loadImageFromDisk();
    }
    @Override
    public void display(){
        System.out.println("Rendering image : "+ file);
    }
    private void loadImageFromDisk(){
        System.out.println("Loading image "+file+" from disk");
    }
}

//Proxy class
public class ProxyImage implements Image {
    private Image image;
    private String file;

    public ProxyImage(String fileName){
        this.file =fileName;
    }
    @Override
    public void display(){
        if(image ==null){// create object of RealImage only if the image reference is null, thus resulting in LazyIntialization 
            //( i.e. Initializing the object only when it is needed not beforehand)
            image = new RealImage(file);
        }
        image.display();
    }

}

// client
public class Main {
    public static void main(String args[]){
        Image image = new ProxyImage("wallpaper.png");
        //image is loaded and displayed for the first time
        image.display();
        //image will not be loaded again, only display will be called 
        image.display();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Loading image wallpaper.png from disk
Rendering image : wallpaper.png
Enter fullscreen mode Exit fullscreen mode

Use Cases:
Lazy initialization: Delaying object creation until it's absolutely necessary.
Access control: Restricting access to specific methods based on user roles or permissions.
Logging: Adding logging or monitoring functionalities.

Top comments (1)

Collapse
 
iananickos profile image
IanaNickos

Great explanation of the Proxy design pattern! It's fascinating how this pattern provides a surrogate to control access to an object, adding an extra layer of control. In the world of software development, especially when dealing with web scraping or automation, having reliable proxies is crucial. I recently came across NodeMaven, a proxy service that offers high-quality residential IPs with a focus on real user behavior. They provide sticky sessions lasting up to 24 hours, ensuring stable connections for long-term tasks. Their global coverage spans over 150 countries and 1,400 cities, making it ideal for tasks requiring precise geo-targeting. If you're looking for a proxy provider that emphasizes IP quality and reliability, it's worth checking out nodemaven.com/proxy-server/

Some comments may only be visible to logged-in visitors. Sign in to view all comments.