• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Need help with OO design

Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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) {
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

(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic