wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Mark's  MVC design - all controller methods private? 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 "Mark Watch "Mark New topic
Author

Mark's MVC design - all controller methods private?

Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Hi everybody,

Does anyone use MVC design where only controller knows both about view and model (model does not know about controller and view, view does not know about controller and model)? If so, please share your opinion on this issue.

Do you think that any controller method needs to be non-private? Since noone knows anything about controller and noone needs to know anything about controller is there any point to make anything besides constructor visible?

Thank you for your thoughts.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Well, I'd say that a Servlet is typically a controller, and the doPost or doGet method is typically public.

I'm not sure if access modifiers necessarily apply to MVC ideas.

For more information on MVC, check out this article on Model View Controller:

What is MVC?

Cheers!

-Cameron McKenzie
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Hi Cameron,

Thank you for your comment. The thing is that in this design view is not directly calling controller methods. Controller registers itself through "hook" methods.

For example:



MainWindow's listenToBookButton() method adds listener to the button and all the event handling code is in controller. View just stays there and looks pretty. My controller spawns new thread for each action non related to GUI and uses SwingUtilities.invokeLater() to refresh GUI after the thread finishes its task. My controller is about 600 lines of pure of code (without comments and blank lines) and all methods are private. Only constructor is public so the controller can be instantiated. I think that is what the design implies, but would like to hear if someones disagree.
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hey Aleksander,

Nice to see you again. It seems you are working pretty hard in order to get SCJD certified, aren't you ?

Well man, let me share with you some thoughts.

When you say : model does not know about controller and view, view does not know about controller and model, you are definitely correct !

In most of samples I have ever seen, the View simply is notified by the Model whenever the underlying data changes. It's accomplished by using the Observer Pattern where the View registers itself to the Model and implements a common interface used by the Model to notify its dependents.

For well known WEB applications which use the "classic MVC", the Model might be established during application load.

However "Swing MVC" seems to focus on the model of the controls, not the domain model. That's why you have a lot of classes such as AbstractTableModel, ... bla bla bla currently in the Swing API.

In Swing MVC, the controller's job is to translate the user inputs fired in the View tier into mapped actions existing in the Model tier. The remaining things such as View updates and so on are fired by the model through the Observer Pattern as I stated above.

This is one of the ways to build your Swing-MVC application. There are many other approaches and someone might even disagree of my opinion. However, such archtecture is present in many Swing samples I have seen out there.

Regarding your question : ... all controller methods private? I think Cameron's reply really a good catch !

I think you should even consider to define some key methods as protected in order to allow further changes in the specs.

I would define as "insane" a guy who decided to define every single method in the controller class as private. Unless, you are really sure about what you are doing, of course.

The truth is : There's no cake recipe to do this. All depend of your good sense.

Finally, I would recommend you to take a look at the following articles :

Presentation Model

Fake MVC in Swing - Is real MVC Possible ?

GUI Architecture

Good lucky man in your study journey. And please keep in touch.

Marry Christmas and a Happy New Year.



"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Hi Edisanrdo,

How is it going? I see you are still around

Yes I've been working pretty hard, started at the begining of october and I almost finished. Just have to write the choices.txt, help for the application user and documentation for the controller, that's why I was asking about controller methods to make sure it is ok to make them all private.

Originally posted by Edisandro Bessa:
When you say : model does not know about controller and view, view does not know about controller and model, you are definitely correct !

In most of samples I have ever seen, the View simply is notified by the Model whenever the underlying data changes. It's accomplished by using the Observer Pattern where the View registers itself to the Model and implements a common interface used by the Model to notify its dependents.


Well, using Observer pattern makes the view knows about the model one way or another. It may not hold the reference to the model but it is still coupled with the model through the observer pattern. As you said this is the most common approach, but after reading Mark's posts on MVC I decided to make controller responsible for *everything*, that is: respond to user actions, map them into commands that are sent to the model, then update the view.

I have just read the topic from the second link you provided. Thank you for these. Guys that took part in that topic agree that "regular" MVC supports coupling between the three MVC parts. This is what Ken Blair wrote at the end of its message.

Originally posted by Ken Blair:
As for our current design, we have completely seperated our data from it's presentation as you would in MVC, but it's not using MVC. It's late and I'm not thinking so well, but I believe our design does fit a pattern I just can't remember the name of it, it's similar to MVC.


I'm not sure if this is the same as MVC design that Mark Spritzler was trying to convince people to use, but that is what Mark's design do. Completely separates view and the model, moreover view and model does not know about controller too. They are unaware of the outside world. And that is what supports low coupling. The less you know the better, just do your job and don't care if anyone is interested in it.

In this design any changes are made to controller only. View's purpose is just to look pretty. If I want to use different controller I can do so without touching the view and model code. If I want to change the model, I only have to change the controller, view code stays untouched and the same about model code. If I want to use different view then give it to me, I will just change the controller.

Controller controls everything as the name implies I read Mark posts on this topic and I have to agree that this is the best approach. I'm not sure if this is ok to make all methods private, for me it's not a problem, but don't know about assesor

Thank you for your inputs Edisandro, they are always welcome and highly appreciated. Did you start with your SCJD? If so, I'll be glad to help if I can. I'm getting my B. Sc. this year and planning to write JEE application with JSF and EJB 3.0, want to finish SCJD, so I can start with it. Actually, I could have already finished, but there's always something to improve, so if you didn't start yet, please take my advice and mark your own border - what is enough to do, otherwise you will never end with adding new features



By the way, Mark where are you? Are you still reading this forum?
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Well Aleksander ...
It seems you are becoming a good Java guru !
Do you remember when we were preparing for SCJP and got questions like :

Given :
String a = new String("A");
String b = new String("A");
(a == b) is true or false ?

Good days, don't you think ? (lol)

I'm still around but just watching through the forums. Last Nov 6th I ate the SCWCD exam by scoring 92% and now I'm part of the wall of fame.

After the SCWCD exam, I decided to wait until next January in order to start my preparation for the SCJD exam. To be honest, I'm really in doubt whether I should start next year by preparing for SCJD or even another quite different thing : The PMI - CAPM exam (Aimed for people who want to start a manager carreer).

Most likely I start with the SCJD once I'm planning to move from my actual job (Production and Real Time applications developed in Centura and Sybase) to Java.

But I still think I would also plan to improve my management skill set further.

For sure I will ask your help when I start my preparation for SCJD, however as a good student, I need first to learn the basic things and then come with a more specific question.

Good luck man.
[ December 09, 2006: Message edited by: Edisandro Bessa ]
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Ok, let's no hijack my own thread I'm sending you a PM.

Controller methods issue still actual, eveybody welcome to share their thoughts.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mark's MVC design - all controller methods private?
 
Similar Threads
Still MVC...Please help
Is this MVC ?
confused with MVC
Data Models
Still having some trouble with Observer/Observable