Date post: | 19-Dec-2015 |
Category: |
Documents |
View: | 215 times |
Download: | 0 times |
Copyright © 2012 Pearson Education, Inc.
UML
Java Software SolutionsFoundations of Program Design
Seventh Edition
John LewisWilliam Loftus
Unified Modeling Language• A graphical language used for designing programs
– http://www.uml.org
• UML for short• Used for illustrating the relationships that exist
among classes in a program• There are several kinds of UML diagrams
– Class diagrams– Object diagrams– Collaboration diagrams– Sequence diagrams
Class Diagrams• Use a box to represent a class• Use lines between the classes to represent the
relationships between them• The box representing a class is divided into up to
three sections– The name of the class goes in the top– The instance variables are listed in the middle section– The methods are listed in the bottom section
• Member visibility represented by a symbol+ for public- for private- # for protected
Example Class Diagram
Copyright © 2012 Pearson Education, Inc.
Dependency• A dependency exists when one class relies on
another in some way, usually by invoking the methods of the other
• We say the dependent class uses the class it depends on
• A driver class (main class) always uses other classes
• Use a dashed arrow to represent the uses relationship
– Arrow points to the class being used
Copyright © 2012 Pearson Education, Inc.
Class Diagram for Pig Latin
PigLatin
+ main (args : String[]) : void
+ translate (sentence : String) : String- translateWord (word : String) : String- beginsWithVowel (word : String) : boolean- beginsWithBlend (word : String) : boolean
PigLatinTranslator
Copyright © 2012 Pearson Education, Inc.
Aggregation
• An aggregate is an object that is made up of other objects
– An aggregate object contains references to other objects as instance data
• Therefore aggregation is a has-a relationship
• This is a special kind of dependency; the aggregate relies on the objects that compose it
• Use a solid line with an open diamond to represent has-a
– Diamond is next to the containing classCopyright © 2012 Pearson Education, Inc.
Aggregation in UML
StudentBody
+ main (args : String[]) : void
+ toString() : String
Student- firstName : String- lastName : String- homeAddress : Address- schoolAddress : Address
+ toString() : String
- streetAddress : String- city : String- state : String- zipCode : long
Address
Copyright © 2012 Pearson Education, Inc.
Inheritance
• Inheritance relationships are shown in a UML class diagram using a solid arrow with an unfilled triangular arrowhead pointing to the parent class
Vehicle
Car
• Proper inheritance creates an is-a relationship, meaning the child is a more specific version of the parent
Copyright © 2012 Pearson Education, Inc.
Abstract classes
• Use abstract for the stereotype to show a class is abstract
4-10
Class Diagram for Words
Book
# pages : int
+ pageMessage() : void
Dictionary
- definitions : int
+ definitionMessage() : void
Words
+ main (args : String[]) : void
Copyright © 2012 Pearson Education, Inc.
Class Hierarchies
• Sometimes we only want to show a class hierarchy– Leave out most of the detail
Copyright © 2012 Pearson Education, Inc.
Interfaces
• Implementing an interface also creates an is-a relationship
• Use a dashed arrow with an outlined triangular head
• Use interface for the stereotype
4-13
Spring 2011
CS 225 14
UML Symbols
Symbol Visibility
+ public
- private
# protected
~ package
Relationship Connector
inheritance (is-a)
open arrow
association (uses)
solid line
aggregation/composition(has-a)
open/ soliddiamond
inner class circle with cross
Object Diagrams• These diagrams are used to represent the state of
the program at some time while it is running– The state changes as the program runs
• Represent an object with a box– The type of the object is underlined– The box contains other boxes to hold the instance data
Copyright © 2012 Pearson Education, Inc.
Object diagram
• An object is also represented as a box
• The name of the object (optional) and the name of the class are underlined
• Attribute values may be shown
4-16
Object Diagrams• There are several kinds of diagrams that show the
objects in a program and how they interact– Object diagrams show the state of the objects at a
particular point in the program– Collaboration diagrams show the interactions between
objects by showing method calls– Sequence diagrams show interactions between objects
as a function of time
4-17
Object Diagram
Copyright © 2012 Pearson Education, Inc.
Collaboration Diagram
• Shows interactions (e.g. method calls) between objects
• Active object is bold
4-19
Sequence Diagram
• Show interactions between objects as a function of time
• Time axis goes down
4-20
Spring 2011
CS 225 21
Tools for UML Diagrams• On onyx
– dia– umbrello– violet
• There are also Eclipse plug-ins but they are usually not open-source– Some of these can build code from the UML diagram