This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Inner classes and Mediator Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Inner classes and Mediator Pattern" Watch "Inner classes and Mediator Pattern" New topic
Author

Inner classes and Mediator Pattern

Amit Kr Kumar
Ranch Hand

Joined: Feb 08, 2002
Posts: 100
Hi Ranchers
I am using the Mediator Design Pattern in Book Seat Module's GUI in which there
is a Controller/Mediator class which knows abt all the visual components.
Visual component are independent and dont need to know abt each other as
they can communicate via Controller / Mediator class.
My doubt is that
1) should i have BookButton, SearchButton etc as seperate class (.java) or inner classes
in FBNBookSeat.java is ok
2) At the same time should the controller class be a seperate file or can be a inner class
I know its a very silly question to ask but i need to give my GUI a neat design. If
i make all of them in a single file and make them as inner classes, then the no of lines is very large and at the same time it will not look as neet design.
If a make seperate files for each visual component and for Controller class then it may create confusions as to which module they belong to e.g BookButton.java or ClearButton.java or FBNComponentController.
Pls Guide
Amit
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
All you need one class(A) that extends JFrame and other class which acts as a mediator to the class A. Put all the listeners as inner classes in the controller A.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
In addition to JFrame and controller, I also have a Mediator. Controller uses mediator to get access to GUI values, such as seats to book, selected flight, etc.
Eugene.
Amit Kr Kumar
Ranch Hand

Joined: Feb 08, 2002
Posts: 100
Hi
How can i fit MVC here ???
Amit
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
Abbreviation for MVC is Model View Controller. The goal of this pattern is to achieve multiple view of your data with little code change. You can think of showing the same data in a graph, pie chart, etc by having different view and corresponding controller for it.
In this assignment, all you have to do is to seperate the view from the data/model and the controller. The sub class of JFrame acts as a View in MVC. The controller holds the logic to take care user action and also holds the data along with TableModel(Model and Contoller part of MVC).
[ June 23, 2002: Message edited by: Sai Prasad ]
Amit Kr Kumar
Ranch Hand

Joined: Feb 08, 2002
Posts: 100
Hi Sai
Thanks for the quick reply.
However i am still confused between Mediator Pattern and MVC. Can you Please explain it briefly in context with your solution ?
As far as i know Mediator Pattern has a mediator class through which components can communicate with each other and it is required when there exist large no of classes and communication between them is very complex. In my app i have used a mix of Mediator Pattern and Command Pattern. For example
<pre>
class JButton ClearButton extends JButton
{

public ClearButton(Mediator m)
{
}

void execute()
{
m.clear();
}
}
class Mediator
{
void clear()
{
txtfieldA.setText("");
txtfieldB.setText("");
}
}
</pre>
As soon as a button is clicked the execute method is invoked of the button which further call the one of the method of Mediator class where communication and processing is done.
How to fit MVC here

Amit
[ June 24, 2002: Message edited by: Amit ]
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Hello,
In my case iam using MVC architecture as follows. I have a FBNClient.java which extends JFrame, This class has all the main GUI part. I have class FBNEventContoller.java which handles all the events here this class implements only ActionListener and all listeners are handled here, Except closing windows(iam using anonums class) Model is nothing but the DataFacade.java.
Here for Flight search i have seperate class like SearchFlight.java. Like wise for bookingFlight i have BookFlight.java ..(Which intern call DataFacade).
The Architecture:

+--------------+
|FBNClient |+-----------------+
|extends JFrame| |
+--------------+ |
+ |
| |
| |
| |
| |
+ |
+-------------------+ |
| FBNEventController| |
+-------------------+ |
+ |
^ +------------+
|------------>-| DataFacade|
v +------------+
+-----------------+ (gateway to server)
+ +
+-----------+ +--------+
|SearchFlig | |bookFlig|
+-----------+ +--------+
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Hello,
In my case iam using MVC architecture as follows. I have a FBNClient.java which extends JFrame, This class has all the main GUI part. I have class FBNEventContoller.java which handles all the events here this class implements only ActionListener and all listeners are handled here, Except closing windows(iam using anonums class) Model is nothing but the DataFacade.java.
Here for Flight search i have seperate class like SearchFlight.java. Like wise for bookingFlight i have BookFlight.java ..(Which intern call DataFacade).
The Architecture:

