Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with OO design

 
Philip Tao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,



i am writing a client/server application to help me to monitor several database servers. The server program accepts connection from clients, process client requests and return results to client.
The clients can be GUI application / Servlets and JSP. The server program runs on all database nodes.




The server tasks:


Waits for client connection in a loop.
Create a new thread for each new client connection.
The thread will process the client request and return results to client.



The client tasks:

Establish connection to server.
Send request to server.
Wait for server result.
Print result.



I have built the server to accept new connection and start new thread to read client request.
But the part that handle the request consists of case statements e.g.
if request = "readfile" then read the file and send content to client".
if request = "execute_command" then execute the command and send results to client".
if request = "get_database_free_space_info" then execute the command and send results to client".
... etc

I am still trying to think in OO way and i find it quite hard to change my mindset after so many years of procedural programming.

By using a bunch of case statements to handle different requests doesn't look OO to me.

I am thinking of creating a new abstract class called Request that contains one abstract method:

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;

public abstract class Request {
String request;
Request(String req) {
request=req;
}
public abstract void process() {
}
}


For each new request type it extends from the abstract class Request:

e.g. A ReadFile request:

public class ReadFileRequest extends Request {

public void process() {
read file ...

}

}


But now I am not sure how the server class will know which Request object it should create to handle the client request?




Thanks in advance


Philip
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You picked up a good thing to improve, and a good direction to go. Moving the method for each choice into its own class takes you to the Command pattern. But we still need to get rid of the big case statement. So this kind of thing is common:

You can load the CommandFactory through configuration, telling it that the request "readfile" is implemented by "com.mycompany.myproject.ReadFile". The factory might create one instance of each command class at startup, or create a new one every time you call it. Then you can add new commands and new requests without touching any of this code. Very slick.

Scroll down the page to the OO, UML, etc. forum. We talk this kind of stuff day & night.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic