• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MVC Interfaces

 
Nick Kerr
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am contemplating having every one of my MVC Components implement an interface. This will enable any single Model, View or Controller component to be replaced easily with a new implementation of the interface.

I am wondering whether this is over-kill for the SCJD exam or expected?

Please could SCJD graduates let me know whether they went to the extent of creating interfaces for every MVC component?

Thanks
Nick
 
Dalton Filho
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can imagine someone doing this with the control layer, considering the use of the State pattern, where each controller would extend an abstract State class. But how would you do it with the view layer? You have windows, dialogs, etc. What kind of interface would you create? You need to create a very specific interface for each window... unless you create some kind of "getAttribute(String key)" and "setAttribute(String key, Object value)" methods on this interface.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is overkill.

I'm the biggest proponent of interfaces out there, but to have EVERY class implement an interface?

I guarantee that if every single class implements an interface, and it's obvious that you've tried to bend every class into an interface, it will be held against you, and not in your favour.

The point is to demonstrate an understanding of when, and when not to, use an interface.

To a man with a hammer, everything looks like a nail.
 
Nick Kerr
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, I truly appreciate your responses.

I noticed that my MVC 'framework' was getting a little out of hand and wanted an excuse to throw it away in exchange for a simpler more tightly coupled implementation The tricky thing with this project is trying to exercise self-constraint when deciding just how much flexibility to implement in the design.

I didn't really find myself needing to 'bend' any implementations with my 'hammer' to suit a generic interface since I had a good hierachy of Interfaces. For example, I had a generic View interface with methods like "setController(Controller controller)" and then had sub-interfaces such as SearchView that had methods like "getSearchCriteria()".

I've decided to take your advice though and remove most of my interfaces since its probably acceptable to have each Controller tightly coupled to the View. Especially considering the View and Controller will be discarded when the application is replaced with a web implementation.


Nick
[ August 29, 2006: Message edited by: Nick Kerr ]
 
Adam Nace
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kameron McKenzie:
This is overkill.

I'm the biggest proponent of interfaces out there, but to have EVERY class implement an interface?

I guarantee that if every single class implements an interface, and it's obvious that you've tried to bend every class into an interface, it will be held against you, and not in your favour.

The point is to demonstrate an understanding of when, and when not to, use an interface.

To a man with a hammer, everything looks like a nail.


I have to dissagree here. If you're going to use an MVC architecture, then you should have interfaces for the view and controller classes. The whole point of an MVC is to decouple the presentation, logic, and data layers of the application.

Decoupling does JUST mean different classes. It means "not dependent on". The use of Interfaces allows you to decouple the presentation, logic, and data such that they don't know extactly WHAT kind of view or control they are interacting with.

As a note, using interfaces allows you to more easily design test classes! You can swap out the view for a test object implementing your interface, and use that object to ensure that you get the results you expected from certain operations.

- Adam
 
Nick Kerr
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response Adam,

I think I'll go with interfaces for each of my components but will limit the extent of my 'framework'. I currently have interfaces for all sorts of generic components (e.g. PanelView, FrameView, DialogView) which may be overkill for this project.

Maybe I should just limit myself to having specific interfaces for each TYPE of View/Model/Controller, e.g. SearchView, SearchModel, SearchController.

I'm extremely interested to hear back from any candidates who have passed the SCJD exam to find out how much of a framework they created? Its really hard to judge just how far to go with this

Im interested in hearing about whether other successful SCJD submissions used the 'hooks' approach mentioned elsewhere on JavaRanch:

JavaRanch Hooks

Or whether a more generic approach was taken. I'm not keen on exposing addActionListener methods in my View to my Controller since that implies a certain type of implementation within the View. Instead, I'm considering whether to have my View's issue "commands" to the controller and have any ActionListener, DocumentListener or other listener code embedded within the View.

Thanks again,

Nick
 
Jason Moors
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nick,

I think your coming to the right conclusion and you're not the only one that has had problems exercising some self control when designing this application.

My philosophy has been to keep everything as simple as possible, therefore I've mainly used interfaces in conjuction with different design patterns where I felt decoupling the design was important.

I've created myself a rule now, that if I have trouble justifying something then I don't do it.

Hope it helps!

regards
Jason
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic