unleashed unleashed - 1 month ago 12
Java Question

Strategy Design Pattern

I've got an AuthenticationHandler interface that supports URLConnection but now i am using 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.

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.

Answer

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:

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();
        }
    }
}
Comments