Wednesday, 20 November 2013

Proxy Design pattern - Implementation [Protection Proxy]

🚀 Build Secure & Robust Java Applications!

Looking for beginner-friendly design pattern tutorials? Subscribe to Ram N Java for clean code and practical architecture guides!

🔔 Subscribe to Ram N Java

What is the Protection Proxy Design Pattern?

In software systems handling sensitive data or privileged actions, not every user or component should have unrestricted access to core business objects. The Protection Proxy Pattern acts as an intermediary or gatekeeper that checks access rights and permissions before delegating requests to the underlying real object.

How the Protection Proxy Works

  • Subject Interface: Outlines the standard methods available to clients.
  • Real Subject: Implements the actual operational logic containing business rules or sensitive actions.
  • Protection Proxy: Sits in front of the Real Subject to evaluate credentials, user roles, or permission levels before forwarding execution.

Key Benefits of Protection Proxies

  1. Granular Access Control: Enforces role-based permissions (such as Read-Only vs. Admin) transparently.
  2. Separation of Concerns: Security verification logic stays isolated from core business rules.
  3. Fail-Safe Execution: Unauthorized requests are blocked early without wasting processing power on the real object.

Explore More Proxy Pattern Guides

Continue mastering design pattern concepts with these tutorials from our channel:


Click here to watch in Youtube : https://www.youtube.com/watch?v=EzQq_MeMAWE

Click the below Image to Enlarge
Proxy Design pattern - Implementation [Protection Proxy]
Proxy Design pattern - Implementation [Protection Proxy] - Class Diagram







































User.java

public class User
{

private String userName;
private String passWord;
private String designation;

public User(String userName, String passWord, String designation)
{
super();
this.userName = userName;
this.passWord = passWord;
this.designation = designation;
}

public String getUserName()
{
return userName;
}

public void setUserName(String userName)
{
this.userName = userName;
}

public String getPassWord()
{
return passWord;
}

public void setPassWord(String passWord)
{
this.passWord = passWord;
}

public String getDesignation()
{
return designation;
}

public void setDesignation(String designation)
{
this.designation = designation;
}

@Override
public String toString()
{
return "User [userName=" + userName + ", passWord=" + passWord + ", designation=" + designation + "]";
}

}


IFolder.java

public interface IFolder
{
public void performReadOrWriteOperations();
}

Folder.java

public class Folder implements IFolder
{

@Override
        public void performReadOrWriteOperations()
        {
System.out.println(this.getClass()+" : Performing read or write operation on the folder");          
        }
}

FolderProxy.java
public class FolderProxy implements IFolder
{
private Folder folder;
private User   user;

public FolderProxy( User user )
{
this.user = user;
}

@Override
public void performReadOrWriteOperations()
{

if( user.getDesignation().equalsIgnoreCase("CEO") || user.getDesignation().equalsIgnoreCase("Manager") )
{
folder = new Folder();
System.out.println(this.getClass()+" : Folder Proxy makes call to the RealFolder 'performReadOrWriteOperations method'");
folder.performReadOrWriteOperations();
}
else
{
System.out.println(this.getClass()+": Folder proxy says 'You don't have access to this folder'");
}

}

}

Client.java

public class Client
{
public static void main( String[] args )
{
new Client().folderAccess();
}

private  void folderAccess()
        {
       System.out.println(this.getClass()+" : Client passing user with designation 'CEO' to folderproxy");
User john = new User("John", "John123","CEO");
FolderProxy folderProxy = new FolderProxy(john);
folderProxy.performReadOrWriteOperations();
System.out.println("\n************************************************************           ***********************\n");
System.out.println(this.getClass()+" : Client passing user with designation 'Developer' to folderproxy");
User raj = new User("Raj", "Raj123","Developer");
FolderProxy folderProxyWrong = new FolderProxy(raj);
folderProxyWrong.performReadOrWriteOperations();
        }
}

Output

class Client : Client passing user with designation 'CEO' to folderproxy
class FolderProxy : Folder Proxy makes call to the RealFolder 'performReadOrWriteOperations method'
class Folder : Performing read or write operation on the folder

*********************************************************************************

class Client : Client passing user with designation 'Developer' to folderproxy
class FolderProxy: Folder proxy says 'You don't have access to this folder'

See also:

  • Proxy Design Pattern - Introduction
  • Proxy Design pattern - Real Time Example[ATM]
  • Proxy Design pattern - Real Time Example [Proxy Server]
  • Proxy Design pattern - Class Diagram
  • Proxy Design pattern - Sequence Diagram
  • Proxy Design pattern - Implementation [Remote Proxy]
  • Proxy Design pattern - Implementation[Virtual Proxy]
  • Proxy Design Pattern - Key Points
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials