Example of Proxy Method Design Pattern in Java
Problem Statement:
We have to create a simple banking application with a proxy to control access to the real bank account.
Key Concepts of Proxy Method Design Pattern in Java
- Subject Interface (or Abstract Class): This is the common interface or abstract class that both the RealSubject (the real object) and the Proxy implement. It defines the operations that the Proxy will control access to.
- RealSubject: This is the real object that the Proxy represents. It implements the Subject interface. It provides the actual implementation of the operations declared in the Subject interface.
- Proxy: The Proxy class implements the Subject interface as well. It holds a reference to the RealSubject and controls access to it. The Proxy may provide additional functionality before or after assigning the operation to the RealSubject.
- Client: The client code interacts with the Proxy as if it were the real object. The client is unaware of whether itâs using the real object or a proxy.
Step wise Step Implementation of above Proxy Method Design Pattern in Java:
âBankAccountâ (Subject Interface): This is the subject interface that both the real and proxy objects will implement. It declares three methods: âdepositâ, âwithdraw,â and âgetBalanceâ.
Java
// Subject interface interface BankAccount { void deposit( double amount); void withdraw( double amount); double getBalance(); } |
âRealBankAccountâ (RealSubject) Class: âRealBankAccountâ is the real subject class implementing the âBankAccountâ interface. It maintains a balance and provides implementations for the deposit, withdraw, and getBalance methods.
Java
// RealSubject class class RealBankAccount implements BankAccount { private double balance; @Override public void deposit( double amount) { balance += amount; System.out.println("Deposited: " + amount); } @Override public void withdraw( double amount) { if (balance >= amount) { balance -= amount; System.out.println("Withdrawn: " + amount); } else { System.out.println("Insufficient funds!"); } } @Override public double getBalance() { return balance; } } |
âSecureBankAccountProxyâ (Proxy) Class: âSecureBankAccountProxyâ is the proxy class implementing the BankAccount interface. It contains a reference to the real bank account (âRealBankAccountâ) and requires a password for authentication during instantiation. The âauthenticateâ method checks the password and initializes the real bank account if authentication is successful. The âdepositâ, âwithdrawâ, and âgetBalanceâ methods delegate to the corresponding methods of the real bank account.
Java
// Proxy class class SecureBankAccountProxy implements BankAccount { private RealBankAccount realBankAccount; private String password; public SecureBankAccountProxy(String password) { this .password = password; authenticate(); } private void authenticate() { if (password.equals("secret")) { realBankAccount = new RealBankAccount(); System.out.println("Authentication successful."); } else { System.out.println("Authentication failed. Access denied."); } } @Override public void deposit( double amount) { if (realBankAccount != null ) { realBankAccount.deposit(amount); } } @Override public void withdraw( double amount) { if (realBankAccount != null ) { realBankAccount.withdraw(amount); } } @Override public double getBalance() { return (realBankAccount != null ) ? realBankAccount.getBalance() : 0.0 ; } } |
âProxyBankExampleâ Class (Client): âProxyBankExampleâ is the client code that demonstrates the usage of the proxy. It creates an instance of âSecureBankAccountProxyâ and performs deposit, withdrawal, and balance retrieval operations.
Java
// Client code public class ProxyBankExample { public static void main(String[] args) { // Using the proxy to access the real bank account BankAccount account = new SecureBankAccountProxy("secret"); // Operations account.deposit( 1000 ); account.withdraw( 500 ); double balance = account.getBalance(); System.out.println("Current Balance: " + balance); } } |
Below is the complete combined code of the above example:
To run this code correctly, copy this code and save it in a file named âProxyBankExample.javaâ.
Java
// Subject interface interface BankAccount { void deposit( double amount); void withdraw( double amount); double getBalance(); } // RealSubject class class RealBankAccount implements BankAccount { private double balance; @Override public void deposit( double amount) { balance += amount; System.out.println("Deposited: " + amount); } @Override public void withdraw( double amount) { if (balance >= amount) { balance -= amount; System.out.println("Withdrawn: " + amount); } else { System.out.println("Insufficient funds!"); } } @Override public double getBalance() { return balance; } } // Proxy class class SecureBankAccountProxy implements BankAccount { private RealBankAccount realBankAccount; private String password; public SecureBankAccountProxy(String password) { this .password = password; authenticate(); } private void authenticate() { if (password.equals("secret")) { realBankAccount = new RealBankAccount(); System.out.println("Authentication successful."); } else { System.out.println("Authentication failed. Access denied."); } } @Override public void deposit( double amount) { if (realBankAccount != null ) { realBankAccount.deposit(amount); } } @Override public void withdraw( double amount) { if (realBankAccount != null ) { realBankAccount.withdraw(amount); } } @Override public double getBalance() { return (realBankAccount != null ) ? realBankAccount.getBalance() : 0.0 ; } } // Client code public class ProxyBankExample { public static void main(String[] args) { // Using the proxy to access the real bank account BankAccount account = new SecureBankAccountProxy("secret"); // Operations account.deposit( 1000 ); account.withdraw( 500 ); double balance = account.getBalance(); System.out.println("Current Balance: " + balance); } } |
Authentication successful. Deposited: 1000.0 Withdrawn: 500.0 Current Balance: 500.0
Diagrammatical Representation of the Proxy Method Design Pattern in Java
In this representation:
- âBankAccountâ is the subject interface with âdepositâ, âwithdrawâ, and âgetBalanceâ methods.
- âRealBankAccountâ is the real subject implementing âBankAccountâ.
- âSecureBankAccountProxyâ is the proxy implementing âBankAccountâ. It contains a reference to a âRealBankAccountâ object.
- âProxyBankExampleâ is the client code creating an instance of âSecureBankAccountProxyâ and performing operations on it.
The arrows represent the relationships between the classes:
- The âSecureBankAccountProxyâ class contains a reference to a âRealBankAccountâ object.
- The client code âProxyBankExampleâ interacts with the âSecureBankAccountProxyâ, which may assign operations to the âRealBankAccountâ.
Proxy Method Design Pattern in Java
A Proxy Method or Proxy Design Pattern is a structural design pattern that provides a surrogate or placeholder for another object to control access to it. This pattern involves creating a new class, known as the proxy, which acts as an intermediary between a client and the real object. The proxy object controls access to the real object, allowing you to add additional functionality, such as lazy loading, access control, logging, or caching, without changing the actual objectâs code.
Important Topics for the Proxy Method Design Pattern in Java
- Example of Proxy Method Design Pattern in Java
- Use Cases of Proxy Method Design Pattern in Java
- Advantages of the Proxy Method Design Pattern in Java
- Disadvantages of the Proxy Method Design Pattern in Java
- Conclusion
Contact Us