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

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

问题描述

我遇到了一个设计问题。我有以下的(相关)类:

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() {
     ...
   }
}

正如你可以看到,无论是登录()和授权()将运行以下code:

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

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

然后,在Htt的prequest后螺纹型Htt prequest类的run()方法将直接更新LoginScreen其结果。例如:

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);

这被证明是一个非常贫穷的设计,但是。我想要的Htt prequest方法类可重用的未来,我希望能够通过很多不同的类型的类,或屏幕。现在,它仅与LoginScreen工作的,并且它被直接更新LoginScreen UI

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.

有没有人有任何建议,我怎么可以更新这个设计?如果你能提供尽可能详细越好,我会非常AP preciate它,因为我已经尝试了许多不同的事情,我一直运行到的问题。

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.

谢谢!

推荐答案

看一看 Observer模式。在这里,在这种情况下,你的UI屏幕将观察者

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

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

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