Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中链异步调用最优雅的解决方案?_Java_Design Patterns_Asynchronous - Fatal编程技术网

java中链异步调用最优雅的解决方案?

java中链异步调用最优雅的解决方案?,java,design-patterns,asynchronous,Java,Design Patterns,Asynchronous,我目前正在开发android应用程序的表示层 我使用的api如下所示: public interface errorInterface{ public void onError(String reason); } public interface if1 extends errorInterface{ public void dataReceived1(Data data); } public interface if2 extends errorInterface{

我目前正在开发android应用程序的表示层

我使用的api如下所示:

public interface errorInterface{
    public void onError(String reason);
}

public interface if1 extends errorInterface{
    public void dataReceived1(Data data);
}
public interface if2 extends errorInterface{
    public void dataReceived2(Data data);
}   

public void fetchData1(if1 receiver) {}
public void fetchData2(if2 receiver) {}
List<DataReceiver> dataReceivers = new ArrayList<DataReceiver>();
dataReceivers.add(new IF1Adapter(someIf1Implementation));
dataReceivers.add(new IF2Adapter(someIf2Implementation));
fetchData(dataReceivers);

public void fetchData(Collection<DataReceiver> receivers) {

  try {
    Data d = getSomeData();
    for (DataReceiver dr : receivers) {
      dr.dataReceived(d);
    }
  }
  catch (Exception e) {
    for (DataReceiver dr : receivers) {
      dr.onError(e.getMessage());
    } 
  }
}
就是,;要获取数据,您需要提供一个接收器,该接收器将在将来某个时候接收操作的结果

当您一次只需要调用一个方法时,这非常有效,但现在我已经达到了一个点,即我需要一次调用10+个这样的方法,并且它们需要一次执行一个

如何以灵活优雅的方式解决此问题


谢谢

让我确定我明白了。。您有一系列接口
if1
if2
<代码>ifn并且您希望他们都能够处理接收到的数据

首先,最好是
if1
if2
等与您的两个基本方法使用相同的接口:
public void dataReceived(Data d)
public void onError(字符串原因)
。这样,您只需将接收者的
列表
集合
传递给
获取数据
,它就可以在集合上迭代,并对每个集合调用
dataReceived(d)

如果,无论出于何种原因,这是不可行的,我会尝试使用适配器将它们引导到类似的
fetchData
接口中。例如:

public interface DataReceiver extends ErrorInterface {
  public void dataReceived(Data d); 
  //or just scrap the ErrorInterface all together and make these into 1 interface
}

public class AbstractIFAdapter<T extends ErrorInterface> implements DataReceiver {
  private T target;
  public AbstractIFAdapter(T target) { this.target = target);
  public void onError(String reason) { target.onError(reason); }
  protected T getTarget() { return target; }
}

public class IF1Adapter extends AbstractIFAdapter<IF1> {
  public IF1Adapter(IF1 target) { super(target); }
  public dataReceived(Data d) { getTarget().dataReceived1(d); }
}

public class IF2Adapter extends AbstractIFAdapter<IF2> {
  public IF2Adapter(IF2 target) { super(target); }
  public dataReceived(Data d) { getTarget().dataReceived2(d); }
}
公共接口DataReceiver扩展了ErrorInterface{
接收到公共无效数据(数据d);
//或者干脆把ErrorInterface全部废弃,并将它们组成一个接口
}
公共类AbstractIFAdapter实现DataReceiver{
私人T靶;
公共抽象适配器(T target){this.target=target);
public void onError(字符串原因){target.onError(原因);}
受保护的T getTarget(){return target;}
}
公共类IF1Adapter扩展了AbstractIFAdapter{
公共IF1适配器(IF1目标){super(目标);}
公共dataReceived(数据d){getTarget().dataReceived1(d);}
}
公共类IF2Adapter扩展了AbstractIFAdapter{
公共IF2Adapter(IF2目标){super(目标);}
公共dataReceived(数据d){getTarget().dataReceived2(d);}
}
现在有了这些,我们可以做如下事情:

public interface errorInterface{
    public void onError(String reason);
}

public interface if1 extends errorInterface{
    public void dataReceived1(Data data);
}
public interface if2 extends errorInterface{
    public void dataReceived2(Data data);
}   

public void fetchData1(if1 receiver) {}
public void fetchData2(if2 receiver) {}
List<DataReceiver> dataReceivers = new ArrayList<DataReceiver>();
dataReceivers.add(new IF1Adapter(someIf1Implementation));
dataReceivers.add(new IF2Adapter(someIf2Implementation));
fetchData(dataReceivers);

public void fetchData(Collection<DataReceiver> receivers) {

  try {
    Data d = getSomeData();
    for (DataReceiver dr : receivers) {
      dr.dataReceived(d);
    }
  }
  catch (Exception e) {
    for (DataReceiver dr : receivers) {
      dr.onError(e.getMessage());
    } 
  }
}
List dataReceivers=new ArrayList();
添加(新的IF1适配器(某些IF1实现));
添加(新的IF2Adapter(someif2实现));
获取数据(数据接收器);
公共数据(收集接收器){
试一试{
数据d=getSomeData();
用于(数据接收器dr:接收器){
博士(d);
}
}
捕获(例外e){
用于(数据接收器dr:接收器){
onError博士(e.getMessage());
} 
}
}

根据您的具体需求,还有其他一些模式可能适用,例如访问者或责任链类型模式,其中您将接收者链接在一个链表类型结构中,每个接收者在递归结构中调用下一个接收者-这将很好,因为
fetchData
不需要知道它是g设置集合时,它只会获取对链中顶部适配器的引用。因此,
AbstractIFAdapter
将引用另一个
AbstractIFAdapter
,我们称它为
next
,如果该引用不为null,它将调用
next.dataReceived)
在其自己的
dataReceived
方法中。类似于
ServletFilter
s,其中每个过滤器获得
ServletRequest
,然后调用
chain.doFilter(请求,响应)
,这是非常有用的答案。我最终得到了一个根据我的需要修改的第一个建议版本!