Chapter 5 Implementing UML Specification Part I ObjectOriented
Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H. K. Tsang, Clarence S. W. Lau and Y. K. Leung Mc. Graw-Hill Education (Asia), 2005 Dr. Hussein Al-Zoubi 1
Objectives n After you have read this chapter, you should be able to n implement a class diagram; n implement a state diagram; n implement an activity diagram; and n implement sequence and collaboration diagrams. 2
Class A Single Class class Sample. Class { private int private. Attribute; protected double protected. Attribute; long package. Attribute; public boolean public. Method(int parameter 1) { … } private float private. Method(byte parameter 1, float parameter 2) { … } protected double protected. Method() { … } void package. Method(short parameter 1) { … } } Package Attribute: Only classes within the same package as the container can see and use the classes. 3
Inheritance n n n Single inheritance can be easily implemented by super class and subclass in most OO programming languages. Multiple inheritances may not be supported in some OO programming languages. Replace some of the inheritances by interfaces. 4
Inheritance class Sub. Class: Base. Class { … } 5
Interfaces n A class and an interface differ: A class can have an actual instance of its type, whereas an interface must have at least one class to implement it. In UML 2, an interface is considered to be a specialization of a class modeling element. Therefore, an interface is drawn just like a class, but the top compartment of the rectangle also has the text " «interface» ", as shown in Figure 10. [Note: When drawing a class diagram it is completely within UML specification to put «class» in the top compartment of the rectangle, as you would with «interface» ; however, the UML specification says that placing the "class" text in this compartment is optional, and it should be assumed if «class» is not displayed. ] 6
Interfaces Figure 10: Example of a class diagram in which the Professor and Student classes implement the Person interface 7
Interfaces n In the diagram shown in Figure 10, both the Professor and Student classes implement the Person interface and do not inherit from it. We know this for two reasons: 1) The Person object is defined as an interface — it has the " «interface» " text in the object's name area, and we see that the Professor and Student objects are class objects because they are labeled according to the rules for drawing a class object (there is no additional classification text in their name area). 2) We know inheritance is not being shown here, because the line with the arrow is dotted and not solid. As shown in Figure 10, a dotted line with a closed, unfilled arrow means realization (or implementation); as we saw in Figure 4, a solid arrow line with a closed, unfilled arrow means inheritance. 8
Inheritance (cont’d) Inheritance interface Base. Interface { // declaration of methods …. } class Concrete. Class : Base. Interface { // implementation of // methods of the // interface Base. Interface … } 9
Inheritance (cont’d) - Multiple inheritances may not be supported in some OO programming languages. - Replace some of the inheritances by interfaces. 10
Inheritance (cont’d) class Class 2: Class 1, Interface 3 { … define attributes from class 2 define operations from interface 3 } 11
One-to-one Association (cont’d) One-to-one Association class Class. A { Class. B _b; // declare attributes // for the // association class … } class Class. B { Class. A _a; … } 12
One-to-many Association (cont’d) One-to-many Association class Class. A { Vector _b; // or hashtable … } class Class. B { Class. A _a; // declare attributes // for association // class } 13
One-to-many Association (cont’d) n n Since the class on the one side has a vector to keep all the references of the associated objects, Methods for adding, removing, or searching objects in the vector are needed. 14
One-to-many Association (cont’d) class Class. A { Vector _Bs; public Class. A() { _Bs = new Vector(); … } public Enumeration get. Bs() { return(_Bs. elements()); } 15
One-to-many Association (cont’d) // remove the link between Class. B object to this // object public void remove. B(Class. B b) { _Bs. remove(b); } public void add. B(Class. B b) { _Bs. add(b); } // other functions for searching objects in the // vector … } 16
Qualified Association (cont’d) One-to-many Association class Class. A { Hashtable _b; … } class Class. B { Class. A _a; // declare attributes // for association // class … } 17
Qualified Association (cont’d) class Class. A { private Hashtable _Bs; public Class. A() { _Bs = new Hashtable(); } public Enumeration get. Bs() { return(_Bs. elements()); } public void add. B(Class. B b, int key) { _Class. Bs. put(new Key(key), b); } 18
Qualified Association (cont’d) public void remove. Class. B(Class. B b) { _Class. Bs. remove(b); } public Class. B get. Class. B(int key) { return((Class. B) _Bs. get(new Key(key))); } } // Class. A 19
Qualified Association (cont’d) class Key { int _key; public Class. B(int key) { _key = key; } public boolean equals(Object obj) { if (obj instanceof Key) return(((Key) obj). _key == _key); else return(false); } public int hash. Code() { return(_key); } }// Key 20
Many-to-many Association (cont’d) n Many-to-many Association n n If the association does not have additional attributes, we can use a vector or a hashtable on each side. If the association has attribute(s), a distinct association class is needed for holding the links between the objects and storing the additional attributes of the association. 21
Implementation of Association Class n n n One-to-one association. Assign the attributes of the association class to one of the classes of the association. One-to-many association. Assign the attributes of the association class to the class on the many side. Many-to-many association. Implement the association class as a distinct class. 22
Example – One-to-many Association 23
What is a VIN / Chassis? n A VIN is the 'Vehicle Identification Number' attached to any motor vehicle manufactured on are after 1 st January 1989. Important Information about a VIN includes: n n A VIN is always 17 characters long A VIN does not contain the letter "o" - it will always be the number zero that appears A VIN does not contact the letter 'i' - it will always be the number one that appears A Chassis number is the identification number attached to motor vehicles manufactured before 1 st January 1989. Important Information about a Chassis includes: n n A chassis does not have any specified length A chassis may not be a unique number 24
Example – One-to-many Association (cont’d) class Car { private int Engine. No; private int Chasis. No; private int Ownership. Year; private int Ownership. Month; private int Ownership. Day; … } 25
Example - Many-to-many Association Example of objects and links 26
Example – Many-to-many Association (cont’d) class School { private String _name; private Vector _registrations; public School(String name) { _name = name; _registrations = new Vector(); } public void set. Name(String name) { _name = name; } public String get. Name() { return(_name); } 27
Example – Many-to-many Association (cont’d) // school class continues public void add. Registration(Registration reg) { _registrations. add(reg); } public void remove. Registration(Registration reg) { _registrations. remove(reg); } public Enumeration get. Students() { int i; Vector students = new Vector(); for (i = 0; i < _registrations. size(); i++) students. add(((Registration) _registrations. element. At(i)). get. Student()); return(students. elements()); } } // school 28
Example – Many-to-many Association (cont’d) class Person { private String _name; private Vector _registrations; public Person(String name) { _name = name; _registrations = new Vector(); } String get. Name() { return(_name); } void set. Name(String name) { _name = name; } 29
Example – Many-to-many Association (cont’d) // Class Person continues public void add. Registration(Registration reg) { _registrations. add(reg); } public void remove. Registration(Registration reg) { _registrations. remove(reg); } public Enumeration get. Schools() { int i; Vector schools = new Vector(); for (i = 0; i < _registrations. size(); i++) schools. add(((Registration) _registrations. element. At(i)). get. School()); return(schools. elements()); } } // Person 30
Example – Many-to-many Association (cont’d) class Registration { private Person _student; private School _school; private int _student. No; private Registration(Person student, School school, int student. No) { _school = school; _student = student; _student. No = student. No; } static public void register(Person student, School school, int student. No) { Registration reg = new Registration(student, school, student. No); school. add. Registration(reg); student. add. Registration(reg); } 31
Example – Many-to-many Association (cont’d) // Class Registration continues public void deregister() { this. _school. remove. Registration(this); this. _student. remove. Registration(this); } public School get. School() { return(_school); } public Person get. Student() { return(_student); } } // class Registration 32
Example – Many-to-many Association (cont’d) public class Main 3 { public static void main(String argv[]) { int i; String school. Names[] = {"TWGS", "KCTS", "LKP", "CMT", "KKY"}; String student. Names[] = {"Peter Chan", "Alan Tong", "John Lee", "Venice Tsui", "Mary Lui"}; Person students[] = new Person[5]; School schools[] = new School[5]; for (i = 0; i < 5; i++) { students[i] = new Person(student. Names[i]); schools[i] = new School(school. Names[i]); } 33
Example – Many-to-many Association (cont’d) Registration. register(students[0], schools[0], 1241); Registration. register(students[1], schools[1], 1234); Registration. register(students[2], schools[1], 1111); Registration. register(students[3], schools[2], 9878); Registration. register(students[4], schools[3], 6782); Registration. register(students[4], schools[4], 9807); Registration. register(students[4], schools[0], 9080); Enumeration s = Registration. get. Schools("Mary Lui"); System. out. println("Mary Lui studies in the following schools: "); for (; s. has. More. Elements(); ) { System. out. println (((School) s. next. Element()). get. Name()); } } } 34
Composition & Aggregation n Aggregation can be implemented as a plain association. n Composition is a special case of aggregation. The parts of the whole object is deleted before the whole object is deleted. 35
Persistent Classes n n n Persistent objects have long life spans and need to be stored in permanent storage media, such as hard disk. Usually a database system is used to store the persistent objects. Two choices of database technologies: OO database vs Relational database is more mature and commonly used technology. Need to map the classes into database tables if relational database is used. 36
A single class n n A class is mapped onto one or more database tables Attributes of class are mapped onto attributes of the created table 37
One-to-many Association n A one-to-many association can be implemented in the following two ways: n Embedding the attributes of the association in the table for the class on the many side. The table for the many side contains a key of the table of the one side n n This method is faster Creating a separate table for the association class n This method is more general 38
Many-to-many association n A many-many association can be implemented by creating a separate table for the association The association class is mapped onto a table In addition to the association class attributes, the association table should have two additional attributes (keys) for linking the other two class n These keys also form the key of the association table 39
Qualified many-to-many association n n A qualified many-to-many association can be implemented by creating a separate table The table contains the primary keys of the associated classes (for linking them) and the qualifier 40
N-ary associations n An N-ary association can be implemented as a separate table with the primary keys of the associated classes as attributes 41
Generalization n There are several methods for mapping a generalization hierarchy of classes: n n n Mapping one class to one table Replicating attributes in subclasses Replicating attributes of all subclasses in the root superclass 42
- Slides: 42