Hey everyone,
I need a design pattern expert to help take me over the line here.
I think i have quite a good understanding of the theory of the Model View Controller pattern after reading a dozen articles on the topic - here is my understanding so far and please correct me if i'm wrong anywhere:
Model: encapsulates the data you want to model and provides methods for changing the models state. Notifies all observers (views) when it has changed.
View: the output of the model as a view into the system.
Controller: handles input usually via keyboard and mouse, updates the model and changes the view.
An example i thought of to explain my understanding would be a 3D house rotating program. The program could use a joystick to let the user move around the outside of the house. The model would be the data required to render the house such as points and vectors, current camera position etc. There could be multiple views outputing to a monitor at the same time - one for front view and one for top view. The controller could be an object that listens to the joystick events and manipulates the model as required, ie. moving the models current camera position.
What i mostly don't understand is how to implement this model in a Windows environment. I'm looking to build an example using c# and windows forms.
My problem mostly lies with data input for business applications - like a job form for adding a new job for a client. I'm having trouble understanding the roles of each object in this scenario. My guess is that the model would be the client and job data and methods to save and load from a database. The view would be the user interface displaying all the text boxes. The controller would recieve events from the view, ie. notifying when a save button is clicked. The controller then could somehow get the data from the view and use it to update the model and save to a database.
Questions:
---------------------------------
1. For the controller to get the data from the view, it has to have access to all of its textboxes, or at least accessor methods.
a. Wouldn't this be inefficient - especially if the modules are distributed on different machines?
b. How can we let two views use the one controller? ie. the first view might be windows forms which HAVE textboxes, but the second view might be a console window which does not.
2. Is data validation done in the controller or the model?
- if its done in the controller, then it would be good if multiple views could make use of it.
3. If textboxes are rendered in the view object, then how can the controller be the object that is listening to the keyboard and mouse - A textbox control automatically displays data as it is entered from the keyboard. The only way i can see is that the view notifies the controller when important events occur.
4. The controllers job is to update the model and change the view - what does 'change the view' mean?
a. Physically change the colour of a textbox on the view directly.
b. Ask the view to change the colour through a method call.
c. Load a totally new view object?
5. I've read other examples where the controller object renders the input screens, and the view object renders the output screens - but what if they are both the same screen? And how can we then reuse the controller objects validation across multiple views?
Thank you to anybody who can help me get through these questions.
Brett Birkett B.Comp
Systems Analyst
I need a design pattern expert to help take me over the line here.
I think i have quite a good understanding of the theory of the Model View Controller pattern after reading a dozen articles on the topic - here is my understanding so far and please correct me if i'm wrong anywhere:
Model: encapsulates the data you want to model and provides methods for changing the models state. Notifies all observers (views) when it has changed.
View: the output of the model as a view into the system.
Controller: handles input usually via keyboard and mouse, updates the model and changes the view.
An example i thought of to explain my understanding would be a 3D house rotating program. The program could use a joystick to let the user move around the outside of the house. The model would be the data required to render the house such as points and vectors, current camera position etc. There could be multiple views outputing to a monitor at the same time - one for front view and one for top view. The controller could be an object that listens to the joystick events and manipulates the model as required, ie. moving the models current camera position.
What i mostly don't understand is how to implement this model in a Windows environment. I'm looking to build an example using c# and windows forms.
My problem mostly lies with data input for business applications - like a job form for adding a new job for a client. I'm having trouble understanding the roles of each object in this scenario. My guess is that the model would be the client and job data and methods to save and load from a database. The view would be the user interface displaying all the text boxes. The controller would recieve events from the view, ie. notifying when a save button is clicked. The controller then could somehow get the data from the view and use it to update the model and save to a database.
Questions:
---------------------------------
1. For the controller to get the data from the view, it has to have access to all of its textboxes, or at least accessor methods.
a. Wouldn't this be inefficient - especially if the modules are distributed on different machines?
b. How can we let two views use the one controller? ie. the first view might be windows forms which HAVE textboxes, but the second view might be a console window which does not.
2. Is data validation done in the controller or the model?
- if its done in the controller, then it would be good if multiple views could make use of it.
3. If textboxes are rendered in the view object, then how can the controller be the object that is listening to the keyboard and mouse - A textbox control automatically displays data as it is entered from the keyboard. The only way i can see is that the view notifies the controller when important events occur.
4. The controllers job is to update the model and change the view - what does 'change the view' mean?
a. Physically change the colour of a textbox on the view directly.
b. Ask the view to change the colour through a method call.
c. Load a totally new view object?
5. I've read other examples where the controller object renders the input screens, and the view object renders the output screens - but what if they are both the same screen? And how can we then reuse the controller objects validation across multiple views?
Thank you to anybody who can help me get through these questions.
Brett Birkett B.Comp
Systems Analyst