2

good evening all. ive been reading up on the command pattern and im wondering if it's a good fit for what i want to build.

essentially, i have a client that forms a socket connection with a server. there is 1 class 'foo' on my server that the client needs to invoke methods against.

lets say foo has 5 methods that my client will invoke. i have made the mistake in the past of marshalling an object that is demarshalled on the server. then i check a variable in the object and using a switch statement, the server logic can determine what action should be invoked.

i want to avoid this and so i believe the command pattern may help me. but, for my example 'foo' class on the server, do i need to create a command class for each method that is to be invoked in foo? should the class that is sent from the client to the server be a command class? will i only need 1 receiver in this case? - the foo class?

thanks very much for your help and sorry about the 'foo' class name here. i havent got any concrete class names yet!

best regards

3
  • 3
    Are there 'Shift' or 'Caps Lock' keys on your keyboard? Feb 15, 2011 at 22:30
  • 1
    im writing from my iphone, why? Feb 15, 2011 at 22:57
  • 1
    Because writing proper sentences starting with capital letters (as well as writing "I") is important if you want people to take your question serious.
    – hlovdal
    Feb 15, 2011 at 23:37

1 Answer 1

4

No matter what you do, you will not be able to entirely get rid of that switch() statement. Command pattern would suggest you do the following:

abstract class AbstractCommand() {
  abstract void execute();
}
class ConcreteCommand extends AbstractCommand {
  // execute implementation
}
// more command classes as needed; probably one per each of your method calls

Then you have a command factory:

class CommandFactory {
  AbstractCommand createCommandForMessage(Message m) {
    // ... switch() goes here
  }
}

And your message receiving part becomes as simple as this:

public class MessageReceiver {
  public void work() {
    while (true) {
      Message m = receiveMessage();
      AbstractCommand command = commandFactory.createCommandForMessage(m);
      command.execute();
    }
  }
}

What's nice about this is that you cleanly separate the actual logic of commands (execute() method implementations) from the logic that decides which command to use, from the logic that knows how to receive messages on the network.

4
  • Hi there, thanks for the reply. I thought (from what I read) that the command pattern eliminates the need for a switch or multiple else/if conditional logic. Also I'm not quite sure I follow the ccommandFactory that you use here. Can you explain it a little as it seems to be different from other examples I've read about the Command pattern. Many thanks again. Feb 16, 2011 at 9:30
  • Also, is the command pattern a suitable choice as a means of a client invoking server actions via a socket? Thanks Feb 16, 2011 at 10:27
  • 1
    You won't be able to eliminate the switch() entirely: depending on the type of packet you receive, you need to process it differently. you could use a map of <MessageType, AbstractCommand>, for instance, but logically it's still the same thing. CommandFactory is where this switch() actually happens, so your code is cleanly separated into 3 pieces: 1. Message receiver, 2. Command creator, and 3. Command logic. I have used this exact pattern for socket communication in the past, and found it flexible and easy to extend, so I'd recommend it.
    – iluxa
    Feb 16, 2011 at 23:53
  • You did not Invoker which decouples Sender and Receiver Feb 11, 2016 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.