Reputation: 6543
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?
Upvotes: 0
Views: 320
Reputation: 39204
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 .
Upvotes: 1
Reputation: 6821
I'd suggest using an iterface instead. If well defined it should also eliminate the instanceof
checks.
Upvotes: 3