定义:
消费者通过代理对象间接与目标对象通信,由于代理对象符合目标对象的行为规范,所以这个过程相对消费者来说是透明的.
要点:
代理对象和目标对象要符合相同的行为规范
实例:
以一个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());
}
}