Thursday, 15 January 2015

Proxy Design Pattern Implementation

Proxy Design Pattern

Hibernate                                                                                              Core Java


Question :- What is Proxy Design Pattern

As per GOF :-"Provide a surrogate or placeholder for another object to control access over it."

This design pattern helps us to create a wrapper over real object. It helps to control in accessing of the real object with the help of proxy object. 

  • This(Proxy design) pattern works on the principal of exposing Java object through a proxy instead of real object.

  • For securing an actual object this pattern can be used. Service provider does not want actual class to be visible to any client Instead It would be shared as per Client contract agreement . Service provider may agree to share only a part of Service with it's client and for that It may expose a different contract in the form of interface in java.

  • Client is not aware of actual object and through Proxy only relevant behavior of actual object will be exposed to client.

  • This pattern will help for lazily loading of an object. Data will be loaded only when it is actually required in an operation till then only proxy to real object will be kept as reference. So as to invoke desired method later and retrieve the actual instance.


The main intention of a proxy is to control access to the target object rather then to enhance the functionality of the target object.There are ways that proxies can provide access control which includes : -
  • Synchronization
  • Authentication
  • Remote Access
  • Lazy instantiation

Now considering a example where we can execute few command to open a specified application. Here the twist is opening any normal application is fine but opening a specific application should be controlled and we should provide access to those users who is having all type of permissions. Here a proxy class can be used to provide controlled access to those programs.

AppExecutor.java

package com.gaurav.designpattern.proxy;
/**
 * @author Gaurav
 * 
 */

public interface AppExecutor {
public boolean openApplication(String appName) throws Exception;
}


AppExecutorImpl.java

package com.gaurav.designpattern.proxy;
/**
 * @author Gaurav
 * 
 */

import java.io.IOException;

public class AppExecutorImpl implements AppExecutor{

@Override
public boolean openApplication(String appName) throws IOException {
boolean successFlag = false;
try{
Runtime.getRuntime().exec(appName);
successFlag = true;
}catch(IOException ioe){
System.out.println("Error message is : "+ioe.getMessage());
}
return successFlag;
}
}


AppExecutorProxy.java

package com.gaurav.designpattern.proxy;
/**
 * @author Gaurav
 * 
 */

public class AppExecutorProxy implements AppExecutor{
private boolean IsManagerLevelPermission = false;
    private AppExecutor appExecutor;
     
    public AppExecutorProxy(String userName, String password){
        if("Gaurav".equals(userName) && "Kumar#2013".equals(password)) 
        IsManagerLevelPermission = true;
        appExecutor = new AppExecutorImpl();
    }
     
    @Override
    public boolean openApplication(String appName) throws Exception {
        boolean success = false;
    if(IsManagerLevelPermission){
            appExecutor.openApplication(appName);
            success = true;
        }else{
            if(appName.trim().startsWith("mspaint")){
                throw new Exception("mspaint application is only available to managers.");
            }else{
                appExecutor.openApplication(appName);
                success = true;
            }
        }
    return success;
    }
}


ProxyDesignPatternDemo.java

package com.gaurav.designpattern.proxy;
/**
 * @author Gaurav
 * 
 */

public class ProxyDesignPatternDemo {
public static void main(String[] args){
        AppExecutor executor = new AppExecutorProxy("Gaurav", "welcome321");
        try {
           executor.openApplication("calc");
           executor.openApplication("notepad");
           boolean isExecuted = executor.openApplication("mspaint");
           System.out.println("Is mspaint app is executed : "+isExecuted);
        } catch (Exception e) {
            System.out.println("Error occurred is : "+e.getMessage());
        }
         
    }
}


Result:-

Error occurred is : mspaint application is only available to managers.

Note:- Here we are passing the wrong password which is not matching with the specified password.

Spring MVC                                           Spring Transaction                                        Web-Services

No comments:

Post a Comment