+--------------+
|FBNClient |+-----------------+
|extends JFrame| |
+--------------+ |
+ |
| |
| |
| |
| |
+ |
+-------------------+ |
| FBNEventController| |
+-------------------+ |
+ |
^ +------------+
|------------>-| DataFacade|
v +------------+
+-----------------+ (gateway to server)
+ +
+-----------+ +--------+
|SearchFlig | |bookFlig|
+-----------+ +--------+
Amit Kr Kumar
Ranch Hand

Joined: Feb 08, 2002
Posts: 100
Hi Ramesh
Do you mean to say that
- BookFlight.java and SearchFlight.java are your Views
- FBNEventController is controller
- DataInfo is your model

Sai what do you have to say abt this ???
Amit
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
You are making this assignment more difficult than it should be. The Controller in MVC is nothing but the mediator in Mediator pattern. I think it is an overkill to use Command pattern and sub class the JButton class. Search flight and book flight are actions and not entities. Therefore no need to have a seperate classes. They are methods defined in the Facade class.
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Sai,
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Sai,
I agree that searchFlight and BookFlight are actions, What is wrong in have these in seperate classes and once the event is generated the appropriate casses is invoked from the controller. Here maintablity is also very simple and we can reduce the size of the file by seperating it in a different class because the readablity is also becomes simple. Please correct me if iam wrong.
thanks & regards,
-rameshkumar.
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
Having searchFlight() and bookFlight() in a DataFacade is also maintainable. You normally design a class for a noun and not an action. Even though there are some exceptions to this rule, in this case, I don't see any need to have two seperate classes.
Amit Kr Kumar
Ranch Hand

Joined: Feb 08, 2002
Posts: 100
Sai,
Thanks a lot for all your help. Its being a gr8 help.
Can you put some light on your Mediator and MVC pattern in context with your design ???
Thanks and regards
Amit
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4474
    
    6

