• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about Controllers + ActionEvent in MVC

 
Colin Yates
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

After reading most of the GUI related topics on this forum (for the past few hours ) I have a question.

Assuming MVC is broken down as follows:

- Model - business logic aneamic chunk of data (i.e. String[][])
- View - business logic aneamic rendering of Model
- Controller - performs business logic (maybe via delegate) which may change Model.
- View is updated by listening to Model change events, which Model publishes on change

My question is whether it is really a good idea to tie up your controller to Swing events (actionEvent). The general consensus seems to be:

- have a non-responsive MyGUIView with public getters which listens to the model.
- have a controller which adds actionListeners onto the respective view controls (buttons etc.) which update the model.

Would it not be better for the controller to expose methods like

void doSearch(final String criteriaA, final String criteriaB);
void book(final int recNo, final String custNumber)

and so on and have the view attach it's own actionListeners which call the controller?

The problem is of course the view is now calling controller methods, but is that so bad? The alternative is that the Controller is Swing specific.

If the first approach is really the way to go, then I would suggest we have an implementation agnostic Controller, and then some sort of EventHandlerAdapter which adds actionEvents to the View which delegate to the Controller.

To boil it down to a single statement Should the Controller be implementation agnostic? If so (which I believe) it cannot talk about ActionEvents.

Ideas and comments?

Col
 
Colin Yates
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any ideas?

Essentially I am asking whether the controller can be aware of Swing, or whether ther controller should provide business methods and an Adapter ties the swing events to the controller's business methods?

Col
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's fine to have your controller be religious: After all, the business logic it exposes is the real prize, and that can always remain agnostic.


Let's delve into metaphor:

Pretend that you're not working with software at all: you're structuring a company, in America, that offers a service. Your goal is now to offer that service to the rest of the world. Your first client happens to be Russian.

Now, your translator(controller) is a Russian-to-English speaker for an essentially English service. IMO, it's fine if he doesn't speak French: we'll get a French translator when/if we need one.

Make sense?
 
Colin Yates
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, so the "controller" isn't the business logic facade. The controller maps GUI events to the facade? Is that right?

I think I am probably complicating things because I always have a callback interface for the respective GUI which the GUI calls on it's actionListeners. So for example, I would have a GUI.java {
public static interface EventHandler {
void onSearch();
}

public String getLocationCriteria() {}
public String getNameCriteria() {}
public void addEventHandler(EventHandler handler);
}

To "use" the GUI now involves:

final GUI gui = new GUI();
gui.addEventHandler(new Gui.EventHandler() {
public void onSearch() {
myModel.setBookingInfo(facade.findBookings(gui.getNameCriteria()));
}
});

I suppose my EventHandler is the controller?

I was getting confused thinking the "Controller" was GUI agnostic, and could live on the server side. Would it be more correct to state:

- Facade is view agnostic and could live on server side
- Controller, View and Model are all GUI objects

So for example, the model isn't the database, it is the current data set the view is working for. So after executing a search, the model only contains the search results.

Is that correct?

Thanks Max. Great book BTW. I know I am doing a disservice to your book by asking these questions, I just want to make sure Ihave the right terminology. I am a J2EE/web gui so thick swing clients are a bit new
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic