OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright 1998-1999 Rational Software,...

26
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright 1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design Using the UML UML-to-Java Mapping

Transcript of OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright 1998-1999 Rational Software,...

Page 1: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 1

Object Oriented Analysis and DesignUsing the UML

UML-to-Java Mapping

Page 2: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 2

Java Features

The slides in this presentation include some suggestions for UML to Java mappings.

Most of this class knows Java – but not all. So, I will try to elaborate upon the Java

language features as they are encountered in support of the slides containing UML that follow.

If you feel good about your personal knowledge of this material, feel free to leave and use your time, perhaps, more profitably.

Page 3: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 3

// Notes will be used in the// rest of the presentation// to contain Java code for// the attached UML elements

public class Course{ Course() { } protected void finalize() throws Throwable { super.finalize(); }};

Course

Mapping Representation: Notes

All Java programs are defined using class definitions.

All Java apps have a main method (not shown here) This is an example of simply a java class.

An exception may be thrown in Course in finalize().A throw statement is used to begin exception propagation and is well beyond where we are here.See pp. 457 – on current Java text used at UNF.

Course() is the Constructor that is executed when objects of this class are instantiated.Constructors do not return a value and do not have a return type…

Page 4: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 4

public class Student{

private String name;

public void addSchedule (Schedule theSchedule; Semester forSemester) { } public boolean hasPrerequisites(CourseOffering forCourseOffering) { }protected boolean passed(CourseOffering theCourseOffering) { }}

Visibility for Attributes and Operations

Student

- name : String

+ addSchedule (theSchedule: Schedule, forSemester: Semester)+ hasPrerequisites(forCourseOffering: CourseOffering) : boolean# passed(theCourseOffering: CourseOffering) : boolean

• Note what the UML translates into.• Notice the visibility and how this is translated into private protected, public..

• Public boolean returns true or false.• Method bodies not shown.

Page 5: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 5

class Student { private static int nextAvailID = 1;

public static int getNextAvaiIID() { }}

Class Scope Attributes and Operations

Student

- nextAvailID : int = 1

+ getNextAvaiIID() : int

The location at which a variable is declared defines its scope, which is the area within a program in which that variable can be referenced. By being declared at the class level (not w/in any method), these variables and constants can be referenced in any method of the class (or outside the class, if not private).

Attributes declared within methods are called instance data because memory space is created for each instance of the class that is created.

Above, we have a class variable (private) and a public class method, getNextAvailID() created from the UML. ‘static’ in Java means ‘at the class level.’ In Java, the static modifier associates a variable or method with its class rather than with an object of the class.

Page 6: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 6

<<utility>> MathPack

-randomSeed : long = 0randomSeed : long = 0-pi : double = 3.14159265358979-pi : double = 3.14159265358979

+sin (angle : double) : double+cos (angle : double) : double+random() : double

import java.lang.Math;import java.util.Random;class MathPack { private static randomSeed long = 0; private final static double pi = 3.14159265358979; public static double sin(double angle) { return Math.sin(angle); } static double cos(double angle) { return Math.cos(angle); } static double random() {

return new Random(seed).nextDouble(); }}

