The Proxy is known as a structural pattern, as it is used to form large object structures across many disparate objects. It functions as an interface to something else such as a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate.

For more information on usage or for below use cases you can refer to http://en.wikipedia.org/wiki/Proxy_pattern

Use Cases

Remote Proxy – Represents an object locally which belongs to a different address space. Take an example of ATM machine, it will hold proxy objects for bank information that exists in the remote server.

Virtual Proxy – In place of a complex or heavy object, use a skeleton representation. When an underlying image is huge in size, just represent it using a virtual proxy object and on demand load the real object. You feel that the real object is expensive in terms of instantiation and so without the real need we are not going to use the real object. Until the need arises we will use the virtual proxy.

Protection Proxy – An MNC where the proxy server that provides us internet by restricting access to some sort of websites like public e-mail, social networking, data storage etc. and provides only work related web pages. This is a type of proxy design pattern.

Example

The real world example would be a cheque or a debit card or a credit card is proxy for what is in our bank account. It can be used in place of cash and provides a mean for accessing that cash when required. And that is exactly what the Proxy pattern does – controls and manages access to the object they are protecting.

Another example would be when we generate client for Soap Web Service. A part of it contains implementation of proxy design pattern. The client has the stub files generated which acts as a proxy for classes in server side.

Class Diagram

Proxy Pattern in Java
Implementation

Create an Interface

package com.roytuts.designpattern.proxy;

public interface Money {

  void withdraw();

}

Create an implementation class

package com.roytuts.designpattern.proxy;

public class DebitCard implements Money {

  @Override
  public void withdraw() {
    System.out.println("Money withdrawn using Debit Card");
  }

}

Create a proxy class

package com.roytuts.designpattern.proxy;

public class ProxyDebitCard implements Money {

  private DebitCard debitCard;
  private AuthService authService;

  public ProxyDebitCard(AuthService authService) {
    this.authService = authService;
    debitCard = new DebitCard();
  }

  @Override
  public void withdraw() {
    if (authService.isAuthenticated()) {
      debitCard.withdraw();
    } else {
      System.out.println("Go to Bank and withdraw money using receipt.");
    }
  }

}

Create another class for authenticate service. Real world application will have more concrete implementation

package com.roytuts.designpattern.proxy;

public class AuthService {

  private boolean authenticated;

  public AuthService(final boolean authenticated) {
    this.authenticated = authenticated;
  }

  public boolean isAuthenticated() {
    return authenticated;
  }

  public void setAuthenticated(boolean authenticated) {
    this.authenticated = authenticated;
  }

}

Run the above class ProxyPatternTest and see the below output

Output 

Money withdrawn using Debit Card
Go to Bank and withdraw money using receipt.

That’s all. Thanks for your reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *