What are the best patterns/designs for stateful API development?

Posted by Svante on Programmers See other posts from Programmers or by Svante
Published on 2011-02-22T13:11:28Z Indexed on 2011/02/22 15:33 UTC
Read the original article Hit count: 235

Filed under:
|
|
|

I am about to implement a API for my TCP/IP server written in Java. Right now I have a temporary method that takes a String, executes a command based on the String and returns a String basically like the following.

public void communicate(BufferedReader in, PrintWriter out) {
    while(true) {
        out.println(handleCommand(in.readLine()));
    }
}

private String handleCommand(String command) {
    if (command.equals("command1") {
        // do stuff
        return "Command 1 executed";
    } else if (command.equals("command2") {
        // do some other stuff
        return "Command 2 executed";
    }
}

I really want to do something more extensible, smarter and stateful, so I could handle more complex and stateful commands and without the method/class getting bloated.

How would you start?

Suggestions, ideas, or links for further reading are very welcome.

© Programmers or respective owner

Related posts about java

Related posts about design