This is the 12th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

You may not have used Linux, but you may have heard of the rm -rf /* command. This command will delete all files in the system, causing the operating system to be unusable and must be reinstalled.

Then in the actual management process, developers will not have such a high permission account, only the operation and maintenance personnel or the operation and maintenance boss can have such permission. Today we are going to use design patterns to talk about how the operating system can control access for different users.

Proxy schema definition

Definition: Provides a proxy or placeholder for another object to control access to it.

The proxy pattern is a structural design pattern that controls access to an existing service or object.

The definition itself is clear, and the proxy design pattern is used when we want to provide access control over functionality.

Proxy pattern implementation

At the beginning of our example, the operating system for performing the function of the command, we assume that the function in an object, if we want to have this feature is open to all users (client), that there may be a serious problem, because the client can send commands to delete some system files or change Settings for some of you don’t want, For example, run rm -rf /*.

How to solve this problem? We can create a proxy for this command-capable object and control access rights for different users in the proxy class.

Proxy mode main class

Since we write Java code based on interfaces, here are our interfaces and their implementation classes.

public interface CommandExecutor {

	public void runCommand(String cmd) throws Exception;
}
Copy the code

Concrete implementation of the interface:

import java.io.IOException;

public class CommandExecutorImpl implements CommandExecutor {
	@Override
	public void runCommand(String cmd) throws IOException {
                // Run the operating system command
		Runtime.getRuntime().exec(cmd);
		System.out.println("'" + cmd + "' command executed."); }}Copy the code

Proxy mode Proxy class

Now we want the admin user to be able to execute all of the commands, if not the admin user, only some of the commands. Here is a very simple proxy class implementation.

public class CommandExecutorProxy implements CommandExecutor {

	private boolean isAdmin;
    
	private CommandExecutor executor;
	
	public CommandExecutorProxy(String user){
		if("admin".equals(user) || "root".equals(user)) isAdmin=true;
		executor = new CommandExecutorImpl();
	}
	
	@Override
	public void runCommand(String cmd) throws Exception {
		if(isAdmin){
			executor.runCommand(cmd);
		}else{
			if(cmd.trim().startsWith("rm")) {throw new Exception("rm command is not allowed for non-admin users.");
			}else{ executor.runCommand(cmd); }}}}Copy the code

Proxy mode client


public class ProxyPatternTest {

	public static void main(String[] args){
		CommandExecutor executor = new CommandExecutorProxy("xiaohei");
		try {
			executor.runCommand("ls -ltr");
			executor.runCommand(" rm -rf /*");
		} catch (Exception e) {
			System.out.println("Exception Message::"+e.getMessage()); }}}Copy the code

The output of the sample agent design pattern program above is:

'ls -ltr' command executed.
Exception Message::rm command is not allowed for non-admin users.
Copy the code

This completes the control of user permissions.

A common use of the proxy design pattern is to control access or provide wrapper implementations for better performance.

The proxy pattern is also used in Java’s RMI package.

Proxy pattern class diagram

The difference between broker mode and adapter mode

You may find that the broker pattern is similar to the adapter pattern. What’s the difference?

The purpose of the adapter pattern is to change the object being adapted, whereas the proxy pattern does not change the object being proxied.

The difference between proxy mode and decorator mode

In contrast to the decorator pattern, the purpose of the decorator pattern is to enhance the function of the propped object, and the proxy pattern is to control the access of the propped object.

summary

The key point to understand about proxy patterns is that the purpose of proxy patterns is to control access to proxied objects.

I am xiaohei, if it is helpful for you to understand the agent mode, a thumbs-up is the biggest affirmation and encouragement for me!