Escolar Documentos
Profissional Documentos
Cultura Documentos
MVC
Copyright 2012 @ M.Fahad Khan
What is MVC?
ModelViewController (MVC) is an architecture that separates the representation of information from the user's interaction with it. The model consists of application data and business rules, and the controller mediates input, converting it to commands for the model or view. A view can be any output representation of data, such as a chart or a diagram. Multiple views of the same data are possible, such as a pie chart for management and a tabular view for accountants.
Model-View-Controller Pattern
MVC consists of three kinds of objects: Model the application object View UI (screen presentation) Controller defines the way the UI reacts to user inputs
Page 7
10 Million Question
Component interactions
In addition to dividing the application into three kinds of components, the MVC design defines the interactions between them. A controller can send commands to its associated view to change the view's presentation of the model (e.g., by scrolling through a document). It can send commands to the model to update the model's state (e.g., editing a document). A model notifies its associated views and controllers when there has been a change in its state. This notification allows the views to produce updated output, and the controllers to change the available set of commands. A passive implementation of MVC omits these notifications, because the application does not require them or the software platform does not support them. A view requests from the model the information that it needs to generate an output representation.
Component interactions
Model. The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller). View. The view manages the display of information. Controller. The controller interprets the mouse and keyboard inputs from the user, informing the model and/or the view to change as appropriate.
The Controller
Users interact with the controller. It interprets mouse movement, clicks, keystrokes, etc Communicates those activities to the model eg: delete row, insert row, etc Its interaction with the model indirectly causes the View(s) to update Controllers are a little more complicated. They act as an intermediary between the view objects and the model objects. When a value changes in a model, the controller is responsible for updating the view. Likewise, the controller knows when theres some user input and can update the model data accordingly. Controller objects are often responsible for setup tasks. The models and views must be loaded and initialized at some point, and the central role of the controller makes it a natural candidate for this work.
Copyright 2012 @ M.Fahad Khan
Page 15
The ControllerExample
To give you an idea of how this works, imagine an application that plots stock prices: 1. The user clicks a button (view) to refresh the graph. This action is sent to the controller. 2. The controller, in turn, tells the model to load new stock data. 3. The model opens a network connection and begins downloading data. 4. After the stock data is loaded, the model notifies the controller that new data has arrived. 5. The controller passes the new data onto the view, and the user sees the results.
Page 18
The Model
The MODEL contains the data Has methods to access and possibly update its contents. Often, it implements an interface which defines the allowed model interactions. Implementing an interface enables models to be pulled out and replaced without programming changes. Models are your applications heart and soul because theyre responsible for managing its data. Unlike views, models know nothing about the actions the user is performing or what theyre seeing on the display. A models only function is to manipulate and process the users data within the application. Models often implement internal logic that provides these basic behaviors.
Page 19
Page 21
The View
The VIEW provides a visual representation of the model. There can be multiple views displaying the model at any one time. For example, a companies finances over time could be represented as a table and a graph. These are just two different views of the same data. When the model is updated, all Views are informed and given a chance to update themselves.
Page 23
The View.(contd)
You know all those buttons, scrolling lists, web browsers, and everything else that appears on your iPhone screen? Those are all views. Views know how to present your applications data. Some views also know how to react to user input. In many applications, youll create your own views for displaying specific data. For example, theres no standard widget for displaying stock graphs, so if you want to do that, youll have to come up with your own solution.
Page 26
The View.(contd)
Likewise, many designers and developers want to customize the look of their application. Whether your motivation is product branding or just wanting to stand out in a crowd, creating a unique look for your app involves building new views based on standard UIView and UIControl classes.
Page 28
Example
Model-View-Controller talks about the three parts that most websites can be broken down into Controller Entry point of the application. Handles IO. PHP files View Templates displayed to the user. HTML files with PHP Model Functions that interact with the database or perform complex operations. SQL queries in PHP
Page 32
Page 44
Page 45
Page 47
Page 49
Page 50
Page 51
MVC Variants
Page 55
N tier Architecture
3 Tier architecture:
Presentation Layer Presentation Layer is the layer responsible for displaying user interface. Business Tier Business Tier is the layer responsible for accessing the data tier to retrieve, modify and delete data to and from the data tier and send the results to the presentation tier. This layer is also responsible for processing the data retrieved and sent to the presentation layer. - BLL and DAL Often this layer is divided into two sub layers: the Business Logic Layer (BLL), and the Data Access Layers (DAL). Business Logic Layers are above Data Access Layers, meaning BLL uses DAL classes and objects. DAL is responsible for accessing data and forwarding it to BLL. Data Tier Data tier is the database or the source of the data itself.
Common mistakes tightly coupling layers in technology and writing business logic in presentation tier
Copyright 2012 @ M.Fahad Khan
Page 56
Advantages of MVC
Page 58
Advantages of MVC
Separating Model from View (that is, separating data representation from presentation)
easy to add multiple data presentations for the same data facilitates adding new types of data presentation as technology develops. Model and View components can vary independently enhancing maintainability, extensibility, and testability.
The main advantage of adopting a design pattern like MVC is that it allows the code in each unit to be decoupled from the others, making it more robust and immune to changes in other code.
Page 60
Page 61
Consequences or Benefits
We make changes without bringing down the server. We leave the core code alone We can have multiple versions of the same data displayed We can test our changes in the actual environment. We have achieved separation of concerns
Page 62
Summary of MVC
Model - the model represents enterprise data and the business rules that govern access to and updates of this data View -the view renders the contents of a model. It accesses enterprise data through the model and specifies how that data should be presented Controller - the controller translates interactions with the view into actions to be performed by the model
Page 63
More Information
Copious amounts of information on MVC pattern available on Internet. Examples of MVC (Delegate style) all throughout Swing see JDK doc for details Hope the overview was useful to you! More tutorials, Source-code, etc available at our company web site, www.SocketSoftware.com We consult and develop applications in many languages, including Java and Microsoft.NET Australian based, Programming world-wide
Page 64
References
Home of the patterns community
http://hillside.net/
Page 65
Assignment 5
Design your own MVC Model for software Product .
COMING UP NEXT
Architecture-Based Testing and Analysis Software Architects: People and Teams
69