设置不同包中两个不同类之间的交互

时间:2014-08-07 06:28:47

标签: java listview interface javafx listener

我有这样的事情:

Image

我需要知道最佳做法是什么"连接"的交互性。

在左侧,我们有一个RoleSlot,它由一个已分配的Employee和1-5个ProjectRole组成。如果我点击其中一个项目角色,那么我们就可以在右侧查看其ProjectResp(onsibilities)。

问题是,我该怎么做?

此ProjectManager类和RoleSlot类位于不同的包中。

我应该:

从ProjectManager类中为每个小ListView设置一个监听器?这需要以某种方式使RoleSlot的私有ListView可访问。

或者我应该让ProjectManager实现RoleSlotInterface并定义一个类似handleSelectedRole()的方法?

还是有另一种更好的选择吗?

如果可能的话,我想知道利弊。更清洁的代码是更好的代码。

1 个答案:

答案 0 :(得分:0)

我会在selectedRoleProperty()类上公开只读RoleSlot,在roleProperty()类上公开ProjectResp。我会在ProjectManager类中设置它们之间的绑定。

class RoleSlot {
    public ReadOnlyObjectProperty<ProjectRole> selectedRoleProperty();
}

class ProjectResp {
    public ObjectProperty<ProjectRole> roleProperty();
}

class ProjectManager {
    private final RoleSlot roleSlot;
    private final ProjectResp projectResp;

    public ProjectManager() {
        roleSlot = ...;
        projectResp = ...;
        projectResp.roleProperty().bind(roleSlot.selectedRoleProperty());
    }
}
相关问题