®
IBM Software Group
Mastering Object-Oriented Analysis and Design
with UML 2.0
Appendix: UML to Java Mapping
1
Mapping Representation: Notes
// Notes will be used in the
// rest of the presentation
// to contain Java code for
// the attached UML elements
public class Course
Course {
Course() { }
protected void finalize()
throws Throwable {
super.finalize();
}
};
2
Visibility for Attributes and Operations
Student
- name : String
+ addSchedule (theSchedule: Schedule, forSemester: Semester)
+ hasPrerequisites(forCourseOffering: CourseOffering) : int
# passed(theCourseOffering: CourseOffering) : int
public class Student
{
private String name;
public void addSchedule (Schedule theSchedule; Semester forSemester) {
}
public boolean
hasPrerequisites(CourseOffering forCourseOffering) {
}
protected boolean
passed(CourseOffering theCourseOffering) {
}
}
3
Class Scope Attributes and Operations
class Student
Student {
private static int nextAvailID = 1;
- nextAvailID : int = 1
public static int getNextAvaiIID() {
+ getNextAvaiIID() : int }
}
4
Utility Class
A grouping of global attributes and
operations import java.lang.Math;
import java.util.Random;
<<utility>> class MathPack
MathPack {
private static randomSeed long = 0;
-randomSeed : long = 0 private final static double pi =
-pi : double = 3.14159265358979 3.14159265358979;
public static double sin(double angle) {
+sin (angle : double) : double return Math.sin(angle);
}
+cos (angle : double) : double
static double cos(double angle) {
+random() : double return Math.cos(angle);
}
static double random() {
void somefunction() { return new
...
myCos = MathPack.cos(90.0); Random(seed).nextDouble();
... }
}
5
Nested Class
Hide a class that is relevant only for
implementation
class Outer
{
Outer public outer() { }
class Inner {
public Inner() { }
}
}
Outer::Inner
6
Associations
Bi-directional associations
// no need to import if in same package
class Schedule
{
Schedule public Schedule() { }
private Student theStudent ;
}
class Student
{
public Student() { }
private Schedule theSchedule;
Student }
7
Association Navigability
Uni-directional associations
class Schedule
{
Schedule
public Schedule() { }
}
class Student
{
public Student() { }
private Schedule theSchedule;
Student }
8
Association Roles
class Professor
{
public Professor() {}
private CourseOffering theCourseOffering;
Professor }
instructor
class CourseOffering
{
public CourseOffering() {}
private Professor instructor;
CourseOffering }
9
Association Multiplicity
class CourseOffering
{
CourseOffering
public CourseOffering() {}
}
0..4 primaryCourses
class Schedule
{
public Schedule() {}
private CourseOffering[] primaryCourses =
Schedule new CourseOffering[4]
}
10
Association Class
alternateCourses // No need to import if in the same package
0..* 0..2 class PrimaryScheduleOfferingInfo
{
CourseOffering public PrimaryScheduleOfferingInfo() {}
Schedule primaryCourses
public CourseOffering get_theCourseOffering(){
0..* 0..4 return theCourseOffering;
}
public void set_theCourseOffering(CourseOffering toValue){
theCourseOffering = toValue;
}
PrimaryScheduleOfferingInfo private char get_Grade (){ return grade; }
private void set_Grade(char toValue) { grade = toValue; }
- grade: char = I private char grade = ‘I’;
private CourseOffering theCourseOffering;
}
Design Decisions
alternateCourses
0..* 0..2
Schedule primaryCourseOfferingInfo PrimaryScheduleOfferingInfo CourseOffering
1 0..4 - grade: char = I 0..* 1
11
Reflexive Associations
prerequisites
0..* import java.util.Vector;
class Course
Course {
public Course() {}
// The unbounded multiple association
// is stored in a vector
private Vector prerequisites;
}
12
Aggregation
class Schedule
{
Schedule
public Schedule() { }
private Student theStudent;
0..* }
1 import java.util.Vector;
class Student
Student {
public Student() { }
private Vector theSchedule;
}
13
Composition
class Schedule
{
Schedule
public Schedule() { }
private Student theStudent;
0..* }
import java.util.Vector;
1 class Student
{
public Student() { }
Student private Vector theSchedule = new Vector();
}
14
Interfaces and Realizes Relationships
<<Interface>>
Serializable interface Serializable {
}
class Schedule implements Serializable {
<<entity>>
Schedule }
15
Generalization
GroundVehicle class GroundVehicle
{
+licenseNumber: int public int licenseNumber;
public void register() { }
+register() }
Truck
class Truck extends GroundVehicle
+tonnage: float {
public float tonnage;
+getTax() public void getTax() { }
}
16
Multiple Inheritance
In Java, a class can only inherit from one
superclass. It can, however implement
<<Interface>>
IVehicle multiple interfaces
{overlapping}
interface IWaterVehicle :
<<realize>> implements IVehicle
Land {
<<Interface>>
Vehicle ...
IWaterVehicle
}
<<realize>>
class AmphibiousVehicle
Amphibious extends LandVehicle
Vehicle implements WaterVehicle
{
...
}
17
Multiple Inheritance (continued)
<<Interface>> <<Interface>>
ILandVehicle IWaterVehicle
An interface can inherit
from many interfaces.
<<Interface>>
LandVehicle
IAmphibiousVehicle
In Java, a class can inherit
from one superclass.
It can, however implement
multiple interfaces.
<<Interface>>
AmphibiousVehicle
IHobby
18
Abstract Class
Animal abstract class Animal
{abstract} {
public abstract void talk();
+talk() {abstract} }
class Tiger extends Animal
{
Lion Tiger public Tiger() { }
public void talk() { }
+talk() +talk() }
19
Parameterized Class
Java does not support parameterized classes
T, n:int
Set
insert(T)
remove(T)
<<bind>>
<float, 100>
mySetOfFloats
20
Subsystems
ICourseCatalog <<subsystem>>
CourseCatalog
package CourseCatalog;
public interface ICourseCatalog {
public CourseOfferingList
getCourseOfferings();
}
21
22