90 likes | 241 Views
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.
E N D
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.
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.
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
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)
Class Diagram (Strategy) From (Stephen Stelting, Olav Maassen, Applied Java™ Patterns)
Class Diagram (Observer) From (Stephen Stelting, Olav Maassen, Applied Java™ Patterns)
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.)
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...
Full Swing ExampleFrom (Stephen Stelting, Olav Maassen, Applied Java™ Patterns) (See associated .java files)