Transcript Week 03

CS 2511
Fall2014 2014
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)
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)
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 depends on another class if it
manipulates objects of the other class

UML Dependency
Shape
- var1 : Point
+ draw(Circle c)
Circle
- radius: double
- area: double
+ center: Point
# getArea() : double
+ setCenter(Point)
Dependency in Code
public class Shape
{
public void draw(Circle c);
private Point var1;
// Some other variables and methods …
}
public class Circle
{
private double radius;
private double area;
protected double getArea();
public void setCenter(Point);
public Point center;
}
Notice that draw(Circle c) is
the only reference to class
Circle in Shape, so a weak
relationship
Aggregation


A class aggregates
another if its objects
contain objects of the
other class.
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
public class Mailbox
{
private ArrayList<Message> messages;
// Other Variables and Methods Go Here
}
public class Message
{
// Variables and Methods Go Here
}
Note that this is a 1 to many
relationship since for every
Mailbox there are 0 or more
messages stored
Inheritance
A class inherits from another if it
incorporates the behavior of the other
class
 Special Cases of objects of the parent
class type
 (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();
}