Wednesday, 20 April 2016

Proxy Design Pattern in Java

Gang of Four Definition:

   Provide a surrogate or placeholder for another object to control access to it.

Computer World Example:

Consider an ATM implementation for a bank. Here we will find multiple proxy objects. Actual bank information will be stored in a remote server. We must remember that in the real programming world, the creation of multiple instances of a complex object (heavy object) is very costly.

In such situations, we can create multiple proxy objects (which must point to an original object) and the total creation of actual objects can be carried out on a demand basis. Thus we can save both memory and creational time.

Explanation:

In the following program, we are calling the doSomework() function of the proxy object, which in turn calls the doSomwork() of the concrete object. With the output, we are getting the result directly through the concrete object.

For this example, I have created different packages, proxy implementations will be reside in package Proxy, and concrete classes will be in package concrete.

UML Class Diagram:


Sample Implementation:

Create an interface which will be implemented by both Proxy and Actual class.
package com.speakingcs.designpatterns.proxypattern.concrete;

public interface Subject {
    
    public abstract void doSomeWork();

}
Concrete Class:
package com.speakingcs.designpatterns.proxypattern.concrete;

public class ConcreteSubject implements Subject{

    @Override
    public void doSomeWork() {
        System.out.println("I am from Concrete subject");
    }

}
Proxy Class:
package com.speakingcs.designpatterns.proxypattern.proxy;

import com.speakingcs.designpatterns.proxypattern.concrete.ConcreteSubject;
import com.speakingcs.designpatterns.proxypattern.concrete.Subject;

public class Proxy implements Subject{
    
    ConcreteSubject cs = null;

    @Override
    public void doSomeWork() {
        System.out.println("inside proxy method");
        //lazy initialization
        if(cs == null){
            cs = new ConcreteSubject();
        }
        
        // delegating method call to actual concrete object
        cs.doSomeWork();        
    }

}

Testing the code:
package test;

import com.speakingcs.designpatterns.proxypattern.proxy.Proxy;

public class ProxyPatternEx {
    
    public static void main(String[] args) {
        System.out.println("*** Proxy Pattern Demo ***");
        Proxy px = new Proxy();
        
        px.doSomeWork();
    }

}
Different Types Of Proxies:

There are different types of Proxies exist, those are

1. Remote Proxies: They will hide the actual object which is in a different address space.
2. Virtual Proxies: They are used to perform optimization techniques like the creation of a heavy object on a demand basis.
3. Protection Proxies: They greatly deal with different access rights.
4. Smart reference: It can also perform some additional housekeeping work when an object is accessed. A typical operation is counting the number of references to the actual object.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.