Proxy Pattern

Category : Design Patterns | Sub Category : Structural design patterns | By Prasad Bonam Last updated: 2023-07-09 14:37:18 Viewed : 77

Proxy Pattern:

The Proxy pattern is a structural design pattern that provides a surrogate or placeholder object to control the access to another object. It acts as an intermediary between the client and the actual object and allows for additional functionality to be added around the objects core functionality. The Proxy pattern is commonly used to implement security checks, caching, lazy initialization, or remote communication. Here is an example of implementing the Proxy pattern in Java:

// Subject interface interface Image { void display(); } // RealSubject class class RealImage implements Image { private String filename; public RealImage(String filename) { this.filename = filename; loadFromDisk(); } private void loadFromDisk() { System.out.println("Loading image: " + filename); } @Override public void display() { System.out.println("Displaying image: " + filename); } } // Proxy class class ImageProxy implements Image { private String filename; private RealImage realImage; public ImageProxy(String filename) { this.filename = filename; } @Override public void display() { if (realImage == null) { realImage = new RealImage(filename); } realImage.display(); } } // Client code public class Client { public static void main(String[] args) { Image image1 = new ImageProxy("image1.jpg"); Image image2 = new ImageProxy("image2.jpg"); // The actual image is loaded and displayed when necessary image1.display(); // Output: Loading image: image1.jpg, Displaying image: image1.jpg image1.display(); // Output: Displaying image: image1.jpg // The actual image is loaded and displayed when necessary image2.display(); // Output: Loading image: image2.jpg, Displaying image: image2.jpg } }

In this example:

  • The Image interface represents the subject interface, which declares the display() method that clients will invoke.
  • The RealImage class is the real subject that performs the core functionality of loading and displaying an image. It implements the Image interface.
  • The ImageProxy class is the proxy class that acts as an intermediary between the client and the real image. It also implements the Image interface and maintains a reference to the real image.
  • In the ImageProxy class, the display() method checks if the real image has been loaded. If not, it creates an instance of the RealImage class and delegates the display() call to the real image.
  • The client code in the Client class demonstrates the usage of the proxy. It creates instances of the proxy (ImageProxy) and calls their display() methods. The first call triggers the loading and display of the actual image, while subsequent calls use the already loaded image.

By using the Proxy pattern, you can control access to an object and add additional functionality around its core operations. The proxy can handle tasks such as lazy initialization, caching, logging, or security checks, without the client being aware of it. This pattern provides a way to achieve transparent or on-demand access to the real object and enables you to control or enhance the behavior of the real object as needed.

Related Articles

Leave a Comment: