代理设计模式案例_代理设计模式

代理设计模式案例

Proxy Design pattern is one of the Structural design pattern and in my opinion one of the simplest pattern to understand.

代理设计模式是结构设计模式之一,在我看来,这是最容易理解的模式之一。

代理设计模式 (Proxy Design Pattern)

proxy design pattern

Proxy design pattern intent according to GoF is:


根据GoF的代理设计模式意图是:

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

为另一个对象提供代理或占位符,以控制对其的访问。

The definition itself is very clear and proxy design pattern is used when we want to provide controlled access of a functionality.

定义本身非常清晰,当我们要提供功能的受控访问时,将使用代理设计模式。

Let’s say we have a class that can run some command on the system. Now if we are using it, its fine but if we want to give this program to a client application, it can have severe issues because client program can issue command to delete some system files or change some settings that you don’t want.

假设我们有一个可以在系统上运行某些命令的类。 现在,如果我们正在使用它,就很好了,但是如果我们想将此程序提供给客户端应用程序,则它可能会遇到严重问题,因为客户端程序可以发出命令以删除某些系统文件或更改某些不需要的设置。

Here a proxy class can be created to provide controlled access of the program.

在这里,可以创建代理类以提供对程序的受控访问。

代理设计模式-主类 (Proxy Design Pattern – Main Class)

Since we code Java in terms of interfaces, here is our interface and its implementation class.

由于我们使用接口对Java进行编码,因此这里是我们的接口及其实现类。

CommandExecutor.java

CommandExecutor.java

package com.journaldev.design.proxy;

public interface CommandExecutor {

	public void runCommand(String cmd) throws Exception;
}

CommandExecutorImpl.java

CommandExecutorImpl.java

package com.journaldev.design.proxy;

import java.io.IOException;

public class CommandExecutorImpl implements CommandExecutor {

	@Override
	public void runCommand(String cmd) throws IOException {
                //some heavy implementation
		Runtime.getRuntime().exec(cmd);
		System.out.println("'" + cmd + "' command executed.");
	}

}

代理设计模式–代理类 (Proxy Design Pattern – Proxy Class)

Now we want to provide only admin users to have full access of above class, if the user is not admin then only limited commands will be allowed. Here is our very simple proxy class implementation.

现在,我们只想提供具有上述类的完全访问权限的admin用户,如果该用户不是admin,则将只允许使用有限的命令。 这是我们非常简单的代理类实现。

CommandExecutorProxy.java

CommandExecutorProxy.java

package com.journaldev.design.proxy;

public class CommandExecutorProxy implements CommandExecutor {

	private boolean isAdmin;
	private CommandExecutor executor;
	
	public CommandExecutorProxy(String user, String pwd){
		if("Pankaj".equals(user) && "J@urnalD$v".equals(pwd)) 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);
			}
		}
	}

}

代理设计模式客户端程序 (Proxy Design Pattern Client Program)

ProxyPatternTest.java

ProxyPatternTest.java

package com.journaldev.design.test;

import com.journaldev.design.proxy.CommandExecutor;
import com.journaldev.design.proxy.CommandExecutorProxy;

public class ProxyPatternTest {

	public static void main(String[] args){
		CommandExecutor executor = new CommandExecutorProxy("Pankaj", "wrong_pwd");
		try {
			executor.runCommand("ls -ltr");
			executor.runCommand(" rm -rf abc.pdf");
		} catch (Exception e) {
			System.out.println("Exception Message::"+e.getMessage());
		}
		
	}

}

Output of above proxy design pattern example program is:

上面的代理设计模式示例程序的输出为:

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

Proxy design pattern common uses are to control access or to provide a wrapper implementation for better performance.

代理设计模式的常见用途是控制访问或提供包装器实现以提高性能。

Java RMI package uses proxy pattern. That’s all for proxy design pattern in java.

Java RMI包使用代理模式。 这就是Java中的代理设计模式。

翻译自: https://www.journaldev.com/1572/proxy-design-pattern

代理设计模式案例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值