在java中传递一个抽象类而不是一个对象?

发布于 2024-11-06 20:32:02 字数 3518 浏览 0 评论 0原文

我在使用 Java 中的 MVC 模型时遇到了一些问题。

我将一个对象 win 传递给 addModal 方法,以便我可以在上下文菜单上采取任何类型的窗口或基本上任何类型的操作,并在我的 handelAction 方法中处理它。然而,我知道传递对象并不是最好的选择,因为我们使用了所有的 java,所以我的问题是

我可以创建某种可以代替对象的抽象包装类吗? ?

请注意,我的所有视图都扩展了 AbstractView,因此我不能简单地扩展这些视图,第二件事要注意的是我不能简单地传入 AbstractView,因为它并不总是会是认为我想要在某个操作发生后显示。

无需过多讨论,这是我现在拥有的一些测试代码:

public void addModal(Action action, Object win, ACTION_CONTEXT_MENU menuSelection){

    switch(menuSelection){
    case SINGLE: 
        singleActions.put(action, win);
        print("singleActions added");
        break;
    case MULTI:
        multiActions.put(action, win);
        print("multiActions added");
        break;
    default: 
        singleActions.put(action, win);
        multiActions.put(action, win);
        print("defaultActions added");
        break;
    }
}

public void handleAction(Action action, Object sender, Object target) {
    if(view.getSelectedRows().size() > 1){
        if(multiActions.get(action) instanceof ModalWindowView){
            print("multi we have an instance of modalwindowview");
            ((ModalWindowView) multiActions.get(action)).showWindow();
        }else{
            view.getTable().setEditable(true);
        }
    }else{
        if(singleActions.get(action) instanceof ModalWindowView){
            print("single we have an instance of modalwindowview");
            ((ModalWindowView) singleActions.get(action)).showWindow();
        }else{
            view.getTable().setEditable(true);
        }
    }


}

public class TestEditTableCrap extends VerticalLayout{

    public TestEditTableCrap(Container container){
        EditTableModel model = new EditTableModel();
        EditTableController controller = new EditTableController();
        EditTableView editTableView = (EditTableView)controller.requestView(model);//this returns an EditTableView
        editTableView.registerEventListener(controller);
        //editTableView.addActionHandler(controller);
        //editTableView.addShortcutListener(controller.getExtendedShortcutListener("enter", KeyCode.ENTER));

        editTableView.setContainerDataSource(container);

        RegistryIdEditTable registryIdTable;

        ContactView view = null;
        addComponent(editTableView.getTable());

        ModalWindowModel winModelNew = new ModalWindowModel(MODAL_SETTINGS.NEW_MODAL_WINDOW);
        ModalWindowModel winModelEdit = new ModalWindowModel(MODAL_SETTINGS.EDIT_MODAL_WINDOW);
        ModalWindowModel winModelDelete = new ModalWindowModel(MODAL_SETTINGS.DELETE_MODAL_WINDOW);

        ModalWindowController winController = new ModalWindowController();
        ModalWindowController winController2 = new ModalWindowController();
        ModalWindowController winController3 = new ModalWindowController();

        ModalWindowView winViewNew = (ModalWindowView)winController.requestView(winModelNew);
        ModalWindowView winViewEdit = (ModalWindowView)winController2.requestView(winModelEdit);
        ModalWindowView winViewDelete = (ModalWindowView)winController3.requestView(winModelDelete);

        controller.addModal(new Action("New"), winViewNew, ACTION_CONTEXT_MENU.ALL);
        controller.addModal(new Action("Modify all selections"), winViewEdit, ACTION_CONTEXT_MENU.MULTI);
        controller.addModal(new Action("Delete"), winViewDelete, ACTION_CONTEXT_MENU.ALL);

    }
}

后续问题:是否有执行这些操作的模式什么样的事情?将已经扩展其他抽象类的类包装在新的抽象类中?

Im having some trouble with my MVC model in java.

Im passing in an Object win to the addModal method so that I can take anykind of window or basicly any kind of action on my context menu and handle it in my handelAction method. However, I know that its not the best to pass along Objects since then we work with all of java so my question is this

could I create some kind of abstract wrapper class that could be taken in instead of the Object?

Please note that All my views extends AbstractView and hence i cant simply extend these views and a second thing to notice is that i cant simply pass in the AbstractView since its not allways going to be view that i whant to show once an action happens.

Without going in to too much details this is the some test code I have for now:

public void addModal(Action action, Object win, ACTION_CONTEXT_MENU menuSelection){

    switch(menuSelection){
    case SINGLE: 
        singleActions.put(action, win);
        print("singleActions added");
        break;
    case MULTI:
        multiActions.put(action, win);
        print("multiActions added");
        break;
    default: 
        singleActions.put(action, win);
        multiActions.put(action, win);
        print("defaultActions added");
        break;
    }
}

public void handleAction(Action action, Object sender, Object target) {
    if(view.getSelectedRows().size() > 1){
        if(multiActions.get(action) instanceof ModalWindowView){
            print("multi we have an instance of modalwindowview");
            ((ModalWindowView) multiActions.get(action)).showWindow();
        }else{
            view.getTable().setEditable(true);
        }
    }else{
        if(singleActions.get(action) instanceof ModalWindowView){
            print("single we have an instance of modalwindowview");
            ((ModalWindowView) singleActions.get(action)).showWindow();
        }else{
            view.getTable().setEditable(true);
        }
    }


}

public class TestEditTableCrap extends VerticalLayout{

    public TestEditTableCrap(Container container){
        EditTableModel model = new EditTableModel();
        EditTableController controller = new EditTableController();
        EditTableView editTableView = (EditTableView)controller.requestView(model);//this returns an EditTableView
        editTableView.registerEventListener(controller);
        //editTableView.addActionHandler(controller);
        //editTableView.addShortcutListener(controller.getExtendedShortcutListener("enter", KeyCode.ENTER));

        editTableView.setContainerDataSource(container);

        RegistryIdEditTable registryIdTable;

        ContactView view = null;
        addComponent(editTableView.getTable());

        ModalWindowModel winModelNew = new ModalWindowModel(MODAL_SETTINGS.NEW_MODAL_WINDOW);
        ModalWindowModel winModelEdit = new ModalWindowModel(MODAL_SETTINGS.EDIT_MODAL_WINDOW);
        ModalWindowModel winModelDelete = new ModalWindowModel(MODAL_SETTINGS.DELETE_MODAL_WINDOW);

        ModalWindowController winController = new ModalWindowController();
        ModalWindowController winController2 = new ModalWindowController();
        ModalWindowController winController3 = new ModalWindowController();

        ModalWindowView winViewNew = (ModalWindowView)winController.requestView(winModelNew);
        ModalWindowView winViewEdit = (ModalWindowView)winController2.requestView(winModelEdit);
        ModalWindowView winViewDelete = (ModalWindowView)winController3.requestView(winModelDelete);

        controller.addModal(new Action("New"), winViewNew, ACTION_CONTEXT_MENU.ALL);
        controller.addModal(new Action("Modify all selections"), winViewEdit, ACTION_CONTEXT_MENU.MULTI);
        controller.addModal(new Action("Delete"), winViewDelete, ACTION_CONTEXT_MENU.ALL);

    }
}

Follow up questions: Is there a pattern for doing these kind of things? Wrapping in classes that already extends other abstract classes in a new Abstract class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忘年祭陌 2024-11-13 20:32:02

我建议使用 iterface 代替。如果定义良好,它还应该消除 instanceof 检查。

I'd suggest using an iterface instead. If well defined it should also eliminate the instanceof checks.

桜花祭 2024-11-13 20:32:02

这就是interface 的用途

That's what interface is for

暮凉 2024-11-13 20:32:02

如果您想避免大量使用instanceof,可以实现访问者模式

您的handleAction可以放入访问者类中,使用双重调度来确定应该访问哪种窗口类型,允许您在访问者类中使用方法重载而不是instanceof。

If you want to avoid an extensive use of instanceof, you can implement visitor pattern.

Your handleAction could be put into visitor class, using double dispatching to establish which window type should be visited, allow you to use method overloading in visitor class instead of instanceof .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文