1 / 9

What is MVC

What is MVC. Category: System MVC=Model-View-Controller Model: The data being maintained by the application (in the form of an object or objects) View: The presentation of the data Controller: The changing of data based on user inputs.

elmo
Download Presentation

What is MVC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. What is MVC • Category: System • MVC=Model-View-Controller • Model: The data being maintained by the application (in the form of an object or objects) • View: The presentation of the data • Controller: The changing of data based on user inputs. • Logically separates object data itself (model), from the presentation logic (view) and modification logic (controller) for that data.

  2. MVC Background • MVC often relies on two other patterns: Strategy & Observer • About Strategy: • Category: Behavioral • Strategy objects represent some logic to be performed • By being stored in separate objects, the logic is pluggable, simply by swapping out the strategy object being used. • Usually, the Controller part of MVC is implemented as a Strategy object to simplify choosing controllers • About Observer: • Category: Behavioral • Often a change to data requires a large number of disparate changes to occur (update 2 screens, send an email, save something to a file, etc). • The observer pattern provides a generic format to notify many observers in a change to an observable • The observable object, when changed, notifies all observer objects that it was changed, allowing each in turn to perform some functionality. • Usually, the model part of MVC is implemented as observable (the object to be watched), and the views are implemented as observers (the objects to do the watching); therefore when the model changes, the views are automatically updated.

  3. How to Use MVC • Create a model object to represent your data. • * Implement the observer pattern with your model to make updating the view objects more intuitive. • Create 1-many views that represent the model object, and potentially some functionality (such as editing) • Create 1-many controllers that represent the functionality for any views with controls on them. • * Implement the strategy pattern with your controllers to make them easily pluggable into the view. • * - Denotes an optional step

  4. Component DiagramNote: A component diagram and subsequently, components, are used to describe this pattern (as opposed to classes) because the model, controller, and view may not be one object, but a combination of many. From (Stephen Stelting, Olav Maassen, Applied Java™ Patterns)

  5. Class Diagram (Strategy) From (Stephen Stelting, Olav Maassen, Applied Java™ Patterns)

  6. Class Diagram (Observer) From (Stephen Stelting, Olav Maassen, Applied Java™ Patterns)

  7. Why MVC • MVC ensures that the three different components (model, view, and controller) are loosely coupled • Different views, and different controllers can be plugged in easily. • Not only can views differ in terms of functionality, they can also differ in terms of platform (Swing, console, web page, etc.)

  8. All-in-one Heavyweight Class public class Contact { // … instance variables // … getter/setter methods public void renderDisplayView() { // render display } public void renderEditView() { // render edit view // connect buttons/fields to this class } public void handleEdit(…) { // handle edit } } Modular, Lightweight Classes public class Contact { // … instance variables // … getter setter methods. } public interface ContactView { public void render(); } public class DisplayContactView { public void render() { // render display view… } } public class EditContactView { public void render() { // render edit view // connect buttons/fields to controller } } public interface ContactController { public void handle(…); } public Class EditContactController { public void handle() { // handle edit… } } ExampleLet’s say we are creating a program to manage contact information...

  9. Full Swing ExampleFrom (Stephen Stelting, Olav Maassen, Applied Java™ Patterns) (See associated .java files)

More Related