"Amit",
Please change your publicly displayed name in your profile back to what it was before or something else that follows the format required by the JavaRanch Naming Policy.
Thanks!
Junilu
[ June 24, 2002: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Amit you obviously have two accounts right now. Because you changed it last time.
Please use the account that has your full name, and delete the other one.
Thanks
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Hi Sai,
In my design as u said iam having a method searchFlight() , BookFlight() in my DataFacade class which intern communicate to the serverside class and fetch the data but, Here The purpose of having seperate class for searchFlight, bookTicket is, These class will have all the GUI part of the searchFlight, BookFlight for example
BookFlight will have the following methods it.
1. a method bookFlight() which will popup a JDialog which has a textField to get the no of seats.
2. a method which popup JDialog saying "Please select a row before u book a ticket".
3. a method which will popup a JDialog saying "you seat is confirmed" Once the reservation is OK.
4. a method which will popup a JDialog saying "sorry no seat is avilable"
5. a method which will popup a JDialog saying "Only 4 seats are avilable" Suppose less no of seats are avilable than the seat quried for.
SearchFlight will have the followings in it.
1. a method searchFlight() which will popup JDialog which contains all the Origin, destination
2. SearchFlight class also contains noDataFound()
This method will popup a JDialog saying no data Found.

I not sure wheathere the above desing is right please give ur valid feedback so that i can correct myself.
regards,
-rameshkumar
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Hello Sai,
Iam waiting for ur reply regarding my previous posting.
-rameshkumar
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Ramesh, in your Popup methods, is this seperate methods or one method.
I would suggest it being one method that takes a String that is the message you want the JDialog to have.
You also have a popup for entering the number of seats to book, and one that allows the user to select what to search on. To make the GUI part simpler can you place all that stuff on one screen? Actually, what is on your main screen then?
Mark
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Hi Mark,
The reason for having seperate classes is to give better maintainablity, Later i can comeup with different search criteria which needs some more components to be added in the JDialog, In that case if i have seperate class like what i have then it will be very easy to modify.
Mark wrote:
--------------------------------------------------
I would suggest it being one method that takes a String that is the message you want the JDialog to have.
--------------------------------------------------
I have implemented the above. But still i have seperate class for the reson i told above. Please correct me still if iam wrong.
regards,
-rameshkumar
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Later i can comeup with different search criteria which needs some more components to be added in the JDialog,

No, actually what I meant was the messages that you are displaying in pop-ups. The "Search screen" or the "Book Screen" in your case would be seperate classes. Although I wonder what is on your main screen then? Just the JTable?
Mark
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Hi Mark,
My main GUI contains JTable for displaying all the avilable flights, JMenuBar which will have the folloings
1. SearchFlight
2. BookFlight
3. ShowAllFlight
4. Exit
In the bottom of the JTable I have searchButton, BookFlight button showAllFlight button, Exit button.
Onclick of these buttons and menuItems it will be notified by FBNEventController.java which implements ActionListener. Here inside the actionPerformed(ActionEvent ae) based on the event fired the appropriate (SearchFlight OR BookFlight) will be called, Which is nothing but popup a Dialogbox with nesscessery components. Once the user press the Ok button of the above JDialog it will be again listerned by the FBNEventController, Which will call the DataFacade.java which intern talk to the serverside db classes.
Now please give me ur feedback regarding the above design.
thanks & regards,
-rameshkumar
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Here inside the actionPerformed(ActionEvent ae)

So in this method you have a bunch of if statements? So each time you add a button you add another if statement. As time goes by this can become difficult to read.
You could break these apart into seperate methods, but you would need to include hook methods into your GUI. Not that this is a bad thing, but it is completely a question of taste. Meaning which feels more comfortable to you.
A hook method in you GUI would look like this

Hope that helps.
Mark
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Hi Mark,
mark wrote:
--------------------------------------------------
/**
* This method is a hook to allow the Controller to define the action for the Search JButton.
* @param ActionListener action Action to assign to the Search JButton.
*/
public void searchAction(ActionListener action) {
searchButton.addActionListener(action);
}
--------------------------------------------------

Hook method this is some thing new for me , Here i want to clear some thing, The above piease of code Should be in the MainGUI class where i define all the JButtons and JMenuItems, u mean i have to introduce seperate methods for all my JButton like the one above. Here in the searchAction(ActionListener al) al is nothing but FBNEventController am i right. Here i have a doubt that in the FBNEventController.java which implements ActionListener and override actionPerformed(ActionEvent ae) again i have to use multiple IF to differenciate the events fired. Please explain me in detail how to proceed.
regards,
rameshkumar
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Yes in your GUI you will have a method like what I put above for each Button, and Each Action you want to hook into.
In your Controller you will have one method called assignActionListeners
Here's a sample from my code

This way there is an anonymous inner class that I created. and then whenever someone presses the button the searchFlight method in the controller is called.
Hope that helps and clears things up for you.
Mark
Gosling Gong
Ranch Hand

Joined: Jun 20, 2002
Posts: 51
I am wondering what the benefit it has to use the hook methods to assign action listener?
say that in the future, you will add some component on the GUI frame, what you have to do will be modify both the GUI and the Controller, you didn't seperate them successfully!
why not making the registration in the GUI itself and invoke the Controller(or mediator maybe much precisely). In this way, the controller is composed with a bunch of methods for the GUI to invoke. Thus, if only GUI changes, we only need to change the GUI class, no need to change the controller(off course, if new functionality added, it's neccessary to modify controller).
looking forward to your opinion on this issue!
thanks
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

the controller is composed with a bunch of methods for the GUI to invoke.

Because now the GUI must know about the controller, which is not the MVC model.
The reason for this hook, is becuase now any controller, any class can be added as an action listener. The GUI never needs to know who did it, or get any kind of reference to it, which really decouples the GUI from knowing any outside class.
In all cases if you change the GUI by added a JButton, somewhere else will have to make a change. But if I change my Controller, I never have to change the GUI.
Mark
 
GeeCON Prague 2014
 
subject: Inner classes and Mediator Pattern