42
1 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 McGraw-Hill Education (Asia), 2005 Dr. Hussein Al-Zoubi

1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

Embed Size (px)

Citation preview

Page 1: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

1

Chapter 5Implementing UML

Specification (Part I)

Object-Oriented TechnologyFrom Diagram to Code with Visual Paradigm for

UML

Curtis H.K. Tsang, Clarence S.W. Lau and Y.K. Leung

McGraw-Hill Education (Asia), 2005Dr. Hussein Al-Zoubi

Page 2: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

2

Objectives After you have read this chapter,

you should be able to implement a class diagram; implement a state diagram; implement an activity diagram;

and implement sequence and

collaboration diagrams.

Page 3: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

3

Classclass SampleClass {

private int privateAttribute;protected double protectedAttribute;long packageAttribute;public boolean publicAttribute;public boolean publicMethod(int parameter1) {

…}private float privateMethod(byte parameter1,float parameter2) {

…}protected double protectedMethod() {

… }

void packageMethod(short parameter1) {…

}}

A Single Class

Package Attribute: Only classes within the same package as the container can see and use the classes.

Page 4: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

4

Inheritance

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.

Page 5: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

5

Inheritance

class SubClass:BaseClass {…}

Inheritance

Page 6: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

6

Interfaces 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.]

Page 7: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

7

Interfaces

Figure 10: Example of a class diagram in which the Professor and Student classes implement the Person interface

Page 8: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

8

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

Page 9: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

9

