最后我自己解决了这个问题事实证明,消费者是可以改变的并通过接口让我的xxx_dialog和强制使用openDialog功能。
产品、客户的数据bean启动
public class Prod extends MyBean {
private String P_NO = "";
private setP_NO(String p_no){
P_NO= p_no;
}
private String getP_NO(){
return P_NO;
}
}
public class Cust extends MyBean {
private String CUST_NO = "";
....
}
public class MyView{
TextField textFieldP_NO;
Button btnProd;
Button btnCust;
public MyView (){
//...constructor...
}
btnProd.addClickListener(e -> {
Prod_Dialog p_dlg = new Prod_Dialog();
//fix before
//p_dlg.openDialog(p -> textFieldP_NO.setValue(p.getP_NO()));
//fix after
p_dlg.openDialog(p -> textFieldP_NO.setValue(((PROD_Dialog.Prod) p).getP_NO()));
});
btnCust.addClickListener(e -> {
Cust_Dialog c_dlg = new Cust_Dialog();
//fix before
//c_dlg.openDialog(c -> textFieldP_NO.setValue(c.getCUST_NO()));
//fix after
c_dlg.openDialog(c -> textFieldP_NO.setValue(((CUST_Dialog.Cust) c).getCUST_NO()));
});
}
[Interface] fix参数Consumer can Polymorphism
public interface BaseDialog {
//fix before just for prodct. That's bad....
//public abstract void openDialog(Consumer<Prod> selectionAction);
//fix after that can service any MyBean...
public abstract void openDialog(Consumer<? extends MyBean> selectionAction);
}
[Dialog] with Prod_Dialog and cust_Dialog通过clickok方法传递Consumer
//fix before
//public class Prod_Dialog {
//fix after
public class Prod_Dialog implements BaseDialog{
private Button btnOk;
...
//fix before
//public void openDialog(Consumer<Prod> selectionAction) {
//fix after
public void openDialog(Consumer<? extends MyBean> selectionAction) {
btnOk.addClickListener(e -> {
//fix before
// Prod sel_prod = grid.asSingleSelect().getValue();
// String p_no = sel_prod.getP_NO(); //get user selected p_no
// Prod prod = new Prod();
// prod.setP_NO(p_no);
// selectionAction.accept(prod);
//fix after that is magic point for me, by Consumer Transformation
clickOK((Consumer<Prod>) selectionAction);
}
}
public void clickOK(Consumer<Prod> selectionAction) {
Prod sel_prod = grid.asSingleSelect().getValue();
String p_no = sel_prod.getP_NO(); //get user selected p_no
Prod prod = new Prod();
prod.setP_NO(p_no);
selectionAction.accept(prod);
}
}
public class Cust_Dialog implements BaseDialog{
private Button btnOk;
...
public void openDialog(Consumer<? extends MyBean> selectionAction) {
btnOk.addClickListener(e -> {
//fix before
// Cust sel_cust = grid.asSingleSelect().getValue();
// String c_no = sel_cust.getCUST_NO(); //get user selected p_no
// Cust cust = new Cust();
// Cust.setCUST_NO(c_no);
// selectionAction.accept(cust);
//fix after
clickOK((Consumer<Cust>) selectionAction); //that is magic point for me, by Consumer Transformation
}
}
public void clickOK(Consumer<Cust> selectionAction) {
Prod sel_cust = grid.asSingleSelect().getValue();
String c_no = sel_prod.getCUST_NO(); //get user selected p_no
Cust cust = new Cust();
cust.setCUST_NO(c_no);
selectionAction.accept(cust); //that is magic point for me
}
}
您的工作实现类代码方法签名
public void openDialog(Consumer<PROD> selectionAction)
与接口签名
不相同。public abstract void openDialog(Consumer<? extends MyBean> selectionAction);
不要写abstract中的接口签名声明(默认情况下它们是公共的和抽象的)!
方法的参数也将/必须/总是相同的!
实现类代码方法签名应该在接口中几乎完全相同。