CS 2511

advertisement
CS 2511
UML Diagram Types

2 Main Types

Structure Diagrams
○
○
○

Class Diagrams
Component Diagrams
Object Diagrams
Behavior Diagrams
○
○
Sequence Diagrams
Use Case Diagrams
Class Diagrams


Type of static structure diagram
Describes:



The structure of a system by showing the
system’s classes
The class attributes
The relationships between classes
Class Node Layout
Class Name
Attributes of Class
(Data Members)
Operations of Class
(Member Functions)
Visibility Specifiers

To specify the visibility of a class
member (both attributes and methods)
use the following before the member's
name:




public: +
protected: #
private: package: ~
Example Class Diagram Revisited
public class Circle {
private double radius;
private double area;
protected double
getArea();
public void
setCenter(Point);
public Point center;
}
Circle
- radius: double
- area: double
+ center: Point
# getArea() : double
+ setCenter(Point)
Class Relationships


A relationship is a general term covering
the specific types of logical connections
found on class and object diagrams.
We’ll look at the following:



Dependency
Aggregation
Inheritance
Dependency


A very weak relationship
A class method has another class type
as a parameter or return value, or it
constructs an object of another class (a
class “uses” another class)
UML Dependency
Shape
- var1 : Point
+ draw(Circle c)
Circle
- radius: double
- area: double
+ center: Point
# getArea() : double
+ setCenter(Point)
Dependency in Code
Notice that draw(Circle c) is
public class Shape {
the only reference to class
public void draw(Circle c);
Circle in Shape, so a weak
private Point var1;
relationship
// Some other variables and methods …
}
public class Circle {
private double radius;
private double area;
protected double getArea();
public void setCenter(Point);
public Point center;
}
Aggregation
A class has an instance
variable that is another class
type or a collection of another
class type (a class object “has”
an object of another class type
as part of its state)
 The aggregate class (the class
with the white diamond
touching it) is in some way the
“whole,” and the other class in
the relationship is somehow
“part” of that whole.

Mailbox
- messages :
ArrayList<Message>
*
Message
Aggregation in Code
Note that this is a 1 to many
relationship since for every
Mailbox there are 0 or more
messages stored
public class Mailbox
{
private ArrayList<Message> messages;
// Other Variables and Methods Go Here
}
public class Message
{
// Variables and Methods Go Here
}
Inheritance




A class is a subclass of another
An object that is an instance of one
class “is” also an instance of another
A class inherits from another if it
incorporates the behavior of the other
class
(Possibly) capable of exhibiting
additional responsibilities
UML Inheritance
Child
+ method2()
Note only methods new to
Child are listed in Child class
(not methods inherited from
Parent)
Parent
+ method1()
Inheritance in Code
public class Parent{
public void method1();
}
public class Child extends Parent{
public void method2();
}
UML Diagram Types
2
Main Types
 Structure Diagrams
○ Class Diagrams
○ Component Diagrams
○ Object Diagrams
 Behavior Diagrams
○ Sequence Diagrams
○ Use Case Diagrams
Sequence Diagrams
A sequence diagram in Unified Modeling
Language (UML) is a kind of interaction
diagram that shows how processes
operate with one another and in what
order.
 In Java most of the interaction between
processes is done via objects, and thus a
sequence diagram basically shows how
objects interact with other objects.

Organization
Object Name:
Class Name
:Class Name
Method Call
Method Call
Object Name
Some Important Points

Each use case can be depicted as a
sequence diagram.

An underline with the class name and/or
object name usually means that the entity
being displayed is an object not a class.
Object

Each box represents an Object that is
alive (i.e. it’s constructor has been
called) at a particular time in the
computation.

3 options to represent objects :
Object Name:
Class Name
:Class Name
Object Name
LifeLines

The time for which an object is “alive” i.e.
still in scope (which means has not been
garbage collected yet) is represented by
a life line.
smallCircle
:Circle
Activation Bars

Activation bars are used to represent the time
when an object is calling a method.

The activation bars ends when the method that
was called returns.
smallCircle
:Circle
myMonitor:
Display
methodCall( )
Returned from method
Call Arrows and Method Names


A call is represented by an arrow going from
one object to another.
Method Names are represented on the call
arrow.
:BridgeProblem
<<create>>
currentState
:BridgeState
Examples
 Code :
public static void main(String[] args) {
BridgeState initialState = new BridgeState("west",
"west", "west", "west", "west", 0);
BridgeState finalState = new BridgeState("east",
"east", "east", "east", "east", 17);
initialState.display();
finalState.display();
}
Sequence Diagram
<<create>>
BridgeTest1.m
ain
<<create>>
display()
display()
initialState:
BridgeState
finalState:
BridgeState
Download