[英]Strategy Design Pattern
I've got an AuthenticationHandler interface that supports URLConnection but now i am using Apache HTTP Client. 我有一个支持URLConnection的AuthenticationHandler接口,但现在我正在使用Apache HTTP Client。 I want to have a common interface for authentication for both connection types (URLConnection and HTTP Client) but they both have different parameters and function differently.
我想为两种连接类型(URLConnection和HTTP Client)提供一个通用的身份验证接口,但它们都有不同的参数和功能。
How would I design this? 我该如何设计呢? Is a Strategy pattern the way to go?
战略模式是否可行?
import java.net.URLConnection;
import java.util.List;
public interface AuthenticationHandler {
/**
* this needs to be called by everyone that needs direct access to a link which may have
* security access rules.
*/
void trustAll();
/**
*
* @param URLconnection where you set access state parameters or anything access related
* @param slice where you could get access config
* @param initializeSlice is true if you want the proxy to hibernate initialize all hibernated objects
* @return
* @throws ConnectionException
*/
void authenticate(URLConnection conn) throws ConnectionException;
List<String> getSingleCookie();
void setSingleCookie(List<String> singleCookies);
CookieManager getCookieManager();
void setCookieManager(CookieManager cookieManager);
boolean isKeepGeneratedCookie();
void setKeepGeneratedCookie(boolean keepGeneratedCookie);
}
My main concern is 我主要担心的是
void authenticate(URLConnection conn) throws ConnectionException;
where it was originally taking a URLConnection conn but now we also want to add support for HTTP client too. 它最初采用URLConnection conn但现在我们也想添加对HTTP客户端的支持。
For Strategy pattern you should use something like this: 对于策略模式,您应该使用以下内容:
public class AuthenticationHandlerImpl implements AuthenticationHandler {
private Authenticator authenticator;
void authenticate() throws ConnectionException {
authenticator.authenticate();
};
public void setAuthenticator(final Authenticator authenticator){
this.authenticator = authenticator;
}
}
interface Authenticator {
void authenticate();
void setLogin(String login);
void setPassword(String password);
}
class URLAuthenticator implements Authenticator {
public void authenticate() {
//use URLConnection
};
}
class HTTPClientAuthenticator implements Authenticator {
public void authenticate() {
//use HTTPClient
};
}
Usage example: 用法示例:
AuthenticationHandler handler = new AuthenticationHandlerImpl();
Authenticator authenticator = new HTTPClientAuthenticator();
//or
//Authenticator authenticator = new URLAuthenticator();
authenticator.setLogin(...);
authenticator.setPassword(...);
handler.setAuthenticator(authenticator)
handler.authenticate();
For creation Authenticator
you can use pattern FactoryMathod : 对于创建
Authenticator
您可以使用模式FactoryMathod :
class AuthenticatorFactory {
private AuthenticatorFactory(){}
//type of param may be enum or other
public static Authenticator createAuthenticator(... param) {
if (param == ...) {
return new URLAuthenticator();
} else if (param == ...) {
return new HTTPClientAuthenticator();
}
}
}
You can overload 你可以超载
void authenticate(HTTPClient client) throws ConnectionException;
or instead pass the in/out streams as parameter or work with a task-oriented callback like doLogin
and pass something like credentials. 或者将输入/输出流作为参数传递,或者使用像
doLogin
这样的面向任务的回调,并传递诸如凭证之类的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.