Proxy Design Pattern In Java

Table of Contents


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 Wikipedia.

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.

Proxy 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.

Proxy Pattern Class Diagram

The following diagram shows the Java class diagram for the proxy design pattern.

proxy design pattern


Java, Maven 3.8.2

Project Setup

You can create a maven based project in your favorite IDE or tool. The following pom.xml file can be used for the project:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=""





Proxy Interface

I have created an interface for the proxy pattern. The interface declares a method withdraw() for tracking the money withdrawal.


public interface Money {

	void withdraw();


Proxy Implementation

The means through which money is withdrawal.


public class DebitCard implements Money {

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


Proxy Class

This class actually proxy class that is used for the means how the money gets withdrawal.


public class ProxyDebitCard implements Money {

	private DebitCard debitCard;
	private AuthService authService;

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

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


Service Class

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


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;


Proxy Example


public class ProxyPatternExample {

	public static void main(String[] args) {
		Money money1 = new ProxyDebitCard(new AuthService(true));

		Money money2 = new ProxyDebitCard(new AuthService(false));


Run the above class ProxyPatternTest and see the below output:

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

Source Code


Leave a Reply

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