refactoring which pattern would you recommend

I have the following piece of code that I am tring to refactor

private String handleAction(String action, HttpServletRequest request,

XMLDelegate xmlDelegate)throws RemoteException{

if (action.equals("getHoldings")){

String portfolios = request.getParameter("portfolios");

String secTypes = request.getParameter("secTypes");

System.out.println("portfolios=" + portfolios);

System.out.println("secTypes=" + secTypes);

return (xmlDelegate.getHoldingsXML(portfolios, secTypes));

}elseif (action.equals("isAuthorized")){

UserPrivilegesAO upao =new UserPrivilegesAO(request

.getParameter(Fields.userId));

String userId = request.getParameter(Fields.userId);

String pass = request.getParameter(Fields.passwd);

System.out.println("userId=" + userId);

if (upao.authenticated(pass))

return ("YES");

else

return ("NO");

}elseif (action.equals("getPortfolios")){

return (xmlDelegate.getPortfoliosXML());

}elseif (action.equals("getSecTypes")){

return (xmlDelegate.getSecTypesXML());

}elseif (action.equals("allPortByMVRequest")){

return (xmlDelegate.getAllPortByMVXML());

}elseif (action.equals("portBreakDownBySecTypeRequest")){

String portfolioId = request.getParameter("portfolioId");

return (xmlDelegate.getPortBreakDownBySecTypeXML(portfolioId));

}elseif (action.equals("portSecTypeBreakDownBySecSubTypeRequest")){

String portfolioId = request.getParameter("portfolioId");

String secType = request.getParameter("secType");

return (xmlDelegate.getPortSecTypeBreakDownBySecSubTypeXML(

portfolioId, secType));

}elseif (action.equals("getNaicShiftsForAport")){

String portfolioId = request.getParameter("portfolioId");

String direction = request.getParameter("direction");

System.out.println("direction=" + direction);

System.out.println("portfolioId=" + portfolioId);

return (xmlDelegate

.getNaicShiftsForAportXML(portfolioId, direction));

}elseif (action.equals("getNaicDetail")){

String portfolioId = request.getParameter("portfolioId");

String secType = request.getParameter("secType");

String asof = request.getParameter("asof");

String direction = request.getParameter("direction");

System.out.println("portfolioId=" + portfolioId);

System.out.println("secType=" + secType);

System.out.println("asof=" + asof);

System.out.println("direction=" + direction);

return (xmlDelegate.getNaicDetailXML(portfolioId, secType, asof,

direction));

}elseif (action.equals("getPortsHasNaicShifted")){

return (xmlDelegate.getPortsHasNaicShiftedXML());

}elseif (action.equals("portSecSubTypeBreakDownByCusipRequest")){

String portfolioId = request.getParameter("portfolioId");

String secType = request.getParameter("secType");

String secSubType = request.getParameter("secSubType");

return (xmlDelegate.getPortSecSubTypeBreakDownByCusipXML(

portfolioId, secType, secSubType));

}

returnnull;

}

As you might see there are a lot of if else statements. I want to replace it with a pattern, which patter would you recommend. I was thinking of factory but there is not enough business logic to mandate use of that, and there would be a proliferation of newar empty classes if I try that. but at the same time all these if else statements look extremely ugly and would not be very extensible or maintainable. So any advose in this refard would be great!!!

[6486 byte] By [LisaMa] at [2007-11-27 5:56:30]
# 1

That looks like when a Swing GUI routes all its menu items to a single

actionPerformed method, no matter how unrelated and disparate they are...

First, is it possible to eliminate that method entirely and have the

client calling more specific methods from the get-go: getHoldings(), isAuthorised(), etc?

If not, one way to clean up the code is to have a Map<String, Action> that

maps the action string to the appropriate "action". Such a map has the

bonus of being configurable, versus the hard-wired code of your handleAction.

I don't know if this technique has a name -- the Command Pattern + a map?

Hippolytea at 2007-7-12 16:27:18 > top of Java-index,Java Essentials,Java Programming...
# 2

I've geeked with this idea before:

import java.util.HashMap;

public class HandlerTest {

// ActionHandler interface.

interface ActionHandler {

public static final String name = "";

public void handle(HashMap props);

}

// TestHandler (there would be many ActionHandlers in the map).

static final class TestHandler implements ActionHandler {

public static final String name = "TestHandler";

public TestHandler() { }

public void handle(HashMap props) {

System.out.println("Your username is " + props.get("username") +

" and your password is " + props.get("password") + ".");

}

}

// Main stuff.

public static HashMap<String, ActionHandler> handlerMap =

new HashMap<String, ActionHandler>();

public static TestHandler testHandler = new TestHandler();

// This might be abstracted away as well into its own class.

public static void handle(String name, HashMap props) {

ActionHandler handler = handlerMap.get(name);

handler.handle(props);

}

public static void main(String[] args) {

// initialize handlers. This would be in a constructor somewhere.

handlerMap.put(testHandler.name, testHandler);

// Set up a call to the handler. This might be in your handleAction method.

HashMap<String, String> params = new HashMap<String, String>();

params.put("username", "MyUserName");

params.put("password", "superSecretR00tPassword");

handle("TestHandler", params);

}

}

kevjavaa at 2007-7-12 16:27:18 > top of Java-index,Java Essentials,Java Programming...
# 3

> I've geeked with this idea before:

> > import java.util.HashMap;

>

>

> public class HandlerTest {

>

>// ActionHandler interface.

> interface ActionHandler {

> public static final String name = "";

> public void handle(HashMap props);

>}

> // TestHandler (there would be many

> ActionHandlers in the map).

> static final class TestHandler implements

> ActionHandler {

>

> public static final String name =

> "TestHandler";

>

> public TestHandler() { }

>public void handle(HashMap props) {

> System.out.println("Your username is " +

> props.get("username") +

> " and your password is " +

> props.get("password") + ".");

>}

>

>// Main stuff.

> public static HashMap<String, ActionHandler>

> handlerMap =

> new HashMap<String,

> ActionHandler>();

> public static TestHandler testHandler = new

> TestHandler();

>

> // This might be abstracted away as well into its

> own class.

> public static void handle(String name, HashMap

> props) {

>ActionHandler handler = handlerMap.get(name);

> handler.handle(props);

>}

>public static void main(String[] args) {

> // initialize handlers. This would be in a

> constructor somewhere.

> handlerMap.put(testHandler.name,

> testHandler);

>

> // Set up a call to the handler. This might

> be in your handleAction method.

> HashMap<String, String> params = new

> HashMap<String, String>();

>params.put("username", "MyUserName");

> params.put("password", "superSecretR00tPassword");

>handle("TestHandler", params);

> }

>

whats the purpose of having

public static final String name = "";

kilyasa at 2007-7-12 16:27:18 > top of Java-index,Java Essentials,Java Programming...