我的界面很胖:

public interface ISolver {
    void solve();
    String printMsgs();
    boolean hasSolution();
    int getLeaderId();
    void add(Agent agent);
    void add(int item);
    void add(String item);
    void addAll(Agent[] data);
    void addAll(int[] data);
    void addAll(String[] data);
    public void initiateStartState();
    Agent next();
    boolean hasNext();
    public List<Integer> getLeftSenders();
    public List<Integer> getRightSenders();
    public List<Integer> getCurrentLeaders();
    Agent get(int idx);
}


有两个类继承了此接口:

public class OneDirectSolver extends Solver{
    // some code..
}

public class BiDirectSolver extends Solver{
    // some code..
}


BiDirectSolver使用在ISolver中声明的所有方法,但是OneDirectSolver仅使用一些方法。我要遵循接口隔离原则。因此,我只在ISolver中保留了常用方法。我专门针对BiDirectSolver的方法在另一个扩展了ISolver的界面中移​​动

public interface ISolver {
    void solve();
    String printMsgs();
    boolean hasSolution();
    int getLeaderId();
    void add(Agent agent);
    void add(int item);
    void add(String item);
    void addAll(Agent[] data);
    void addAll(int[] data);
    void addAll(String[] data);
    public void initiateStartState();
    Agent next();
    boolean hasNext();
}

public interface BiDirectSolvable extends ISolver {
    public List<Integer> getLeftSenders();
    public List<Integer> getRightSenders();
    public List<Integer> getCurrentLeaders();
    Agent get(int idx);
}


但是现在我有另一个问题。我试图描述它。因此,我有2个BiDirectSolverOneDirectSolver的客户端:

public class OneDirectPanel extends MyAbstractPanel {

    private OneDirectSolvable solver;

    public void setSolver(OneDirectSolver solver) {
        this.solver = solver;
    }
    //some code..
}

public class BiDirectPanel extends MyAbstractPanel {

    private BiDirectSolvable solver;

    public void setSolver(BiDirectSolvable solver) {
        this.solver = solver;
    }
    // some code
}


现在,我想显示主类的片段,在其中创建OneDirectPanel,BiDirectPanel和ISolver的实例:

public class GUI {
    private ISolver solver;
    private MyAbstractPanel panelWithPicture;
    // a lot of code..
    if (comboBoxForMode.getSelectedItem().equals("one direct")) {
        solver = new OneDirectSolver();
        panelWithPicture = new OneDirectPanel();
        panelWithPicture.setSolver((OneDirectSolver)solver);
    } else {
        solver = new BiDirectSolver();
        panelWithPicture = new BiDirectPanel();
        panelWithPicture.setSolver((BiDirectSolvable)solver);
    }
}


我只看到一种解决方案:

if (comboBoxForMode.getSelectedItem().equals("one direct")) {
    OneDirectSolver solver = new OneDirectSolver();
    panelWithPicture = new OneDirectPanel();
    panelWithPicture.setSolver(solver);
} else {
    BiDirectSolvable solver = new BiDirectSolver();
    panelWithPicture = new BiDirectPanel();
    panelWithPicture.setSolver(solver);
}


但是对我来说这是不可能的,因为创建解算器后必须对它进行一些调整。我必须在所有情况下都这样做。所以,我在主班做。
 因此,在主类中,我必须声明类型为ISolver的引用。

UPD:
最重要的是,如果我对主类中的求解器执行某些操作,则它必须影响OneDirectPanel和BiDirectPanel中的求解器。

最佳答案

由于您暂时需要一个更特定的运行时类型,因此可以在之后分配给this.solver

private ISolver solver;
...
if (comboBoxForMode.getSelectedItem()
                    .equals("one direct")) {
                OneDirectSolver solver = new OneDirectSolver();
                panelWithPicture = new OneDirectPanel();
                panelWithPicture.setSolver(solver);
                this.solver = solver;
            } else {
                BiDirectSolvable solver = new BiDirectSolver();
                panelWithPicture = new BiDirectPanel();
                panelWithPicture.setSolver(solver);
                this.solver = solver;
            }

10-04 21:40