File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Need help with OO design Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with OO design" Watch "Need help with OO design" New topic

Need help with OO design

Philip Tao

Joined: Feb 08, 2006
Posts: 7
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.util.*;
import java.lang.*;

public abstract class Request {
String request;
Request(String 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

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Need help with OO design
It's not a secret anymore!