【问题标题】:Using the Command Pattern in Android in combination with an Http Request Handler to handle its result结合使用 Android 中的命令模式和 Http 请求处理程序来处理其结果
【发布时间】:2016-02-16 18:10:41
【问题描述】:

我的实际问题在页面底部,可能不需要您查看我的所有代码。

所以我想我想在我的 Android 应用程序中使用命令模式。我有一个类HttpRequestHandler,其中extends AsyncTask,因此有doInBackgroundonPostExecute 方法。 HttpRequestHandler 接受一个 URL 和一个 HttpRequest 类型作为参数。

我想要做的是将一个方法(以接口的形式)传递给HttpRequestHandler,以便在onPostExecute 中我可以调用该方法并让它显示从 Http 请求中获得的结果给定文本视图。这样我就不必在我的onPostExecute 中使用巨大的switch(case)

不过,我需要一些执行方面的帮助。在这种情况下,我有两个单独的类来实现 Command。

public interface Command{
    public void execute(JSONObject json);
}

public class DisplayResult implements Command{

    public TextView textview;

    public DisplayResult(TextView textview){
        this.textview = textview;
    }
    @Override
    public void execute(JSONObject output){
        textview.setText(output.getString("mystring")
    }
}

public class ConfirmPost implements Command{

    public ConfirmPost(){
    }
    @Override
    public void execute(JSONObject output){
        Log.d("Success! ","POST successfull");
    }
}

还有两个对HTTPRequestHandler 的调用,一个传递DisplayResult,一个传递ConfirmPost 类。 公共静态无效

TextView mytextview = (TextView) findViewById(R.id.mytextview);
new HttpRequestHandler(new DisplayResult(mytextview)).execute("myurl","GET");

new HttpRequestHandler(new ConfirmPost()).execute("myurl","POST");

现在它在我的HttpRequestHandler 中,我遇到了问题。

public class HttpRequestHandler extends AsyncTask<String, String, String>{
Command presenter;

    Public HttpRequestHandler(Object presenter){    
        this.presenter = (Command) presenter;
    }

    Public String doInBackground(String... uri){
       ...
    }

    Public onPostExecute(String result){
        presentation = new JSONObject(result);
        presenter.execute();
    }

在我看来,仅凭感觉使用 Object 作为参数类型并不是最佳实践。我从来没有见过有人像这样使用它,并且必须将给定对象转换为Command 的想法让我误以为是。有什么方法可以让我给它一个Command对象的类信任

这是我第一次使用命令模式,我还没有完全理解它。温柔点。

更新

看起来我想在这里使用泛型。不过,我仍然不知道如何以最佳实践方式执行该操作。我现在拥有的是:

Command presenter; 

public <Command> HttpRequestHandler(Command presenter){
    this.presenter = (com.example.myproject.Command) presenter;
    //ugly!
}

这很好用,我似乎可以将Command 的任何旧实现传递给它,我似乎无法在网上找到任何具有相同语法或功能的参考。我听说过一些关于工厂模式的提及,但我感觉命令模式并不依赖于其他模式来完全发挥这种功能。任何方向将不胜感激。

【问题讨论】:

  • 最后的“温柔”部分显示了 SO 的初学者是多么害怕提出可能有意义的问题,那是因为一些获得了良好声誉但没有体面的头脑发热的人。这是一个信号伙计们。 “温柔”
  • 我刚刚问了这么多关于编程的看似无害的问题,却因为真正愚蠢的事情而受到严厉谴责。

标签: java android command-pattern


【解决方案1】:

这确实是泛型的问题。 Using Bounded Type Parameters 并通过让T 扩展Command,我可以确保我的参数将是Command 的子类(在这种情况下是实现)

HttpRequestHandler 现在应该看起来像这样

public class HttpRequestHandler extends AsyncTask<String, String, String>{

    Command presenter;

    public <T extends Command> HttpRequestHandler(T presenter){
        this.presenter =  presenter;
    }

    presenter.execute("Success!");
}

不需要可怕的铸造。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 2016-12-14
    • 2016-05-17
    • 2013-09-11
    • 2016-04-04
    • 1970-01-01
    相关资源
    最近更新 更多