MVC Concept

posted by titoneo on 2009-07-10 14:21:24
Sorry my english, I'm spanish.
Hello, I have a conceptual question. I try to use MVC pattern in a Java (J2SE) application.
The idea is create a process (in the model) and a visual class (in the view) to show updated information to the user about the process is doing. To make the code reusable, the model doesn't know the view and the view doesn't know the model, then the controller come here to connects them. The comunication throw them works like this: the model throws events that the controller handlers and calls the views functions, in other words, the controller capture the model events to show the information on the view.
My problem is when I want to create the view class:
 
public class ComponentA extends JPanel{
	//..
}
public class ComponentB extends JScrollBar{
	//..
}
 
public class View extends JFrame{
	private ComponentA compA;
	private ComponentB compB;
	//...
}
 
The View have 2 components and they can have others subcomponents. The model launches events, the controller captures them and the view is updated (imagine the model extends to an abstract class that has implemented ErrorEvent and SuccessEvent):
 
public class Controller {
 
	private Model model;
	private View view;
 
	public Controller(Model model, View view){
		this.model = model;
		this.view = view;
	}
 
	public init(){
		conectModelToView();
		view.init();
	}
 
	private void conectModelToView(){		
 
		model.addErrorEventListener(new ErrorEventListener(){
 
			public ActionPerformed(ErrorEvent e){
 
				view.setErrorMessage(e.getMessage());
 
			}
 
		});
 
		model.addSuccessEventListener(new SuccessEventListener(){
 
			public ActionPerformed(SuccessEvent e){
 
				view.setSuccessMessage(e.getMessage());
 
			}
 
		});
 
		//..
 
	}
}
 
 
 
public class View extends JFrame{
	private ComponentA compA;
	private ComponentB compB;
 
	public View(){
		//...
	}
 
	public setErrorMessage(String message){
 
		compA.update(message);
 
	}
 
	public setSuccessMessage(String message){
 
		compB.update(message);
	}
 
 
	//...
 
}
 
This forces me to create an interface for each subcomponent. Imagine that View has about 10 components and each component has 10 other components, this would force me to create a superinterface in Class View to update these components depending on the event received. There is another solution that resolves this problem is as follows:
 
public class View extends JFrame{
	private ComponentA compA;
	private ComponentB compB;
 
	public View(){
		//...
	}
 
	public ComponentA getComponentA(){return compA;}
	public ComponentB getComponentB(){return compB;}
}
 
public class Controller {
 
	private Model model;
	private View view;
 
	public Controller(Model model, View view){
		this.model = model;
		this.view = view;
	}
 
	public init(){
		conectModelToView();
		view.init();
	}
 
	private void conectModelToView(){		
 
		model.addErrorEventListener(new ErrorEventListener(){
 
			public ActionPerformed(ErrorEvent e){
 
				view.getComponentA().(e.getMessage());
 
			}
 
		});
      }
}
 
This need not create any interface in Class View, save code in the subclasses as well, the bad news is that it breaks a little encapsulation of the application ... What is the most correct or best solution? Please give me your opinion.
Thank you very much.
dail avatar
dail
12
In traditional MVC, the controller has the model and passes it to the view.
If that means your model is complex, so be it -- although xwork and other APIs provide easy access to a flexible model.
Basically, You shouldn't be creating MVC at all - you should use someone else's MVC implementation. Plus, there are lots of swing docs about this.
You may use http://www.opensymphony.com/xwork/
49 answers - 0 questions
  Positive      - 1  Negative

titoneo avatar
titoneo
0
The complex is the view
3 answers - 1 questions
  Positive        Negative

titoneo avatar
titoneo
0
Please, any idea?
3 answers - 1 questions
  Positive        Negative

rasbonw avatar
rasbonw
0
I think you must use the second one, why? because in both alternatives the Controller have knowledge about how to use View and Model, and the first option, as you've commented, is more tedious. So, after a event has been fired the Controller must use the View methods to do whatever, because the Controller is the engine/brain/...
bye, saludos ;)
1 answers - 0 questions
  Positive        Negative

titoneo avatar
titoneo
0
Posted by rasbonw:
I think you must use the second one, why? because in both alternatives the Controller have knowledge about how to use View and Model, and the first option, as you've commented, is more tedious. So, after a event has been fired the Controller must use the View methods to do whatever, because the Controller is the engine/brain/...
bye, saludos ;)

Thanks rasbonw, I think too.
Normally I think of patterns to be ready for when make a great application, and I think the MVC is a good pattern for this
3 answers - 1 questions
  Positive        Negative



Answer the question



Top Users
  • dail (12)
  • livin52 (3)
  • camu (3)
  • softweb (2)
  • Nadine (1)
  • Josware (1)
  • lfelipecr (1)
  • gregoriohc (1)
  • Mitu (1)
  • ryan714 (1)