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:
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
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