File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes refactoring to fit MVC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "refactoring to fit MVC" Watch "refactoring to fit MVC" New topic
Author

refactoring to fit MVC

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

for an exercize, i am going to refactor a project i did at community college to fit the MVC framework we used later at university. i didnt notice when i first learned it, but that professors framework was fundamentally different from the way i did things before. now that i notice, i want to know why. ill post some code from both.
from the class i am going to refactor:


from the MVC framework:


my first question is why he made JFrame an instance variable instead of extending JFrame? it kind of reminds me of what i read about Spring using Plain Old Java Objects that dont extend or implement anything.

my second is about why he made MainPanel class instead of just adding straght to the ContextPane like i used to do. my guess is its more object oriented and easier to read.

any insight or comments appreciated


SCJP
Visit my download page
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 988
    
    1
Of course I can't look into your professor's brain and know exactly why he did anything in a particular way. However I can think of one or two possibilities:

...why he made JFrame an instance variable instead of extending JFrame?


IMO, the "View" is an container concept that encompasses all the interface objects. It's true that one of interface objects, such as the JFrame, could be "promoted" to be the boss, but it's better (purer?) to have all the concrete object be children of the View class/object. It's a cleaner design at the cost of one additional object. Also, if you ever have to change out the object that is currently a JFrame for an object of a different class, you'll be changing out one of the children of the View instead of reworking the View itself.

...why he made MainPanel class instead of just adding straght to the ContextPane


It depends on what behavior is included in the MainPanel class and what's in the View class. It would make sense to me if the MainPanel class acts as a wrapper around all the lower level interface objects that are on the panel, while the View handles higher level functionality that doesn't depend on details. For instance, I could see the Controller sending the View a message (a.k.a. call a View method) in response to "update the interface elements". The View might then tell the MainPanel to display the current options. The MainPanel would then tell CheckBoxA to be unchecked and RadioButtonB to become checked. In this scenario, the View knows that the MainPanel is responsible for displaying the current set of options, but the MainPanel knows that details of what types of interface elements (checkboxes, text areas, etc.) that are actually used.

But those are just guesses.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

thanks for the opinion. i think i like the way he did it, and he made that nice framework for me so i will follow his example when using MVC pattern for desktop apps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: refactoring to fit MVC
 
Similar Threads
divide by 0 exception
public vs. private modifier
Populating textbox with the selected item from the pop-up window
what layouts to use
Mouse shift event coding - to trigger a predetermined sequence of images to a GUI