Wednesday, November 3, 2010

Command Design Pattern

Command Design Pattern: Check the Configurable Command Pattern Implementation. You can pass parameters to command objects and get values after executing command.
Uses: Make a command.properties file and put command and implementer class like
 TESTCOMMAND=org.paandav.command.TestCommand

package org.paandav.command;
public interface Command {
 public Object execute(Object params);
}


package org.paandav.command;
public class CommandException extends Exception {
 private static final long serialVersionUID = 1L;
 public CommandException() {
  super();
 }
 public CommandException(String msg) {
  super(msg);
 }
}


package org.paandav.command;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CommandExecutor {
 private static final String COMMAND_FILE = "command.properties";
 private static CommandExecutor cmdex = null;
 private static Map<String, String> commandMap = null;
 private CommandExecutor() throws CommandException {
  loadCommondMap();
 }
 public static CommandExecutor getInstance() throws CommandException {
  try {
   if (cmdex == null) {
    synchronized (CommandExecutor.class) {
     if (cmdex == null) {
      cmdex = new CommandExecutor();
     }
    }
   }
  } catch (Exception ex) {
   throw new CommandException(ex.getMessage());
  }
  return cmdex;
 }
 public Object executeCommand(String commondKey, Object params)
   throws CommandException {
  Object retObject = null;
  String cmdClsName = null;
  Command cmdObject = null;
  Class<?> cls = null;
  try {
   cmdClsName = commandMap.get(commondKey);
   if (cmdClsName == null || "".equals(cmdClsName.trim())) {
    throw new CommandException("Command class nor found");
   }
   cls = Class.forName(cmdClsName);
   cmdObject = (Command) cls.newInstance();
   retObject = cmdObject.execute(params);
  } catch (Exception ex) {
   throw new CommandException(ex.getMessage());
  }
  return retObject;
 }
 private static void loadCommondMap() throws CommandException {
  File pfile = new File(COMMAND_FILE);
  Properties pro = null;
  InputStream is = null;
  try {
   if (pfile.exists()) {
    pro = new Properties();
    is = new FileInputStream(pfile);
    pro.load(is);
    if (pro.size() > 0) {
     commandMap = new HashMap<String, String>();
     Set<String> keys = pro.stringPropertyNames();
     for (String key : keys) {
      if (null != pro.getProperty(key)
        && !"".equals(pro.getProperty(key).trim())) {
       commandMap.put(key, pro.getProperty(key));
      }
     }
    }
   } else {
    throw new CommandException("Could not find " + COMMAND_FILE);
   }
  } catch (Exception ex) {
   throw new CommandException(ex.getMessage());
  }
 }
}

package org.paandav.command;
public class TestCommand implements Command {
 @Override
 public Object execute(Object params) {
  if (params != null) {
   System.out.println("Params received" + params.toString());
  }
  return "TestCommand executed";
 }
}

package org.paandav.command;
public class CommandTest {
 public static void main(String[] args) throws Exception {
  CommandExecutor ex = CommandExecutor.getInstance();
  String[] arr = new String[] { "One", "Two" };
  Object retValue = ex.executeCommand("TESTCOMMAND", arr);
  System.out.println(retValue);

 }
}

No comments:

Post a Comment