Hi, These days I was always confused about my GUI structure until I saw some Command pattern examples. By employing the Command pattern we can further decouple the command's logic from the action. I think that can make our code more reusable and more readable. The Command actions structure are just like following:
The structure looks fine, it just dispatch all kinds of action logic into individual classes. But now there is another problem comes, since the execute() method has no argument and no return. If the logic want to get some properties from other component and change the relevant properties for other component, the access to each component has to be reconsidered.
Now my structure are like following:
There are many similar methods called createXXX(). As you can see, the actionPerformed() must access other component(now as class member). If I want to split those logic into individual class, I must write class for each of those components, and every class will supply relevant setXXX() and getXXX() method for other component to use. But if that, too many classes would be created, is that necessary? Any good suggestions or comments about my idea or current GUI structure. For I'm just starting using the SWING, maybe lost something important. Thanks for your help. -- Fox
Yes, but doesn't you example put a lot of the "Action", "Command" code into your GUI code. That should be seperated out. You can always create an Action class that implements ActionPerformed, and it has a reference to the Controller and the GUI, so that this class is added to the JButtons Action via the GUI, and it also calls the Controller method to fire when that action occurs. Just to make you think Mark
Hi Mark, Thanks for the reply and the support. I'm going to change my GUI structure as you mean. And there are some problem still need your help:
You can always create an Action class that implements ActionPerformed, and it has a reference to the Controller and the GUI
I'm still not so clear about the concept of Controller, can you give me some brief on the definition of the Model, Controlller and View? The seperate class which represents each component in GUI, which I mentioned in my post, how about its role in the MVC pattern? Is it a Controller? Thanks, -- Fox
Well think of it like this. The view is only there to display the screen. It doesn't know about anything else. The Model is the Data it only knows the data. So the Controller controls the situation. He handles the actions, he gets the data, he passes the data to the screen, and gets information from the screen and passes it to the data. Therefore the JButton on the screen has a list of the ActionListeners that it needs to tell that it has been pressed. That is it's only responsibility, and Sun gave it to the JButton. Now who is listening, you can use your Action or Command pattern class to hear, then call the controllers method that does something, or you can have the controller be the listener registered with the Button. the choice is yours. Mark
So the Controller controls the situation. He handles the actions, he gets the data, he passes the data to the screen, and gets information from the screen and passes it to the data. ... Now who is listening, you can use your Action or Command pattern class to hear, then call the controllers method that does something, or you can have the controller be the listener registered with the Button.
Now I'm more clear about the MVC concept, but just met some problem when implements it. Just as my previous code shown, in my Search button's actionPerformed() method, I need to know the selection for each comboBox, I need to get the search result from Flight service, I need to update the table model and switch to another Tab. Now if I seperate the action to individual class (some Command class), then how can I access the comboBox and JTabbedPanel in my GUI class to get and change their status? I read some MVC topics and just find many Controller method just call relevant Model's update method to update the View. Does that mean I should create Model class for each Component in my GUI, such as above comboBox and JTabberPanel? Mark, how did you solve above problem? Can you give some example codes for your relevant implementation. Thanks so much -- Fox
Here is where you will get different arguments. You need to have the Controller ask the GUI for what it has. So to do this the Controller needs to have a reference to the GUI. That is where the argument begins. Should the controller have a reference to the GUI, which means it needs to know about it's methods. My belief is that this is fine. The GUI can have accessor methods to get the data that it has, and the controller can call these methods. In the case of the Action classes, they will be the ones that would need a reference to the GUI, and call to get the data that it will pass back to the Controller. p.s There are other ways that people have argued as options. I just don't like those. You get to pick which one to use. Mark
I know command pattern can be used in GUI but i am having tough time trying to get a example of it.
in my current GUI i have the user to highlight a text from the Text area, then he needs to select a index from a List and and then he needs to select another index from another list. The problem here is i have 3 different actions but i require the items choosen by the 3 actions for my function to perform. People say Command pattern can solve this but i am not sure how
Any suggestions ?
[Moving this to GUI forum as i just noticed that it was on a wrong forum]
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop