简体   繁体   English

java中的回调行为

[英]call-back behavior in java

My question is more towards the design pattern to use for my implementation. 我的问题更多的是用于我的实现的设计模式。 I have the code written as follows - 我的代码编写如下 -

X-Service.java X-Service.java

handler.setListeners(new HttpResponseHandler.conListers() {

    @Override
    public void success() {

    }

}); 

HttpResponseHandler HttpResponseHandler

protected conListers conListener;
public interface conListers {
    public void success();
}

public void conListers(conListers listener) {
    this.conListers = listener;
}

So now my problem is I can use this technique If I had just one type of success function. 所以现在我的问题是我可以使用这种技术如果我只有一种类型的成功函数。 To be more clear I have multiple services where success method have different signature like -- 为了更清楚我有多个服务,其中成功方法有不同的签名像 -

public void success(String x);
public void success(HashMap y, Integer z);

I do not want to put all the methods in the interface, as I will have to implement them in all the services. 我不想把所有方法都放在界面中,因为我必须在所有服务中实现它们。 I need a way in which I can just implement the success method I want. 我需要一种方法来实现我想要的成功方法。

You could define the interface using a generic type declaration: 您可以使用泛型类型声明来定义接口:

public interface conListers<E> {
    public void success(E value);
}

Alternatively, if you need a variable number of arguments of the same type then you can use: 或者,如果您需要可变数量的相同类型的参数,则可以使用:

public interface conListers<E> {
    public void success(E... value);
}

If you need a fixed number of arguments then you can just test the length of the value argument in the definition of success() in the implementing class. 如果需要固定数量的参数,那么您可以在实现类中的success()定义中测试value参数的长度。

However, I can't think of any pattern you can use to allow success() to take a variably fixed number of different typed arguments unless you use Object but that then brings its own issues (like having to type check all the arguments within the implementing class): 但是,我想不出你可以使用任何模式来允许success()采用可变数量固定数量的不同类型的参数,除非你使用Object然后带来它自己的问题(比如必须输入检查所有的参数)实施班):

public interface conListers {
    public void success(Object... value);
}

You can use the command pattern in this scenario, basically the conListener will be your command. 您可以在此方案中使用命令模式,基本上conListener将是您的命令。 You'll have as many conListeners implementations as your services. 您将拥有与服务一样多的conListeners实现。

Example: 例:

public class conListenersA implements conListener{
    protected Service serviceA;
    public void success(){
        serviceA.success(arg1);//the method has  arguments
    }
}

public class conListenersB implements conListener{
    protected Service serviceB;
    public void success(){
        serviceB.success(arg1,arg2);//the method has 2 arguments
    }
}

The advantage is that whenever you need to execute a conListener you call will be "uniform", as simple as conListener.success() 优点是无论何时需要执行conListener您都会调用“uniform”,就像conListener.success()一样简单。

You can try with most general Java class Object as a parameter and use varargs: 您可以尝试使用大多数通用Java类Object作为参数并使用varargs:

public interface conListers {
    public void success(Object... arguments);
}

Inside implementation you'll have to figure out object types and the number of arguments, but you'll have a clean interface for your functions. 在内部实现中,您必须弄清楚对象类型和参数的数量,但是您将为您的函数提供一个干净的界面。

Another approach is to define a class that holds all of your arguments that you are planning to send on a success, and then inside implemented success methods get parameters that you really need. 另一种方法是定义一个类,它包含您计划成功发送的所有参数,然后在实现的成功方法中获取您真正需要的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM