cs6320 – mvc l. grewe the issue: separating implementation from interface the business logic stays...
Post on 22-Dec-2015
214 views
TRANSCRIPT
THE ISSUE: Separating THE ISSUE: Separating Implementation from InterfaceImplementation from Interface
The business logic stays the same The business logic stays the same regardless of what the presentation isregardless of what the presentation is
The business logic does not depend on The business logic does not depend on the presentation medium, and the presentation medium, and different presentation media may be different presentation media may be used for the same business logicused for the same business logic
Separating the business logic from the Separating the business logic from the presentation results in more modular presentation results in more modular and reusable codeand reusable code
The MVC FrameworkThe MVC Framework MVC = Model – View – ControllerMVC = Model – View – Controller Model = Data (state of entity)Model = Data (state of entity) View = Interface (visualization of entity)View = Interface (visualization of entity) Controller = Business Logic - Controller = Business Logic -
communication between the interface communication between the interface and the data layerand the data layer• The controller may be a separate class, or The controller may be a separate class, or
just specific methods in the business logic just specific methods in the business logic class that are used for the communicationclass that are used for the communication
The Model ObjectThe Model Object Knows about the data needed to be Knows about the data needed to be
manipulatedmanipulated Knows about the business logic, including Knows about the business logic, including
operations that can be performed on the dataoperations that can be performed on the data Data should be accessed and manipulated Data should be accessed and manipulated
using methods, independent of the GUIusing methods, independent of the GUI e.g., A clock – the model only keeps track of e.g., A clock – the model only keeps track of
the time, with an internal representation, and the time, with an internal representation, and does not have anything to do with how it is does not have anything to do with how it is displayeddisplayed
Will allow external controls to modify stateWill allow external controls to modify state
The View ObjectThe View Object Should refer to the model to update its displayShould refer to the model to update its display Query the model using accessor methods to Query the model using accessor methods to
retrieve data from the modelretrieve data from the model Same model can have different views:Same model can have different views:
• A clock can have an analog viewA clock can have an analog view• A clock can have a digital viewA clock can have a digital view• A clock can be used as a timer/stopwatchA clock can be used as a timer/stopwatch• Note that the model for all of the above is the Note that the model for all of the above is the
samesame Different views would have no bearing on the Different views would have no bearing on the
intrinsic behavior of the modelintrinsic behavior of the model
The Controller ObjectThe Controller Object
Knows about physical means by which users Knows about physical means by which users manipulate data in the modelmanipulate data in the model
Responds to events from the GUI and translates them Responds to events from the GUI and translates them to method calls for the modelto method calls for the model
Usually sits between the user interface and the model Usually sits between the user interface and the model – and calls model methods based on view operations– and calls model methods based on view operations• e.g., setting the time of a clock by typing in the e.g., setting the time of a clock by typing in the
current time would cause the controller to trap the current time would cause the controller to trap the action event and calling, say, the “setTime” action event and calling, say, the “setTime” method of clockmethod of clock
Often controller will not need interaction of the model Often controller will not need interaction of the model (e.g., for sorting a view)(e.g., for sorting a view)
What does this mean?What does this mean? When you design business logic classes, you When you design business logic classes, you
should not use any explicit I/O (except for test should not use any explicit I/O (except for test code – maybe in the main() method)code – maybe in the main() method)
All interaction between business logic classes and All interaction between business logic classes and the outside world through public methodsthe outside world through public methods
When you use interface classes (application When you use interface classes (application classes, servlets, JSP) you should not use any classes, servlets, JSP) you should not use any explicit business logicexplicit business logic
Only code in interfaces classes should be class Only code in interfaces classes should be class instantiation and method callsinstantiation and method calls