开发者

Need help with Java design of Http Requester

开发者 https://www.devze.com 2022-12-18 14:34 出处:网络
I am running into a design problem. I have the following (pertinent) classes: class LoginScreen { public login() {

I am running into a design problem. I have the following (pertinent) classes:

 class LoginScreen {
   public login() {
      httpRequest = factory.createHttpRequest(url, method, this);
      httpRequest.start();
   }
   public authorize() {
      httpRequest = factory.createHttpRequest(url, method, this);
      httpRequest.start();
   }
   public requestSucceeded(){...}
   public requestFailed(){...}
 }

class HttpRequest extends Thread {

   LoginScreen screen;
   ...

   public HttpRequest(url, method, screen) {
      this.url = url;
      this.method = method;
      this.screen = screen
   }

   public run() {
     ...
   }
}

As you can see, both login() and authorize() will run the following code:

httpRequest = factory.createHttpRequest(url, method, loginScreen);
httpRequest.start();

And then, in the threaded HttpRequest class after the HttpRequest, the run() method will update LoginScreen directly with its results. For example:

screen.requestSucceeded(baos.toByteArray(), contentType);

This is proving to be a very poor design, however. I want the HttpRequest method class to be reusable in the future, and I want to be able to pass it many different types of classes, or screens. Right now, it only works with LoginScreen, and it is directly updating the LoginScre开发者_运维知识库en UI.

Does anyone have any suggestions as to how I can update this design? If you could provide as much detail as possible, I'd greatly appreciate it as I have tried many different things and I keep running into problems.

Thanks!


Have a look at Observer pattern. Here, in this case, your UI screens will be observers.

0

精彩评论

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

关注公众号