views:

33

answers:

1

I wanna build server socket emulator, but I want implement some design pattern there. I will described my case study that I have simplified like these:

My Server Socket will always listen client socket. While some request message come from the client socket, the server emulator will response the client through the socket.

the response is response code. '00' will describe request message processed successfully, and another response code expect '00' will describe there are some error while processing the message request.

IN the server there are some UI, this UI contain check response parameter such as. response code timeout interval

  1. While the server want to response the client message, the response code taken from input parameter response form UI
  2. check the timeout interval, it will create sleep thread and the interval taken from timeout interval input from UI.

I have implement the function, but I create it in one class. I feel it so sucks. Can you suggest me what class / interface that I must create to refactor my code.

A: 

The need to refactor the code really depends on what task your server is performing based on the client request. If it is something simple then a single class may very well be the best design. If it is doing something more complicated then you may want to move the various operations that can be performed to various service classes. If your results are standard you could create an object (maybe enum?) to describe them.

This is the approach I have taken in one of my own applications. The server handles essentially only the IO between itself and the client. When the client sends a message the server parses it into a standard format "operation" object. This object is then passed to manager object which finds an appropriate "request servicing object". This object then does the actual work. When it is finished it generates a return object that describes the status/ results of the object. This is then taken by the server and formatted in an appropriate manner to send across the wire to the client.

Hopefully this can give you some ideas as to what might be appropriate for your application.

M. Jessup