代理模式

定义:

消费者通过代理对象间接与目标对象通信,由于代理对象符合目标对象的行为规范,所以这个过程相对消费者来说是透明的.

要点:

代理对象和目标对象要符合相同的行为规范

实例:

以一个HttpRequest请求为例,通过代理request控制不同user对r目标request中port,url,header的访问权限

1.定义HttpRequest规范

/**
 * 
 * 定义HTTP请求统一接口
 * 
 * @author ypqiao
 *
 */
public interface HttpRequest {
	
	/**
	 * 获取URL
	 * @return
	 * @throws Exception
	 */
	public String getURL() throws Exception;
	
	/**
	 * 获取端口
	 * @return
	 * @throws Exception
	 */
	public int getPort() throws Exception;
	
	/**
	 * 获取消息头
	 * @return
	 * @throws Exception
	 */
	public String getHeader() throws Exception;
	
	/**
	 * 获取消息体
	 * @return
	 * @throws Exception
	 */
	public String getBody() throws Exception;
	
	/**
	 * 获取当前登录用户
	 * @return
	 * @throws Exception
	 */
	public User getUser() throws Exception;

}

2.定义目标request类型

public class DefaultHttpRequest implements HttpRequest {
	
	
	private String url;
	private int port;
	private String header;
	private String body;
	private User user;
	
	
	public DefaultHttpRequest(){
		
	}
	
	public DefaultHttpRequest( String url, int port, 
							   String header, String body, User user ){
		this.url = url;
		this.port = port;
		this.header = header;
		this.body = body;
		this.user = user;
	}

	@Override
	public String getURL() throws Exception {
		// TODO Auto-generated method stub
		return url;
	}

	@Override
	public int getPort() throws Exception {
		// TODO Auto-generated method stub
		return port;
	}

	@Override
	public String getHeader() throws Exception {
		// TODO Auto-generated method stub
		return header;
	}

	@Override
	public String getBody() throws Exception {
		// TODO Auto-generated method stub
		return body;
	}

	public User getUser() {
		return user;
	}


}

3. 定义代理request类型

public class ProxyHttpRequest implements HttpRequest {
	
	
	private HttpRequest request;
	
	public ProxyHttpRequest(){
		
	}
	
	public ProxyHttpRequest( HttpRequest request){
		this.request = request;
	}
	

	@Override
	public String getURL() throws Exception {
		// TODO Auto-generated method stub
		if( request.getUser().authValue > 0 ){
			return request.getURL();
		}
		else {
			throw new RuntimeException(" no auth to access url ");
		}
			
		
	}

	@Override
	public int getPort() throws Exception {

		if( request.getUser().authValue > 1 ){
			return request.getPort();
		}
		else {	
			throw new RuntimeException(" no auth to access port ");
		}
	}

	@Override
	public String getHeader() throws Exception {
		
		if( request.getUser().authValue > 2 ){
			return request.getHeader();
		}
		else {	
			throw new RuntimeException(" no auth to access header ");
		}
		
	}

	@Override
	public String getBody() throws Exception {
		if( request.getUser().authValue > 3 ){
			return request.getBody();
		}
		else {	
			throw new RuntimeException(" no auth to access body ");
		}
	}

	@Override
	public User getUser() throws Exception {
		return request.getUser();
	}

}

4.定义user类

class User {
	
	String name;
	int authValue;
	
	User(){
		
	}
	
	User( String name, int authValue ){
		this.name = name;
		this.authValue = authValue;
	}
}

5. 定义客户端调用

class Client {
	
	public static void main( String[] args ) throws Exception{

		User user = new User("karl",4);
		HttpRequest request = new DefaultHttpRequest("http://localhost:8080/servlet",8080,"header","body",user);
		
		
		HttpRequest requestProxy = new ProxyHttpRequest(request);
		
		System.out.println(requestProxy.getURL());
		
	}
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值