Download Object Oriented Analysis and Design Using the UML

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Object Oriented Analysis and Design
Using the UML
UML-to-Java Mapping
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
1
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.
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
2
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 {
All Java programs are defined using class definitions.
super.finalize();
}
All Java apps have a main method (not shown here) This
};
is an example of simply a java class.
Course() is the Constructor that is
executed when objects of this
An exception may be thrown in Course in finalize().
A throw statement is used to begin exception propagation class are instantiated.
Constructors do not return a value
and is well beyond where we are here.
and do not have a return type…
See pp. 457 – on current Java text used at UNF.
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
3
Visibility for Attributes and Operations
• Note what the UML
translates into.
Student
• Notice the visibility and how
- name : String
this is translated into private
protected, public..
+ addSchedule (theSchedule: Schedule, forSemester: Semester)
+ hasPrerequisites(forCourseOffering: CourseOffering) : boolean
# passed(theCourseOffering: CourseOffering) : boolean
• Public boolean returns true
or false.
• Method bodies not shown.
public class Student
{
private String name;
public void addSchedule (Schedule theSchedule; Semester forSemester) {
}
public boolean
hasPrerequisites(CourseOffering forCourseOffering) {
}
protected boolean
passed(CourseOffering theCourseOffering) {
}
}
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
4
Class Scope Attributes and Operations
Student
- nextAvailID : int = 1
class Student
{
private static int nextAvailID = 1;
public static int getNextAvaiIID() {
}
+ 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.
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
5
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)
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();
<<utility>>
MathPack
-randomSeed : long = 0
-pi : double = 3.14159265358979
+sin (angle : double) : double
+cos (angle : double) : double
+random() : double
void somefunction() {
...
myCos = MathPack.cos(90.0);
...
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
6
}
}
Nested Class
 Hide a class that is relevant only for implementation
class Outer
{
public outer() { }
Outer
class Inner {
public Inner() { }
}
}
Outer::Inner
• 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.)
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
7
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.
// no need to import if in same package
Schedule
class Schedule
{
public Schedule() { } //constructor
private Student theStudent;
}
class Student
{
public Student() { }
private Schedule theSchedule;
Student
}
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
8
Association Navigability
 Uni-directional associations
 Student knows about schedule; Student is a client of Schedule.
 Can Create objects of other class.