Inheritance (cont’d)interface BaseInterface {

// declaration of methods….}class ConcreteClass : BaseInterface {// implementation of // methods of the // interface BaseInterface…}

Inheritance

Page 10: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

10

Inheritance (cont’d)

- Multiple inheritances may not be supported in some OO programming languages.- Replace some of the inheritances by interfaces.

Page 11: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

11

class Class2: Class1, Interface3 {

define attributes from class2

define operations from interface3

}

Inheritance (cont’d)

Page 12: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

12

One-to-one Association (cont’d)

class ClassA {ClassB _b;// declare attributes // for the // association class…}class ClassB {ClassA _a;…

}

One-to-one Association

Page 13: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

13

One-to-many Association (cont’d)

class ClassA {Vector _b; // or hashtable…}class ClassB {ClassA _a;// declare attributes // for association // class}

One-to-many Association

Page 14: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

14

One-to-many Association (cont’d) 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.

Page 15: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

15

One-to-many Association (cont’d)

class ClassA {Vector _Bs;

public ClassA() {_Bs = new Vector();…

}public Enumeration getBs() {

return(_Bs.elements());}

Page 16: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

16

One-to-many Association (cont’d)

// remove the link between ClassB object to this// objectpublic void removeB(ClassB b) {

_Bs.remove(b);}public void addB(ClassB b) {

_Bs.add(b);}// other functions for searching objects in the// vector…}

Page 17: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

17

Qualified Association (cont’d)class ClassA {

Hashtable _b;…}class ClassB {ClassA _a;// declare attributes // for association // class…}

One-to-many Association

Page 18: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

18

Qualified Association (cont’d)class ClassA {

private Hashtable _Bs;public ClassA() {

_Bs = new Hashtable();}public Enumeration getBs() {

return(_Bs.elements());}public void addB(ClassB b, int key) {

_ClassBs.put(new Key(key), b);}

Page 19: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

19

Qualified Association (cont’d)public void removeClassB(ClassB b) {

_ClassBs.remove(b);}public ClassB getClassB(int key) {

return((ClassB) _Bs.get(new Key(key)));}} // ClassA

Page 20: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

20

Qualified Association (cont’d)class Key {

int _key;public ClassB(int key) {

_key = key;}public boolean equals(Object obj) {

if (obj instanceof Key)return(((Key) obj)._key == _key);

elsereturn(false);

}public int hashCode() {

return(_key);}

}// Key

Page 21: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

21

Many-to-many Association (cont’d) Many-to-many Association

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.

Page 22: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

22

Implementation of Association Class

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.

Page 23: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

23

Example – One-to-many Association

Page 24: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

24

What is a VIN / Chassis? A VIN is the 'Vehicle Identification Number' attached to

any motor vehicle manufactured on are after 1st January 1989. Important Information about a VIN includes:

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 1st January 1989. Important Information about a Chassis includes:

A chassis does not have any specified length A chassis may not be a unique number

Page 25: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

25

Example – One-to-many Association (cont’d)

class Car {private int EngineNo;

private int ChasisNo;

private int OwnershipYear;

private int OwnershipMonth;

private int OwnershipDay;

}

Page 26: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

26

Example - Many-to-many Association

Registration

9080

9807

6782

9878

1111

1234

1242

9080

9807

6782

9878

1111

1234

1242Peter Chan

Alan Tong

John Lee

Venice Tsui

Mary Lui

TWGS

KCTS

LKP

CMT

KKY

Example of objects and links

Many-to-many Association

Page 27: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

27

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 setName(String name) { _name = name; }

public String getName() { return(_name); }

Page 28: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

28

Example – Many-to-many Association (cont’d)// school class continuespublic void addRegistration(Registration reg) { _registrations.add(reg); }

public void removeRegistration(Registration reg) { _registrations.remove(reg); }

public Enumeration getStudents() { int i; Vector students = new Vector();

for (i = 0; i < _registrations.size(); i++) students.add(((Registration) _registrations.elementAt(i)).getStudent()); return(students.elements()); }} // school

Page 29: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

29

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 getName() { return(_name); }

void setName(String name) { _name = name; }

Page 30: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

30

Example – Many-to-many Association (cont’d)// Class Person continuespublic void addRegistration(Registration reg) { _registrations.add(reg); }

public void removeRegistration(Registration reg) { _registrations.remove(reg); } public Enumeration getSchools() { int i; Vector schools = new Vector(); for (i = 0; i < _registrations.size(); i++) schools.add(((Registration) _registrations.elementAt(i)).getSchool()); return(schools.elements()); }} // Person

Page 31: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

31

Example – Many-to-many Association (cont’d)class Registration { private Person _student; private School _school; private int _studentNo;

private Registration(Person student, School school, int studentNo) { _school = school; _student = student; _studentNo = studentNo; }

static public void register(Person student, School school, int studentNo) { Registration reg = new Registration(student, school, studentNo); school.addRegistration(reg); student.addRegistration(reg); }

Page 32: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

32

Example – Many-to-many Association (cont’d)// Class Registration continues public void deregister() { this._school.removeRegistration(this); this._student.removeRegistration(this); }

public School getSchool() { return(_school); }

public Person getStudent() { return(_student); }

} // class Registration

Page 33: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

33

Example – Many-to-many Association (cont’d)

public class Main3 {

public static void main(String argv[]) {

int i;

String schoolNames[] = {"TWGS", "KCTS", "LKP", "CMT", "KKY"};

String studentNames[] = {"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(studentNames[i]);

schools[i] = new School(schoolNames[i]);

}

Page 34: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

34

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.getSchools("Mary Lui"); System.out.println("Mary Lui studies in the following schools:"); for (;s.hasMoreElements();) { System.out.println (((School) s.nextElement()).getName()); }

}}

Page 35: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

35

Composition & Aggregation

Aggregation can be implemented as a plain association.

Composition is a special case of aggregation. The parts of the whole object is deleted before the whole object is deleted.

Page 36: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

36

Persistent Classes 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.

Relational database is more mature and commonly used technology.

Need to map the classes into database tables if relational database is used.

Page 37: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

37

A single class A class is mapped onto one or

more database tables Attributes of class are mapped

onto attributes of the created table

Page 38: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

38

One-to-many Association A one-to-many association can be

implemented in the following two ways: 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

This method is faster Creating a separate table for the association

class This method is more general

Page 39: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

39

Many-to-many association

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 These keys also form the key of the

association table

Page 40: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

40

Qualified many-to-many association 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

Page 41: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

41

N-ary associations An N-ary association can be

implemented as a separate table with the primary keys of the associated classes as attributes

Page 42: 1 Chapter 5 Implementing UML Specification (Part I) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence

42

Generalization There are several methods for

mapping a generalization hierarchy of classes: Mapping one class to one table Replicating attributes in subclasses Replicating attributes of all subclasses

in the root superclass