Java Reference
In-Depth Information
Listing 14‐2 has two classes: AbstractActionFactory and ActionFactory . The
AbstractActionFactory class creates an instance of the ActionFactory class. The Action Factory's
getAction method accepts an HttpServletRequest object, which contains a reference to the URI
of the requested location. The factory uses the URI to determine which Action
object to return to
the controller. You maintain a map of URI request paths and Action objects in the action Map . An
Action object is chosen from the map based on the URI request path and returned to the controller.
LISTING 14‐2: The Factory class
package com.devchronicles.mvc.plain;
public class AbstractActionFactory {
private final static ActionFactory instance = new ActionFactory();
public static ActionFactory getInstance() {
return instance;
package com.devchronicles.mvc.plain;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
public class ActionFactory {
private Map<String, Action> actions = new HashMap<>();
private Action action;
public ActionFactory() {
actions.put("GET/users", new HomeAction());
actions.put("GET/users/listusers", new ListUsersAction());
public synchronized Action getAction(HttpServletRequest request) {
String path = request.getServletPath() + request.getPathInfo();
String actionKey = request.getMethod() + path;
action = actions.get(actionKey);
if(action == null){
action = actions.get("GET/users");
return action;
object is that the concrete implementation provides an
implementation of the execute() method. This method performs business‐specii c logic required
to generate the page that the user requested. It may query a database to obtain data, perform
calculations, or generate a i le.
What is important in the Action
Search WWH ::

Custom Search