void somefunction() {. . . myCos = MathPack.cos(90.0);. . .

Utility Class A grouping of global attributes and operations – recall, we don’t instantiate these –

hence ‘static.’ Here, we are invoking several class methods (static methods) Math class is part of the Java standard class library and is defined in the java.lang

package. Below, we have a utility class called MathPack, which appears to be importing java.lang.Math and java.util.Random Reserved word static implies that the method can be invoked through the name of

the class, as in: Math.cos(angle) (below)

Page 7: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 7

Outer

Outer::Inner

class Outer{ public outer() { }

class Inner { public Inner() { } }}

Nested Class

Hide a class that is relevant only for implementation

• Can declare a class inside another class (just like a loop). Nested classes are considered a member of the enclosing class.• Creates a separate bytecode file; has the extension .class and is referenced via: Enclosing$nested.class.• Nested class has access to enclosing class’s instance variables and methods, even if declared with private visibility. But enclosing class can access data in nested class only if data is declared public. Nested classes is the exception to declaring data ‘public.’ It is normal to declare data of a private nested class ‘public’ because only the enclosing class can get to that data (despite its public declaration.)

Page 8: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 8

Schedule

Student

class Student{ public Student() { } private Schedule theSchedule;

}

// no need to import if in same package

class Schedule{ public Schedule() { } //constructor private Student theStudent; }

Associations Bi-directional associations; Two classes in package. Note the declarations in each class needed to support

bidirectional associations…both have public methods (to ‘know’ each other) and private data.

Create objects of other class.

Page 9: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 9

Schedule

Student

class Student{ public Student() { } private Schedule theSchedule;}

class Schedule{ public Schedule() { }}

Association Navigability Uni-directional associations

Student knows about schedule; Student is a client of Schedule. Can Create objects of other class.

Page 10: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 10

Professor

CourseOffering

instructor

class Professor {public Professor() {}private CourseOffering theCourseOffering;}

class CourseOffering {public CourseOffering() {}private Professor instructor;}

Association Roles

• Adding a role indicator: Each class ‘knows’ about the other class. • For class CourseOffering, this contains a public method, CourseOffering() and a private declaration of an object (instructor) of type Professor. • For class Professor, this contains a public method, Professor, and the creation of a private object theCourseOffering of type CourseOffering.

Page 11: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 11

CourseOffering

Schedule

class Schedule{public Schedule() {}private CourseOffering[] primaryCourses =

new CourseOffering[4]}

class CourseOffering{public CourseOffering() {}}

0..4 primaryCourses

Association Multiplicity

• Here we are showing how multiplicity in UML is accommodated in Java. • Have class CourseOffering containing a public method, CourseOffering().• In class Schedule, in addition to the public method, Schedule(), we have a new array of four CourseOffering objects each object is of type CourseOffering.

Page 12: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 12

// No need to import if in the same package

class PrimaryScheduleOfferingInfo{ public PrimaryScheduleOfferingInfo() {}

public CourseOffering get_theCourseOffering(){ return theCourseOffering; }

public void set_theCourseOffering(CourseOffering toValue){ theCourseOffering = toValue; }

private char get_Grade (){ return grade; } private void set_Grade(char toValue) { grade = toValue; } private char grade = ‘I’; private CourseOffering theCourseOffering;}

Association Class

Schedule CourseOffering

0..40..*

primaryCourses

PrimaryScheduleOfferingInfo

- grade: char = I

alternateCourses

0..20..*

0..*

Schedule

CourseOffering11 0..4

0..2

0..* alternateCourses

primaryCourseOfferingInfoPrimaryScheduleOfferingInfo

- grade: char = I

Design Decisions

• Remember, an association class is a class that is connected to an association. There is an instance of the association class for every instance of the relationship (e.g., for every link).

Page 13: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 13

// No need to import if in the same package

class PrimaryScheduleOfferingInfo{ public PrimaryScheduleOfferingInfo() {}

public CourseOffering get_theCourseOffering(){ return theCourseOffering; } public void set_theCourseOffering(CourseOffering toValue){ theCourseOffering = toValue; } private char get_Grade (){ return grade; } private void set_Grade(char toValue) { grade = toValue; } private char grade = ‘I’; private CourseOffering theCourseOffering;}

Association Class

Schedule CourseOffering

0..40..*

primaryCourses

PrimaryScheduleOfferingInfo

- grade: char = I

alternateCourses

0..20..*

0..*

Schedule

CourseOffering11 0..4

0..2

0..* alternateCourses

primaryCourseOfferingInfoPrimaryScheduleOfferingInfo

- grade: char = I

Design Decisions

• During design, some decisions are made regarding navigation between the involved classes. A subset of the class operations and attributes are shown above. For this example, we included a subset to demonstrate the UML construct we are emphasizing.

Page 14: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 14

Course

prerequisites

0..* import java.util.Vector;

class Course { public Course() {}// The unbounded multiple association// is stored in a vector private Vector prerequisites;}

Reflexive Associations

• A class may have an association with objects of the same type, as we know.

• Here is the corresponding Java realization of that realization.

• Vector is a class in java.util. Vector is a public class … that manages an array of objects.

• Elements can be added or removed from this list and the size of the list can change

dynamically.

• Objects of type Vector include methods such as copyInto, elementAt, contains,

insertElementAt, addElement and much more.

Page 15: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 15

Schedule

Student

class Schedule{public Schedule() { }private Student theStudent;}0..*

1

import java.util.Vector;

class Student{ public Student() { } private Vector theSchedule;}

Aggregation

• Vector is a reusable list class available in the Java programming environment. • ‘Student’ class declares an reusable list class of type ‘Vector.’• (Java has no explicit construct for aggregation. So it treats aggregation kind of like

an array of objects)• In Java, the code for aggregation looks the same as it does for “vanilla” association, where each class ‘knows about’ each other...)

Page 16: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 16

Schedule

Student

class Schedule{public Schedule() { }private Student theStudent;}0..*

1

import java.util.Vector;

class Student{public Student() { }private Vector theSchedule = new Vector();}

Composition

Java does not support containment by value.

theSchedule is a new class list created by Student…

This part is the same as aggregation…

Page 17: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 17

Serializable<<Interface>>

Schedule<<entity>>

interface Serializable {

}

class Schedule implements Serializable {

}

Interfaces and Realizes Relationships

• Java has the notion of ‘implements’ which translates to the ‘realizes relationship’ in the UML.

• Java classes implement interfaces and they can implement as many interfaces as they need to.• Interfaces can extend other interfaces.

•In Java, interfaces may have attributes defined for them. This differs from pure UML definition of interface which states, “An interface is a declaration of a collection of operations that may be used for defining a service offered by an instance. Interfaces may not have attributes, associations, or methods.”

Page 18: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 18

GroundVehicle

class Truck extends GroundVehicle{ public float tonnage; public void getTax() { }}

class GroundVehicle{ public int licenseNumber; public void register() { }}

+licenseNumber: int

+register()

Truck

+tonnage: float

+getTax()

Generalization

• Remember: generalization is a ‘is-a’ or ‘kind of’ type of association. • It is used to represent a generalization type of association • Generalization is modeled as an open triangular arrowhead pointing to the base of the “base class” end.• Java has the notion of ‘extends’ which translates to the generalization relationship in the UML.• Java classes can extend ONE other class. • Interfaces can extend other interfaces. Java interfaces are discussed ahead….

Page 19: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 19

<<Interface>>IVehicle

<<Interface>> IWaterVehicle

LandVehicle

Amphibious Vehicle

{overlapping}

class AmphibiousVehicle extends LandVehicle implements WaterVehicle{ . . .}

<<realize>>

<<realize>>interface IWaterVehicle : extends IVehicle{ . . .}

In Java, a class can only inherit from one superclass. It can, however implement multiple interfaces

Multiple Inheritance

• In Java, a class can only inherit from ONE superclass. However, a class can realize multiple interfaces.• Remember, subclasses not mutually exclusive can be annotated with the UML {overlapping} constraint. • This supports multiple inheritance.

Page 20: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 20

Multiple Inheritance (contd)

AmphibiousVehicle

LandVehicle IAmphibiousVehicle<<Interface>>

IHobby<<Interface>>

IWaterVehicle<<Interface>>

ILandVehicle<<Interface>>

In Java, a class can inherit from one superclass.It can, however implement multiple interfaces.

An interface can inherit from many interfaces.

Java supports multiple inheritance between interfaces.In Java, an interface CAN inherit from multiple interfaces.Multiple inheritance of interfaces overcomes the ‘weakness’ of Java regarding true multiple inheritance.

Page 21: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 21

abstract class Animal { public abstract void talk();}

Animal{abstract}

Lion

+talk()

Tiger

+talk()

class Tiger extends Animal{ public Tiger() { } public void talk() { }}

+talk() {abstract}

Abstract Class

Remember, an abstract class is a class for which no instances are created.

Page 22: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 22

SetT, n:int

<<bind>>

mySetOfFloats

insert(T)remove(T)

<float, 100>

Java does not support parameterized classes

Parameterized Class

Page 23: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 23

package CourseCatalog;

public interface ICourseCatalog {

public CourseOfferingList

getCourseOfferings();

}

CourseCatalog<<subsystem>>

ICourseCatalog

getCourseOfferings() : CourseOfferingList

Subsystems

There can be only one public class per file. (You can have inner classes in the file as well, but only one ‘top level’ class). The name of the file must be the same as the name of the public class.

Page 24: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 24

package CourseCatalog;

public interface ICourseCatalog {

public CourseOfferingList getCourseOfferings();

}

CourseCatalog<<subsystem>>

ICourseCatalog

getCourseOfferings() : CourseOfferingList

Subsystems

These restrictions are what hinderJava’s support for subsystems. In the UML, the mapping between interfaces and subystems is many to many (subsystems can realize one or more interfaces; interfaces can be realized by one or more subsystems). In Java the mapping is always one-to-one.

Note: CourseOfferingList is assumed to exist in a separate common package.The import statement has been excluded from the code fragment.

Page 25: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 25

package CourseCatalog;

public interface ICourseCatalog {

public CourseOfferingList getCourseOfferings();

}

CourseCatalog<<subsystem>>

ICourseCatalog

getCourseOfferings() : CourseOfferingList

Subsystems and UML

As we have discussed w/i OOAD course subsystems are the Design Modelrepresentation for components.

Some aspects of UML subsystems can beimplemented using Java packagesJava packages have a 1-1 correspondenceto UML packages.

Page 26: OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2 Copyright  1998-1999 Rational Software, all rights reserved 1 Object Oriented Analysis and Design.

OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2Copyright 1998-1999 Rational Software, all rights reserved 26

Packages…

Packages in Java also correspond to directories.

Packages are declared at the top of the file. All classes defined within the file are considered part of the specified package.

All classes defined within the same package\ can see each other automatically.

If the package statement is omitted (i.e., no package is specified), the file contents are considered to be in the ‘default package’ (e.g., the root package), and all other classes for which a package was not specified can see the classes defined within a file.