CS 160: Software Engineering September 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~ mak
Dec 29, 2015
CS 160: Software EngineeringSeptember 10 Class Meeting
Department of Computer ScienceSan Jose State University
Fall 2014Instructor: Ron Mak
www.cs.sjsu.edu/~mak
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
2
Model-View-Controller Architecture (MVC)
The Model-View-Controller (MVC) architecture is used for client-server applications that include a user interface._
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
3
Three Types of MVC Objects
Model objects Maintain the data and knowledge
of your application.
View objects Display the model to the user. The presentation layer.
Controller objects Manage the application flow. Handle user interactions.
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
4
Model-View-Controller Operation The user interacts with the
controller to send it commands. Via buttons, text fields,
mouse actions, etc.
The commands may tell the controller to modify the view directly, or the controller may alter the state of the model.
The altered model causes the view to update how it displays the model’s data.
The user may react to changes in the view by interacting with the controller to send new commands.
The user never manipulates the model directly,
only through the controller.
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
5
Model-View-Controller Example
alter state
update update
CONTROLLER
MODEL
VIEW #1 VIEW #2
User
send command
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
6
MVC Implementation for Web Applications
Implement the controller with servlets.
Goal: Reduce the amount of Java code in the JSPs. Move control logic into servlets. Servlets are multi-threaded and can be shared.
Implement the view with JSPs.
view = web pages JSPs should look like HTML as much as possible. Include tags for dynamically generated data.
_
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
7
MVC Implementation, cont’d
Implement the model with JavaBeans.
Persist the data maintained by the JavaBeans into a backend data repository, such as a MySQL database._
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
8
MVC Implementation: Loose Coupling
Keep the implementations of the three objects types separate.
Each type of objects does not depend on how the other types are implemented.
Your application is more robust (resilient to change)._
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
9
MVC Implementation: Parallel Development
Controller (servlets) Java programmers
View (JSPs) Web page designers
Model (JavaBeans and database) Java programmers Database developers
_
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
10
Handle User Interactions with Servlets Forms on HTML pages should send user data to servlets.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <title>Murach's Java Servlets and JSP</title></head><body> <h1>Join our email list</h1> <p>To join our email list, enter your name and email address below. <br> Then, click on the Submit button.</p> <form action="addToEmailList" method="get"> <table cellspacing="5" border="0"> ... </table> </form></body></html>
Servlet
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
11
Handle User Interactions with Servlets, cont’d Map a servlet name in the application file WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" ...>
... <!-- servlet definitions --> <servlet> <servlet-name>AddToEmailListServlet</servlet-name> <servlet-class>music.email.AddToEmailListServlet</servlet-class> </servlet>
...
<!-- servlet mappings --> <servlet-mapping> <servlet-name>AddToEmailListServlet</servlet-name> <url-pattern>/addToEmailList</url-pattern> </servlet-mapping>
...</web-app>
NetBeans will automaticallycreate this mapping for you.
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
12
Servlet Servlet JSP
After a servlet has done its work (possibly including interactions with the model components), it may want to pass data
to another servlet for more work, or
to a JSP to display the next web page.
Servlets are shared, multi-threaded code!
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
13
Servlet Servlet JSP, cont’d
The originating servlet stores the data into the request object:
User user = new User(firstName, lastName, emailAddress);request.setAttribute("userObj", user);
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
14
Servlet Servlet JSP, cont’d
The servlet forwards the request to a target servlet or JSP:
String url = "/cart/displayUserInfo.jsp";RequestDispatcher dispatcher = getServerContext().getRequestDispatcher(url);dispatcher.forward(request, response);
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
15
Servlet Servlet JSP, cont’d
The target servlet or JSP retrieves the data from the request object:
User user = (User) request.getAttribute("userObj");
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
16
Analysis
Identifying the three types of objects of a MVC application architecture is part of analysis._
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
17
Analysis Goal
Build an analysis model of the application that you’re developing that is correct, complete, consistent, and unambiguous.
Model building is a highly iterative and incremental activity.
The model describes the application domain.
Developers work with clients to update the functional specification as they discover new requirements.
Don’t confuse the uses of the word modelin Model-View-Controller and analysis model.
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
18
Analysis Model Submodels
1. Functional model Use cases and scenarios UML use case diagrams and descriptions
2. Object model Derive objects from the use cases Precursor for system design UML class and object diagrams
3. Dynamic model Behavior of the system UML sequence diagrams and statecharts
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
19
MVC Model Objects
Represent persistent information maintained by your application. AKA entity objects
Examine the participating objects in your use case descriptions.
Map parts of speech (nouns, ‘doing’ verbs, ‘having’ verbs, ‘being’ verbs, adjectives, etc.) to model components (classes, objects, operations, attributes, relationships, etc.)_
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
20
MVC Model Objects, cont’d
Color, size, positionAttributeAdjective
Must be, shall beConstraintsModal verb
Has a, consists of, includesAggregation‘Having’ verb
Is a kind of, is one of eitherInheritance‘Being’ verb
Creates, submits, selectsOperation‘Doing’ verb
PolicemanClassCommon noun
AliceObjectProper noun
ExampleModel ComponentPart of Speech
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
21
MVC View Objects
System interface with the actors.
View objects represent user interface components. Continue to use user-level terms.
In each use case, each actor interacts with at least one view object.
A view object collects information from the actor in a form that the model and controller objects can use._
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
22
MVC Controller Objects
Coordinate the model and view objects.
Often have no physical counterpart in the real world. Closely related to a use case. Collect information from view objects
for dispatch to model objects. This is how user-entered data can update the model
Represent application control flows._
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
23
Example: Bank ATM System
Log incustomer
Display balance
Shut downATM
Start upATM
Log outcustomer
Withdrawcash
Operator
Customer
Bank
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
24
Example: Bank ATM System (cont’d) Model objects
operator customer bank account cash
View objects display options (withdraw cash,
deposit check, etc.) messages
Controller objects Start up controller (“Start up ATM” use case) User verification controller (“Log in customer” use case) Withdrawal controller (“Withdraw cash” use case)
Log incustomer
Display balance
Shut downATM
Start upATM
Log outcustomer
Withdrawcash
Operator
Customer
Bank
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
25
Example Dynamic Model: ATM Cash Withdrawal
Customer
“Withdraw cash” Keypad Bank AccountDisplay
selectnotify
display confirmation
enter amount
notifyverify
acceptnotify
display bank adsnotify
dispense cash
TIME
UML Sequence Diagram
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
26
Dynamic Model: UML Sequence Diagram
A UML sequence diagram represents how the behavior of a use case is distributed among the participating objects._
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
27
Dynamic Model: UML Sequence Diagram, cont’d
Columns of the diagram represent the objects.
Horizontal arrows from one column to another represent messages or stimuli sent from one object to another (method invocations).
Receiving a message by an object triggers the object to activate an operation.
Vertical rectangles represent the duration of an operation’s activation.
Time proceeds from top to bottom.
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
28
Dynamic Model: UML Statechart Diagram
A UML statechart diagram represents the behavior of the system from the perspective of a single object._
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
29
Dynamic Model: UML Statechart Diagram, cont’d
Developers and clients may identify new behaviors (and new requirements).
Create statechart diagrams only for objects with extended lifetimes and state-dependent behavior
always for controller objects sometimes for model objects almost never for view objects
_
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
30
Example: Customer Withdraws Cash from ATM
Logged in
Cardaccepted
Readingbank ads
Has cash
swipe bank card
enter PIN
enter withdrawal amount get cash
leave
UML Statechart Diagram
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
31
Object Model Associations
An association is a relationship between two or more classes._
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
32
Object Model Associations, cont’d
Draw a line between two classes in a UML class diagram to show a relationship.
Clarify the object model by making relationships explicit.
Discover constraints associated with relationships. An association can have a name. You can show roles and multiplicities at each end.
Do not overdo showing associations.
Teammember
Usecase
writes
author artifact
1 *
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
33
Aggregations and Compositions
An aggregation is an “ownership” or “has a” association. Although there is a strong association between the
object and its owner, the object can exist on its own. An object can change owners or have several owners.
A composition is a “made up of” association. The constituent objects generally would not exist alone. This is the strongest association.
PageBook
aggregation
composition
Student
Shelf
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
34
Generalization
Generalization is a special association that consolidates common attributes or behavior among classes.
Subclasses inherit attributes and behavior from their superclasses.
Person
Instructor Student
subclasses
superclass
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
35
Attributes
An attribute is a property of an object. An association with another object is not an attribute.
Attributes are the least stable part of an object model.
Attributes often change or are discovered late. At the beginning, it’s not necessary
for attributes to describe fine details.
Student
gender : {male, female}id : Stringyear : integer
Methods will go here
Attributes
Class name
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
36
System Design
System design is the transformation of your analysis model into a system design model.
This is the design of your entire web application._
Computer Science Dept.Fall 2014: September 10
CS 160: Software Engineering© R. Mak
37
System Design Goal
A model that includes a subsystem decomposition and descriptions of chosen development strategies:
hardware/software strategy persistent data management strategy global control flow access control policy boundary conditions handling
_