views:

127

answers:

3

K so last year I made a family tree program in java as a group project and we used MVC but I really forget how it works, and the notes aren't online anymore.
To make things worse, since our project wasn't huge the prof said to combine the Model and Controller.
So now that I'm trying to make a sweet project to add to my employability portfolio I have no clue how MVC really works.

What would my relationships would have to look like between the model, view and controller in the following cases, I could probably figure out the rest from that

  1. Model has a state called location and the view needs to know when this changes so it can update it's display
  2. View has a button that the model needs to know when it is clicked

With those solved I can most likely do the rest myself.

Or if anyone knows of any good sites with information I'm not opposed to relearning this either.

+2  A: 

Here is a CodeProject that may help: http://www.codeproject.com/KB/cs/model_view_controller.aspx

dsrekab
it's a good startbut just like my old project I had made, the controller is combined with the others, and I'd like to know how it works when everything is seperate
Jean-Bernard Pellerin
A: 

Here is a great example in ASP.NET-MVC: http://www.codeplex.com/mvcsamples. The Model is just the data, the view is the UI code that shows the data, and the Controller decides what View to show.

CSharpAtl
+1  A: 
  1. Model has a state called location and the view needs to know when this changes so it can update it's display

Create a interface for the View called IMyScreen or something similar Add a method UpdateLocation to the interface or something similar Create a variable or property functions that are visible to the state of type IMyScreen AND to the view. Call it something like MyScreen Have the View implement IMyScreen and put the code needed to update the View when the Location state changes in the UpdateLocation method. In your initialization routine of the View have the View register itself as the current MyScreen. Modify the code working with Location to fire MyScreen.UpdateLocation anytime it changes.

Ideally the View would create a Command object run it's execute method. THe execute method would pull in any parameters it needed, modify the locaction, and then call MyScreen.UpdateLocation. But you should focus on changing one thing at a time and save that for later.

  1. View has a button that the model needs to know when it is clicked

This is a little vague. Generally buttons like this either do an action or indicate a status (check boxes, options, etc)

If it does an action then the ideal method is to create a ICommand Interface. It has one method Execute. Use the initialization routine to pass in any needed parameters. Put the code that is needed to modify the model in the execute method. When the button is click it would do something like

Sub MyButton_Click
   ModifyCommand ThisCommand = New ModifyCommand(Parm1, Parm2, Parm3)
   ModifyCommand.Execute
End Sub

If you need to query the status of the button then use the IMyScreen Interface and add a property called ButtonClicked. When the button is clicked then set a flag that it has been clicked in the View When the Model needs to know if the button has been clicked it called MyScreen.ButtonClicked.

RS Conley