1 / 26

Object Oriented Analysis and Design Using the UML

Object Oriented Analysis and Design Using the UML. UML-to-Java Mapping. Java Features. The slides in this presentation include some suggestions for UML to Java mappings. Most of this class knows Java – but not all.

arowell
Download Presentation

Object Oriented Analysis and Design Using the UML

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Analysis and DesignUsing the UML UML-to-Java Mapping

  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.

  3. Course 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() { } protected void finalize() throws Throwable { super.finalize(); } }; 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…

  4. Student - name : String + addSchedule (theSchedule: Schedule, forSemester: Semester)+ hasPrerequisites(forCourseOffering: CourseOffering) : boolean# passed(theCourseOffering: CourseOffering) : boolean 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 • 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.

  5. class Student { private static int nextAvailID = 1; public static int getNextAvaiIID() { } } Student - nextAvailID : int = 1 + getNextAvaiIID() : int Class Scope Attributes and Operations 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, thestatic modifier associates a variable or method with its class rather than with an object of the class.

  6. 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) 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

  7. Nested Class • Hide a class that is relevant only for implementation class Outer { public outer() { } class Inner { public Inner() { } } } Outer 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.)

  8. // no need to import if in same package class Schedule { public Schedule() { } //constructor private Student theStudent; } Schedule class Student { public Student() { } private Schedule theSchedule; } Student 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.

  9. Schedule Student Association Navigability • Uni-directional associations • Student knows about schedule; Student is a client of Schedule. • Can Create objects of other class. class Schedule { public Schedule() { } } class Student { public Student() { } private Schedule theSchedule; }

  10. Professor Association Roles class Professor { public Professor() {} private CourseOffering theCourseOffering; } instructor class CourseOffering { public CourseOffering() {} private Professor instructor; } CourseOffering • 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.

  11. Association Multiplicity class CourseOffering { public CourseOffering() {} } CourseOffering 0..4 primaryCourses class Schedule { public Schedule() {} private CourseOffering[] primaryCourses = new CourseOffering[4] } Schedule • 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.

  12. alternateCourses 0..* 0..2 primaryCourses 0..4 0..* PrimaryScheduleOfferingInfo CourseOffering CourseOffering Schedule Schedule - grade: char = I Design Decisions PrimaryScheduleOfferingInfo - grade: char = I Association Class // 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; } • 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). alternateCourses 0..* 0..2 primaryCourseOfferingInfo 1 1 0..4 0..*

  13. alternateCourses 0..* 0..2 primaryCourses 0..4 0..* PrimaryScheduleOfferingInfo CourseOffering CourseOffering Schedule Schedule - grade: char = I Design Decisions PrimaryScheduleOfferingInfo - grade: char = I Association Class // 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; } • 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. alternateCourses 0..* 0..2 primaryCourseOfferingInfo 1 1 0..4 0..*

  14. Reflexive Associations prerequisites import java.util.Vector; class Course { public Course() {} // The unbounded multiple association // is stored in a vector private Vector prerequisites; } 0..* Course • 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.

  15. Schedule Student Aggregation class Schedule { public Schedule() { } private Student theStudent; } 0..* import java.util.Vector; class Student { public Student() { } private Vector theSchedule; } 1 • 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...)

  16. Schedule Student Composition class Schedule { public Schedule() { } private Student theStudent; } This part is the same as aggregation… 0..* import java.util.Vector; class Student { public Student() { } private Vector theSchedule = new Vector(); } 1 Java does not support containment by value. theSchedule is a new class list created by Student…

  17. <<Interface>> Serializable <<entity>> Schedule Interfaces and Realizes Relationships interface Serializable { } 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.”

  18. 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….

  19. Multiple Inheritance In Java, a class can only inherit from one superclass. It can, however implement multiple interfaces <<Interface>> IVehicle {overlapping} interface IWaterVehicle : extends IVehicle { . . . } <<realize>> Land Vehicle <<Interface>> IWaterVehicle <<realize>> 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.

  20. Multiple Inheritance (contd) 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. <<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

  21. Abstract Class abstract class Animal { public abstract void talk(); } Animal {abstract} +talk(){abstract} class Tiger extends Animal { public Tiger() { } public void talk() { } } Tiger +talk() Lion +talk() Remember, an abstract class is a class for which no instances are created.

  22. Parameterized Class Java does not support parameterized classes T, n:int Set insert(T) remove(T) <<bind>> <float, 100> mySetOfFloats

  23. <<subsystem>> CourseCatalog ICourseCatalog getCourseOfferings() : CourseOfferingList Subsystems package CourseCatalog; public interface ICourseCatalog { public CourseOfferingList getCourseOfferings(); } 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.

  24. <<subsystem>> CourseCatalog ICourseCatalog getCourseOfferings() : CourseOfferingList Subsystems 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. Note: CourseOfferingList is assumed to exist in a separate common package. The import statement has been excluded from the code fragment. package CourseCatalog; public interface ICourseCatalog { public CourseOfferingList getCourseOfferings(); }

  25. <<subsystem>> CourseCatalog ICourseCatalog getCourseOfferings() : CourseOfferingList Subsystems and UML As we have discussed w/i OOAD course subsystems are the Design Model representation for components. Some aspects of UML subsystems can be implemented using Java packages Java packages have a 1-1 correspondence to UML packages. package CourseCatalog; public interface ICourseCatalog { public CourseOfferingList getCourseOfferings(); }

  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.

More Related