1 / 53

Chapter 5 Implementing UML Specification (Part I)

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. Objectives. After you have read this chapter, you should be able to

Download Presentation

Chapter 5 Implementing UML Specification (Part I)

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. Chapter 5Implementing 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

  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.

  3. Class • class 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

  4. Package Package package com.abc.library; class ClassA …. { … } 

  5. Inheritance • 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.

  6. Inheritance Inheritance class SubClass extends BaseClass { … } 

  7. Inheritance (cont’d) Inheritance • interface BaseInterface { • // declaration of methods • …. • } • class ConcreteClass implements BaseInterface • { • // implementation of • // methods of the • // interface BaseInterface • … • } 

  8. Inheritance (cont’d)

  9. Inheritance (cont’d) • class Class2 extends Class1 implements Interface3 { • … • define attributes from class3 • define operations from interface3 • }

  10. One-to-one Association (cont’d) One-to-one Association • class ClassA { • ClassB _b; • // declare attributes • // for the • // association class • … • } • class ClassB { • ClassA _a; • … • } 

  11. One-to-many Association (cont’d) One-to-many Association • class ClassA { • Vector _b; // or hashtable • … • } • class ClassB { • ClassA _a; • // declare attributes • // for association • // class • } 

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

  13. One-to-many Association (cont’d) • // remove the link between ClassB object to this • // object • public void removeB(ClassB b) { • _Bs.remove(b); • } • public void addB(ClassB b) { • _Bs.add(b); • } • // other functions for searching objects in the • // vector • … • }

  14. Qualified Association (cont’d) One-to-many Association • class ClassA { • Hashtable _b; • … • } • class ClassB { • ClassA _a; • // declare attributes • // for association • // class • … • } 

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

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

  17. 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); • else • return(false); • } • public int hashCode() { • return(_key); • } • }// Key

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

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

  20. Example – One-to-many Association

  21. Example – One-to-many Association (cont’d) class Car { private int EngineNo; private int ChasisNo; private int OwnershipYear; private int OwnershipMonth; private int OwnershipDay; … }

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

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

  24. Example – Many-to-many Association (cont’d) // school class continues public 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

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

  26. Example – Many-to-many Association (cont’d) // Class Person continues public 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

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

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

  29. 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]); }

  30. 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()); } } }

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

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

  33. Mapping Classes to Tables

  34. Mapping Classes to Tables (cont’d) CREATE TABLE Student ( Student_number char(30) not null, name char(30) not null, address char(30), PRIMARY KEY (student_number)); CREATE INDEX student_index_name ON Student (name);

  35. Mapping Many-to-many Associations • Create a separate table for the association.

  36. Mapping Many-to-many Associations (cont’d)

  37. Mapping Many-to-many Associations (cont’d) CREATE TABLE Registration ( Person_ID char(30) not null, School_ID char(30) not null, Student_number char(30), PRIMARY KEY (Person_ID, School_ID), FOREIGN KEY (Person_ID) REFERENCES Person(Person_ID), FOREIGN KEY (School_ID) REFERENCES School(School_ID)); CREATE INDEX Registration_index_person ON Registration (Person_ID); CREATE INDEX Registration_index_school ON Registration (School_ID);

  38. Mapping One-to-many Associations • Two choices: • Create a separate table for the association, or • Embed the attributes of the association in the class on the many side.

  39. Mapping One-to-many Associations (cont’d) Merging person and association class:

  40. Mapping One-to-many Associations (cont’d) CREATE TABLE Person ( Person_ID char(30) not null, School_ID char(30) not null, Person_name char(30) not null, Student_number char(30), PRIMARY KEY (person_ID), FOREIGN KEY (school_ID) REFERENCES School(School_ID)); CREATE INDEX Registration_index_person ON Person (person_name); CREATE INDEX Registration_index_school ON Person (school_ID);

  41. Mapping Qualified Many-to-many Associations • Need a separate table to implement the association. • The qualifier is an attribute of the table.

  42. Qualified Many-to-many Associations (cont’d)

  43. Mapping N-ary Associations Map the association into a table.

  44. Mapping N-ary Associations (cont’d)

  45. Mapping Generalizations • There are several methods to map a generalization hierarchy of classes. • One table for each class. Slow access. Fully extensible. • Eliminate all superclasses and replicate the attributes in superclasses in subclasses. Map the subclasses into tables. • Replicate attributes of all subclasses in the root superclass. Map the superclass into a table.

  46. Example – One Table for Each Class

  47. Example – One Table for Each Class (cont’d)

  48. Example – One Table for Each Class (cont’d) A teacher object is stored in two records, one record in person table and the other in the teacher table.

  49. Example – One Table for Each Class (cont’d) Create view TeacherView as select Person.*, Teacher.*, from Person, Teacher, where (Person.person-ID = Teacher.person-ID); Create view StudentView as select Person.*, Student.*, from Person, Student, where (Person.person-ID = Student.person-ID);

  50. Example – Replicating Attributes in Subclasses

More Related