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 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) { … } } 3

Package package com. abc. library; class Class. A …. { … } 4

Inheritance n n n Single inheritance can be easily implemented by super class and subclass in most OO programming languages, e. g. use extends in Java. Multiple inheritances may not be supported in some OO programming languages. Replace some of the inheritances by interfaces. 5

Inheritance class Sub. Class extends Base. Class { … } 6

Inheritance (cont’d) Inheritance interface Base. Interface { // declaration of methods …. } class Concrete. Class implements Base. Interface { // implementation of // methods of the // interface Base. Interface … } 7

Inheritance (cont’d) 8

Inheritance (cont’d) class Class 2 extends Class 1 implements Interface 3 { … define attributes from class 3 define operations from interface 3 } 9

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; … } 10

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 } 11

One-to-many Association (cont’d) import java. util. Vector; class Class. A { Vector _Bs; public Class. A() { _Bs = new Vector(); … } public Enumeration get. Bs() { return(_Bs. elements()); } 12

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 … } 13

Qualified Association (cont’d) One-to-many Association class Class. A { Hashtable _b; … } class Class. B { Class. A _a; // declare attributes // for association // class … } 14

Qualified Association (cont’d) import java. util. Hashtable; 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); } 15

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 16

Qualified Association (cont’d) class Key { int _key; public Key(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 17

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

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. 19

Example – One-to-many Association 20

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; … } 21

Example - Many-to-many Association Example of objects and links 22

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); } 23

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 24

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; } 25

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 26

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); } 27

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 28

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]); } 29
![Example – Many-to-many Association (cont’d) Registration. register(students[0], schools[0], 1241); Registration. register(students[1], schools[1], 1234); Registration. Example – Many-to-many Association (cont’d) Registration. register(students[0], schools[0], 1241); Registration. register(students[1], schools[1], 1234); Registration.](http://slidetodoc.com/presentation_image_h2/0f2a765f653deb3390e5fb7046bf99a8/image-30.jpg)
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()); } } } 30

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. 31
- Slides: 31