CS 635 Advanced Object-Oriented Design & Programming Spring Semester, 2004 Model-View-Controller |
||
---|---|---|
© 2004, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 15-Apr-04 |
Model-View-Controller (MVC)
Context
Interactive application with human-computer interface
Forces
Model
Core application code
Contains a list of observers (view or controller)
Has a broadcast mechanism to inform views of a change
Same mechanism as subject in Observer pattern
View
Displays information to user
Obtains data from model
Each view has a controller
Controller
Handles input from user as events
View & Controller
Make up the user interface
Some GUI frameworks combine these
VW Smalltalk contains both, but hides controller from programmer
|
|
|
|
ApplicationDialogController BasicButtonController ClickWidgetController ColoredAreaController ComboBoxButtonController ComboBoxInputBoxController ComboBoxListController ControllerWithMenu ControllerWithSelectMenu DataSetController DataSetControllerProxy DelayingWidgetController DrawingController DropDownListController EmulatedDataSetController EmulatedSequenceController EmulationScrollBarController HierarchicalSequenceController InputBoxController |
ActionButtonView AutoScrollingView BasicButtonView BooleanWidgetView CheckButtonView ClickWidget ComboBoxButtonView ComboBoxInputFieldView ComboBoxListView ComposedTextView DataSetView DefaultLookCheckButtonView DefaultLookRadioButtonView EmulationScrollBar GeneralSelectionTableView HierarchicalSequenceView HorizontalTabBarView HorizontalTopTabBarView InputFieldView |
Template View
Renders information into HTML by embedding markers in an HTML page
Server PagesJava <html> <body> <%! int x = 1; %> <%! int y = 2; %> If we add <%= x %> to <%= y %> we will get <%= x + y %> </body> </html>
Smalltalk <HTML> <BODY> <% x :=1. y :=2. %> <p> When we add <%= x %> to <%= y %> we get <%= x + y %> </p> </BODY> </HTML>
SDSU Template Class
Markers
<HTML> <BODY> </p> When we add @@@x@@@ to @@@y@@@ we get @@@sum@@@ <p> </BODY> </HTML>
template := Template fromFile: Sum.html. template replace: 'x' with: 1 printString. template replace: 'y' with: 2 printString. template replace: 'sum' with: 3 printString. template asString
<HTML> <BODY> <p> When we add 1 to 2 we get 3 </p> </BODY> </HTML>
<p>Please pay your bill <If user.isDeadBeat()> <B> </IF> now. <IF use.isDeadBeat()> </B> </IF>
ServletsJava import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<body>"); out.println("<head>"); out.println("<title>Hello World!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello World!</h1>"); out.println("</body>"); out.println("</html>"); } }
Smalltalk Smalltalk.VisualWave defineClass: #VeryBasicServlet superclass: #{VisualWave.HttpServlet} indexedType: #none private: false instanceVariableNames: '' classInstanceVariableNames: '' imports: '' category: 'Web Toolkit-Servlet' VisualWave.VeryBasicServlet methodsFor: 'serving' doGet: aRequest response: aResponse aResponse write: '<HTML><BODY>GET<BR> Hello world</BODY></HTML>'. doPost: aRequest response: aResponse aResponse write: '<HTML><BODY>POST<BR> Hello world</BODY></HTML>'.
Page Controller
An object that handles a request for a specific page or action on a Web page
Page Controller
public class AlbumController extends ActionServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { Album requestedAlbum = Album.find(request.getParameter(“id”)); if (requestedAlbum == null) { forward( “missingAlbumError.jsp”, request, response ); return; } request.setAttribute(“helper”, requestedAlbum); if (requestedAlbum.isClassical() ) forward(“/classicalAlbum.jsp”, request, response); else if (requestedAlbum.isRock() ) forward(“/rockAlbum.jsp”, request, response); else forward(“/album.jsp”, request, response); }
Parent Class of All Page Controllers
public class ActionServlet extends HttpServlet { protected void forward(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(target); dispatcher.forward(request, response); }
Front Controller
A controller that handles all requests fro a Web site
Forces
How it works
All requests to a Web site are directed to the FrontController
The FrontController
Intercepting Filter
You want to manipulate a request and a response before and after the request is processed
Forces
You want
Composite View
Build a view from atomic components while managing content and layout indepently
Forces