Proxy Design Pattern In Java

Table of Contents

Introduction

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

Prerequisites

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="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>java-proxy-design-pattern</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>11</maven.compiler.source>
		<maven.compiler.target>11</maven.compiler.target>
	</properties>

	<dependencies>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
			</plugin>
		</plugins>
	</build>
</project>

Proxy Interface

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

package com.roytuts.java.proxy.design.pattern;

public interface Money {

	void withdraw();

}

Proxy Implementation

The means through which money is withdrawal.

package com.roytuts.java.proxy.design.pattern;

public class DebitCard implements Money {

	@Override
	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.

package com.roytuts.java.proxy.design.pattern;

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.");
		}
	}

}

Service Class

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

package com.roytuts.java.proxy.design.pattern;

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

package com.roytuts.java.proxy.design.pattern;

public class ProxyPatternExample {

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

		Money money2 = new ProxyDebitCard(new AuthService(false));
		money2.withdraw();
	}

}

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

Download

Leave a Reply

Your email address will not be published.