Schedule
class Schedule
{
public Schedule() { }
}
class Student
{
public Student() { }
private Schedule theSchedule;
}
Student
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
9
Association Roles
Professor
instructor
CourseOffering
class Professor
{
public Professor() {}
private CourseOffering theCourseOffering;
}
class CourseOffering
{
public CourseOffering() {}
private Professor instructor;
}
• 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.
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
10
Association Multiplicity
CourseOffering
0..4
class CourseOffering
{
public CourseOffering() {}
}
primaryCourses
Schedule
class Schedule
{
public Schedule() {}
private CourseOffering[] primaryCourses =
new CourseOffering[4]
}
• 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.
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
11
Association Class
// No need to import if in the same package
alternateCourses
0..*
class PrimaryScheduleOfferingInfo
{
public PrimaryScheduleOfferingInfo() {}
0..2
Schedule
primaryCourses
public CourseOffering get_theCourseOffering(){
return theCourseOffering;
}
CourseOffering
0..4
0..*
public void set_theCourseOffering(CourseOffering toValue){
theCourseOffering = toValue;
}
PrimaryScheduleOfferingInfo
private char get_Grade (){ return grade; }
private void set_Grade(char toValue) { grade = toValue; }
private char grade = ‘I’;
private CourseOffering theCourseOffering;
- grade: char = I
}
• 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).
Design Decisions
0..*
Schedule
alternateCourses
0..2
primaryCourseOfferingInfo
1
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
0..4
PrimaryScheduleOfferingInfo 0..* 1
- grade: char = I
12
CourseOffering
Association Class
// No need to import if in the same package
alternateCourses
0..*
class PrimaryScheduleOfferingInfo
{
public PrimaryScheduleOfferingInfo() {}
0..2
Schedule
primaryCourses
CourseOffering
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;
0..4
0..*
PrimaryScheduleOfferingInfo
- grade: char = I
}
• 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.
Design Decisions
0..*
Schedule
alternateCourses
0..2
primaryCourseOfferingInfo
1
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
0..4
PrimaryScheduleOfferingInfo 0..* 1
- grade: char = I
13
CourseOffering
Reflexive Associations
prerequisites
0..*
Course
import java.util.Vector;
class Course
{
public Course() {}
// The unbounded multiple association
// is stored in a vector
private Vector prerequisites;
}
• 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.
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
14
Aggregation
class Schedule
{
public Schedule() { }
private Student theStudent;
}
Schedule
0..*
import java.util.Vector;
1
Student
class Student
{
public Student() { }
private Vector theSchedule;
}
• 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...)
OOAD
Using the UML
- Appendix:
UML-to-Java
Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
15
Composition
class Schedule
{
public Schedule() { }
private Student theStudent;
}
Schedule
0..*
import java.util.Vector;
1
Student
class Student
{
public Student() { }
private Vector theSchedule = new Vector();
}
Java does not support containment by value.
theSchedule is a new class list created by Student…
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
16
This part is the
same as
aggregation…
Interfaces and Realizes Relationships
<<Interface>>
Serializable
interface Serializable {
}
<<entity>>
Schedule
class Schedule implements Serializable
{
}
• 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.”
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
17
Generalization
class GroundVehicle
{
public int licenseNumber;
public void register() { }
}
GroundVehicle
+licenseNumber: int
+register()
class Truck extends GroundVehicle
{
public float tonnage;
public void getTax() { }
}
Truck
+tonnage: float
+getTax()
• 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….
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
18
Multiple Inheritance
In Java, a class can only inherit from one
superclass. It can, however implement
multiple interfaces
<<Interface>>
IVehicle
{overlapping}
<<realize>>
Land
Vehicle
<<Interface>>
IWaterVehicle
<<realize>>
interface IWaterVehicle :
extends IVehicle
{
...
}
class AmphibiousVehicle
extends LandVehicle
implements WaterVehicle
{
...
}
Amphibious
Vehicle
• 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.
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
19
Multiple Inheritance (contd)
<<Interface>>
ILandVehicle
<<Interface>>
IWaterVehicle
LandVehicle
<<Interface>>
IAmphibiousVehicle
Java supports multiple inheritance
between interfaces.
In Java, an interface CAN inherit from
multiple interfaces.
Multiple inheritance of interfaces overcome
the ‘weakness’ of Java
An interface can inherit
from many interfaces.
regarding true multiple
inheritance.
In Java, a class can inherit
from one superclass.
It can, however implement
multiple interfaces.
AmphibiousVehicle
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
<<Interface>>
IHobby
20
Abstract Class
abstract class Animal
{
public abstract void talk();
}
Animal
{abstract}
+talk() {abstract}
Lion
Tiger
+talk()
+talk()
class Tiger extends Animal
{
public Tiger() { }
public void talk() { }
}
Remember, an abstract class is a class for which no instances are created.
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
21
Parameterized Class
Java does not support parameterized classes
T, n:int
Set
insert(T)
remove(T)
<<bind>>
<float, 100>
mySetOfFloats
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
22
Subsystems
<<subsystem>>
CourseCatalog
ICourseCatalog
getCourseOfferings() : CourseOfferingList
package CourseCatalog;
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.
public interface ICourseCatalog {
public CourseOfferingList
getCourseOfferings();
}
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
23
Subsystems
<<subsystem>>
CourseCatalog
ICourseCatalog
getCourseOfferings() : CourseOfferingList
package CourseCatalog;
public interface ICourseCatalog {
public CourseOfferingList
getCourseOfferings();
Note: CourseOfferingList is assumed
to exist in a separate common package.
The import statement has been
excluded from the code fragment.
}
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
These restrictions are what hinder
Java’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.
24
Subsystems and UML
<<subsystem>>
CourseCatalog
ICourseCatalog
getCourseOfferings() : CourseOfferingList
As we have discussed w/i OOAD course
subsystems are the Design Model
representation for components.
package CourseCatalog;
Some aspects of UML subsystems can be
implemented using Java packages
Java packages have a 1-1 correspondence
to UML packages.
public interface ICourseCatalog {
public CourseOfferingList
getCourseOfferings();
}
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
25
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.
OOAD Using the UML - Appendix: UML-to-Java Mapping, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved
26