In this article we are going to learn about Proxy Design pattern.

  • What is Proxy pattern?
  • What are the basic principle?
  • How to implement?
  • what are the usages?

What is proxy patter?

By the name we can understand the basic of it. That means a class representing another class.

In the OO world when we want to hide/protect a class(sensitive) from accessed by other classes then we introduce an intermediate class who is called proxy class. And the implementation of the proxy object is known as proxy class.

You might have question, why another class, proxy class. Suppose , we are designing a ATM machine transaction where client object want to know the machine state and cash in the machine. So, if we give client full ATMMechine object, it will be risky. So, we introduce a proxy class that only gets the state & cash amount an provide to client. And, client will have no worries where the information coming, he just initiate the proxy to get information.

Or, an Image information display that shows information only, don’t actually open the image just get the information about its name, size and resolution. In here, client will not get the actual image, it will only initiate the proxy and get the image information. No need, heavy length process of image rendering to get info.

Proxy classes is some thing like substitute of classes that it needs to protect. In the proxy patter, we have mainly three elements.

  • The class which we need to protect
  • The proxy class which protects our sensitive classes from client
  • Client who actually calls the proxy. (may or may not initiate with real client)

how to implement?

As, clients will access the subject class(the class we want to protect) via proxy class, so client will initiate the proxy class.

And it will call the method that have the necessary functions of subject class inside proxy class.

Now, the proxy class should have access the necessary methods of the subject class.

And, a common interface that describes what proxy & subject class share information and client will use the interface to initiate proxy.



Example :

Let’s think about the same financial transaction domain. Let’s design online account state.

Suppose, we have to build an online bank account statement view system where a client can see his account information and print that out and he can not transfer money.

So, we have to protect the account other information other than balance to get accessed.

So, we make a proxy class , say StatementInformer who actually make the actual user’s object and provides a balance information.

So, let’s start with an interface

public interface IBalanceInformation {
    public double getBalance();

And, implementing proxy class (actually the proxy class that is going to be called by client)

public class StatementInformer implements IBalanceInformation{
    public StatementInformer() {
   public double getBalance() {
        Account aRealaccount = new Account();
        return aRealaccount.getBalance();

So, now make a Account class implements the the same IBalanceInformation

public class Account implements IBalanceInformation{
    private String name;
    private String address;
    private String mobileNumber;
    private double balance = 1000.00;
    private String lastAccessDate;    
    public String getLastAccessDate() {        
        return lastAccessDate;    
    public String getName() {        
        return name;    
    public void setName(String name) {        = name;    
    public String getAddress() {        
        return address;    
    public void setAddress(String address) {        
        this.address = address;    
    public String getMobileNumber() {        
        return mobileNumber;    
    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;    
    public void setBalance(float balance) {        
        this.balance = balance;      
    public double getBalance() {        
        return balance;    

And, the client that contains Main class calling getBalance() from proxy. Here we see main initiating the proxy using IBalanceInformation.

public class Client {
    public static void main(String[] args) {
        IBalanceInformation myInformer = new StatementInformer();
        System.out.println("The account Balance is : "+myInformer.getBalance());

Java Example :

In the example I have added some extra method and proxy contractor to make us understand how it works.

C# Example :

In the example, I have changed the some method name and implementation way as c# works little different than java

When we use this?

  • When we need to protect a class from others to access or we want to implement protection from some classes to certain classes, we use proxy pattern.
  • When more than one object of a class exists ,the proxy pattern can be used to reduce memory, resource accessing, that provides results in tailored outcome as well as appropriate access and security.

It is used with Java RMI(remote method invocation) and security proxies.