Android - 动态方法调用(java)



我有这个类,它包含四个私有方法,每个方法具有相同的两个参数,以及一个在它们之间进行选择的公共方法。 可能的方法存储在 Map 中,可以添加更多方法。 我想动态调用正确的方法,删除开关。箱。 有没有礼貌的方法可以做到这一点,即没有getMethod/invoke?

public class PlayerClass {
private MediaSource customPlayFunction1(Channel chn, OtherParam other) {

}

private MediaSource customPlayFunction2(Channel chn, OtherParam other) {

}

private MediaSource customPlayFunction3(Channel chn, OtherParam other) {

}

private MediaSource defauPlayFunction(Channel chn, OtherParam other) {

}


public void playChannel(Activity activity, Channel chn) {
//do something 
switch (chn.customFunction) {
case ("CustomPlayFunction1"): {
videoSource = customPlayFunction1(chn,other);
break;
}
case ("CustomPlayFunction2"): {
videoSource = customPlayFunction2(chn,other);
break;
}
case ("CustomPlayFunction3"): {
videoSource = customPlayFunction3(chn,other);
break;
}
default: {
videoSource = defaultPlayFunction(chn,other);
break;
}
}
//do something else

}
}

您可以将Mapjava.util.function中的BiFunction<Channel, OtherParam, MediaSource>结合起来。您需要做的就是根据字符串键填充地图:

private Map<String, BiFunction<Channel, OtherParam, MediaSource>> myMap;
public void setup() {
myMap = new HashMap<>();
myMap.put("CustomPlayFunction1", this::customPlayFunction1);
myMap.put("CustomPlayFunction2", this::customPlayFunction2);
myMap.put("CustomPlayFunction3", this::customPlayFunction3);
}
public void playChannel(Activity activity, Channel chn) {
videoSource = myMap.getOrDefault(chn.customFunction, this::defaultPlayFunction).apply(chn, other);
}

apply将使用适当的参数调用接收到的函数。

getOrDefault将捕获地图中找不到键的任何情况,并自动调用defaultPlayFunction

好吧,如果它们是有限的并且使用有意义,您可以使用Enums(取决于上下文,但目前您尚未提供任何信息):

public enum Channel{
CUSTOM_1 {
@Override
public abstract MediaSource perform(Other other){
// customPlayFunction1
}
}, 
CUSTOM_2{
@Override
public abstract MediaSource perform(Other other){
// customPlayFunction2
}
}, 
CUSTOM_3{
@Override
public abstract MediaSource perform(Other other){
// customPlayFunction3
}
}, 
DEFAULT{
@Override
public abstract MediaSource perform(Other other){
// defaultPlayFunction
}
};
public abstract MediaSource perform(Other other);
}

否则,您必须将责任分离到Strategy

public interface PlayStrategy{
public MediaSource perform(Channel chn, Other other);
}
public class Custom1Strategy implements PlayStrategy{
public MediaSource perform(Channel chn, Other other){
// customPlayFunction1
}
}
// same fore 2, 3  and default

Channel中,您可以存储一个PlayStrategy对象,该对象将传递给将执行以下操作的PlayerClass

chn.getPlay().perform(chn, other);

最后但最不建议的方法是使用这样的Map<String, BiFunction<Channel, Other, MediaSource>>

public class PlayerClass {
private BiFunction<Channel, Other, MediaSource>> map = new HashMap<>();
public PlayerClass(){
map.put("CustomPlayFunction1", this::customPlayFunction1)
map.put("CustomPlayFunction2", this::customPlayFunction2)
map.put("CustomPlayFunction3", this::customPlayFunction3)
}
private MediaSource customPlayFunction1(Channel chn, OtherParam other) {}
private MediaSource customPlayFunction2(Channel chn, OtherParam other) {}
private MediaSource customPlayFunction3(Channel chn, OtherParam other) {}
private MediaSource defauPlayFunction(Channel chn, OtherParam other) {}
public void playChannel(Activity activity, Channel chn) {
map.getOrDefault(chn.customFunction, this::defauPlayFunction).apply(chn, other);
}
}

相关内容

  • 没有找到相关文章