开发者

Implementing Request Response design

I am trying to implement a request/response client. Here is the basic implementation for it.

Class Request
{
    prepareRequest1();
    prepareRequest2();
}

Class Response
{
    processResponse1();
    processResponse2();
}

Class Client
{
    connect();
    sen开发者_开发百科dRequest();
}

myrequest = new Request();
client = new Client (myserver);
my $rawResponse1 = client.sendRequest (myrequest.prepareRequest1());

myresponse = new Response();
myresponse.processResponse1 ($rawResponse1);

I have three classes namely - Request, Response and Client. Request class contains a function specific to a request. These request are the XML requests. There XML request does not have anything in common except few initial tags (e.g. sessionid etc). All other XML parameters are specific to the type of request. So, Currently I am handing it by creating a separate function for each request. I understand that it has a scalability issue, but please suggest me some other best approach I can apply here.

Similar to the request, the response to each request require a specific treatment. so I have a separate function specific to each type of response I am expecting. Generally you can assume that there is a 1:1 mapping between request and response.

Last is the Client class which hands for connection.

I have almost 50+ such request and response, and I am planning to implement with this approach. As I am in initial stage of development, please suggest me some improvement/best practices to implement such request/response.


Its best if you keep the mapping of the request and response outside of your code. Easier to manage. Keep an xml config file that stores the class names of your request and response tied to a particular type of xml message. You can then obtain the class names and go to that class to process your request and response. Its best to have separate classes to handles these messages rather than separate methods. Thats more scalable. Ensure you have interface for all your request classes and the same for your response classes. This will make it easy to swap your request responses classes easily by modifying the xml config file..

Hope you got it :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