Results 1 to 5 of 5
  1. #1
    Galian is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default Better way of Command pattern implementation

    Hi everybody!

    I'm trying to implement Command pattern, and I'm stuck solving one design issue...

    So I have Command.java file, which contains next classes:

    PHP Code:
    public class Command {
    	public void execute(CommandHandler handler) {
    		handler.handleCommand(this);
    	}
    }
    
    class StartCommand extends Command {
    	//TODO: command specific stuff
    }
    
    class StopCommand extends Command {
    	//TODO: command specific stuff
    }

    Second file CommandHandler.java which contains next class:

    PHP Code:
    public class CommandHandler {
    	public void handleCommand(Command command) {
    		System.out.println("CommandHandler.Command");	
    	}
    	
    	public void handleCommand(StartCommand command) {
    		System.out.println("CommandHandler.StartCommand");
    	}
    
    	public void handleCommand(StopCommand command) {
    		System.out.println("CommandHandler.StopCommand");
    	}
    }

    I want to use this classes in the next way:

    PHP Code:
    ArrayList<Command> t = new ArrayList<Command>();
    
    t.add(new StartCommand());
    t.add(new StopCommand());
    	
    //Executing commands
    CommandHandler handler = new CommandHandler();
    
    for (Command cmd: t) {
    	cmd.execute(handler);
    }

    and of course it don't work...
    Every time when I call cmd.execute(handler) it calls execute method of Command class, and call of handler.handleCommand(this) every time goes to handleCommand(Command command). I was hoping that if I will call handler.handleCommand, and will pass as a argument base class reference on child object, Java will dispatch call to the corresponding handleCommand method, but it's not.
    It's working perfectly fine, if I will change Commands class implementation, and in every Command class will overload execute method, like this:

    PHP Code:
    class StartCommand extends Command {
    	public void execute(CommandHandler handler) {
    		handler.handleCommand(this);
    	}
    }
    Problem is that I don't like this solution because of two reasons:
    1. I don't want add execute function every time, when I create new Command class, with exactly the same body and signature;
    2. Theoretically this method already in base class, and maybe there is a better solution for this case.


    Thanks a lot beforehand for your time, and sorry for so long question.

    P.S. I wrapped code in PHP tag intentionally, because highlighting make code easily readable.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    That doesn't look like a Command Pattern as I've seen it used before (but please note that I'm not an expert in this). For one, it's usually done via an interface (at least when I've ever seen it used), and your execute method seems to be hard-code set. In fact one of your major issues is that you don't want to create a unique execute method for each Command class, but this is in fact what should be done (AFAIK).

    Maybe I'm misinterpreting things, but I would imagine that Command should be an interface with a single execute method that takes no parameters, that the CommandHandler class has a stop and a start method, that your StartCommand and StopCommand classes implement Command and take a CommandHandler parameter in their constructors and that they call the handler's start and stop commands respectively in their execute methods. Forgive me if I'm confusing the issue.

    A simple example:

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class CommandTest {
       public static void main(String[] args) {
          List<Command> commandList = new ArrayList<Command>();
          CommandHandler handler = new CommandHandler();
          
          commandList.add(new FooCommand(handler));
          commandList.add(new BarCommand(handler));
          
          for (Command command : commandList) {
             command.execute();
          }
       }
    }
    
    interface Command {
       void execute();
    }
    
    class FooCommand implements Command {
       private CommandHandler handler;
       
       public FooCommand(CommandHandler handler) {
          this.handler = handler;
       }
       
       public void execute() {
          handler.foo();
       }
    }
    
    class BarCommand implements Command {
       private CommandHandler handler;
       
       public BarCommand(CommandHandler handler) {
          this.handler = handler;
       }
       
       public void execute() {
          handler.bar();
       }
    }
    
    class CommandHandler {
    
       public void foo() {
          System.out.println("foo in handler class");
       }
       
       public void bar() {
          System.out.println("bar in handler class");
       }
       
    }
    Last edited by Fubarable; 11-06-2010 at 04:36 AM.

  3. #3
    Galian is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Hi Fubarable,

    thanks a lot for the response, your understanding is right. This is really doesn't seems like pure Command pattern, it's something like Command + Visitor. Yes, what I wanted is to have one execute method in base class, maybe it's really not possible. Code that you have posted it's also good solution, friend of mine also suggested me to do it in exactly the same way.

    Thanks for your time, if I will find other solution, will post it here.

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I've always done it via an abstract Command class that has a command identifier, and then an abstract method to execute the command. You can then extend the class, and feed instances of those classes to a CommandHandler. The CommandHandler then takes a command, figures out which instance of Command in its list has that identifier, and executes. I've used this pattern in Java, C++, and Python, so its definitely reliable.

    Basically, I reverse Fubarables design and have everything go through one method in the CommandHandler and call different implementations of Command, instead of executing a Command and calling a specific method in the CommandHandler. It's basically a listener-based command implementation.

    So, every time you write a new Command, you will have to implement the execute method, but you don't need to redirect the call to another method. You CAN just call another method, but you don't need to. (I guess the same is true of the first two patterns in the thread as well)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    Galian is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Singing Boyo View Post
    I've always done it via an abstract Command class that has a command identifier, and then an abstract method to execute the command. You can then extend the class, and feed instances of those classes to a CommandHandler. The CommandHandler then takes a command, figures out which instance of Command in its list has that identifier, and executes. I've used this pattern in Java, C++, and Python, so its definitely reliable.

    Basically, I reverse Fubarables design and have everything go through one method in the CommandHandler and call different implementations of Command, instead of executing a Command and calling a specific method in the CommandHandler. It's basically a listener-based command implementation.

    So, every time you write a new Command, you will have to implement the execute method, but you don't need to redirect the call to another method. You CAN just call another method, but you don't need to. (I guess the same is true of the first two patterns in the thread as well)
    Hi Singing Boyo,
    thanks for the answer, don't really understand CommandHandler implementation in your case, is it something like this?

    Java Code:
    abstract class Command {
    	abstract void execute();
    	protected int _cmdID;
    	
    	public int getCommand() {
    		return _cmdID;
    	}
    }
    
    class FirstCommand extends Command {
    	public FirstCommand(int cmdID) {
    		_cmdID = cmdID;
    	}
    	
    	@Override
    	void execute() {
    		//TODO: command specific stuff
    	}
    }
    
    class SecondCommand extends Command {
    	public SecondCommand(int cmdID) {
    		_cmdID = cmdID;
    	}
    	
    	@Override
    	void execute() {
    		//TODO: command specific stuff
    	}
    }
    
    class CommandHandler {
    	public void handle(Command cmd) {
    		//What's here?
    	}
    }
    Sorry for misunderstanding.

    Thanks a lot!

Similar Threads

  1. Class pattern to generate following pattern:-
    By vxs in forum New To Java
    Replies: 5
    Last Post: 07-14-2010, 11:15 PM
  2. Command design pattern
    By hannes in forum AWT / Swing
    Replies: 10
    Last Post: 01-28-2010, 06:41 AM
  3. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •