Proxy Design Pattern: It’s Super Easy

Asiri Gunasena

Published:

Design Pattern

Proxy Design Pattern

Proxy Design Pattern intention is to create a new layer of representations for the real objects. That means adding a new intermediate level between real objects and object users. This level of the intermediate layer is a substitute for a real object or proxy for the real object.

There are two main reasons to create a proxy. The first one is adding security to the real objects and the second one is reducing the cost and working as a wrapper for the original object. The wrapper hides the complexity of the real object.

Let’s move into the examples of proxy patterns and how to map with the real world.

Proxy Design Pattern Real World Example

One of the best matching examples for the Proxy pattern is the Bank account and the Debit card. The bank account is the real object and it is represented by the debit card. Therefore debit card is a proxy for the bank account.

Let’s see the class diagram before the implementation.

Proxy Design Pattern
Proxy Design Pattern link Debit card with Bank Account

Proxy Design Pattern in Java

//
// Initiate account functionalities
interface BankAccount{
   void debit();
}
// Real Object as Saving account
class SavingAccount implements BankAccount{

   @Override
   public void debit() {
      System.out.println("Debit from Bank Account");
   }  
   // Validate Account from Bank
   public boolean validate() {
      System.out.println("validate Bank Account");
      return true;
   }   
}
// Proxy class for the real object of SavingAccount
class DebitCardProxy implements BankAccount{
   SavingAccount savingAccount;
   
   @Override
   public void debit() {
      if (savingAccount == null) {
         savingAccount = new SavingAccount();
      }
      // Doing some validation before debit from account
      if (!savingAccount.validate()) {
         return;
      }
      savingAccount.debit();
   }   
}


public class ProxyClient {
   public static void main(String[] args) {
      BankAccount account= new DebitCardProxy();
      account.debit();
   }
}

Output

Validate Bank Account
Debit from Bank Account

According to implementation, DebitCardProxy is the proxy class and it represents the Real SavingAccount. But at the same time proxy class is used to validate and reduce the complexity of the main account. 

Advantages of Proxy Design Pattern

  • The main advantage is the security of the original object.
  • Create a copy of a real object using a proxy.
  • Do the necessary changes to copy without affecting the real object.

Design Patterns Book

Design Patterns are one of the most famous solutions in programming and millions of people follow them to fix their tasks, design projects, and so on. Most of the patterns are created based on basic OOP concepts (If your beginner then read our basic OOP, Encapsulation, Inheritance, Polymorphism, and Abstraction articles with great examples )

There are a lot of articles and design pattern books available with different examples to read. Other than our article if you are interested in this topic you could go through the below books as well.

BooksDescription

Head First Design Patterns: A Brain-Friendly Guide
My favorite and it’s more readable. There are good examples and they look good to beginners as well.

Design Patterns: Elements of Reusable Object-Oriented Software
There are 23 design patterns. showing how to select an appropriate pattern for your case.

Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software
There are a lot of simple examples showing how to use design patterns in the correct way. Those are based on SOLIC principles.
Design Patterns ebook online

There are a lot of design pattern ebooks on Amazon. Those design patterns are written with great examples in a different programming language. I included a few of them according to their best ranking.

ennicode

Address: 89/1 Rabbegamuwa, Handessa, Kandy, Central, Sri Lanka

Email to: Primary: [email protected]

Services

E-Learning

Company Websites

Support and Configuration work

Banners, Covers, and Post

Web Development & Configurations

Content Writing and Marketing

Contact

Ennicode