需要帮助Http请求者的Java设计 [英] Need help with Java design of Http Requester

查看:205
本文介绍了需要帮助Http请求者的Java设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个设计问题。我有以下(相关)课程:

  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屏幕;
...

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

public run(){
...
}
}

如你所见,login()和authorize()都将运行以下代码:

  httpRequest = factory.createHttpRequest(url,method,loginScreen); 
httpRequest.start();然后,在HttpRequest之后的线程HttpRequest类中,run()方法将直接更新LoginScreen,然后在$ H pre $结果。例如:

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

然而,这证明是一个非常差的设计。我希望HttpRequest方法类在将来可以重复使用,我希望能够传递许多不同类型的类或屏幕。现在,它只适用于LoginScreen,它直接更新LoginScreen UI。



有没有人有任何建议,我如何更新这个设计?如果您可以提供尽可能多的细节,我将非常感谢,因为我尝试了许多不同的事情,我不断遇到问题。



谢谢!

解决方案

看看观察者模式。在这种情况下,您的UI屏幕将是观察者。


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 LoginScreen 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.

这篇关于需要帮助Http请求者的Java设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