ObjectOriented Software Development Class Diagrams and Its Implementation

Object-Oriented Software Development: Class Diagrams and Its Implementation Chih-Ping Chu Dept. of Computer Science & Information Engineering National Cheng Kung University

Outline Features of object-oriented software Developing OO systems based on models UML diagrams Essentials of class diagrams Implementing class diagrams

Models are used for: - leading software engineers to have insights about the system - providing abstraction - helping create designs - permitting analysis and review of those designs. (check design) - being the core documentation describing the system.

UML diagrams Class diagrams - describe classes and their relationships Interaction diagrams - show the behaviour of systems in terms of how objects interact with each other State diagrams and activity diagrams - show systems behave internally Component and deployment diagrams - show the various components of systems are arranged logically and physically

Class diagrams

Interaction diagrams

State diagrams and activity diagrams


Component and deployment diagrams

Essentials of UML Class Diagrams The main symbols shown on class diagrams are: Classes - represent the types of data themselves Associations - represent linkages between instances of classes Attributes - are simple data found in classes and their instances Operations - represent the functions performed by the classes and their instances


Implementing Class Diagrams in Java - Attributes are implemented as instance variables - Generalizations are implemented using extends - Interfaces are implemented using implements - Associations are normally implemented using instance variables - Divide each two-way association into two one way associations so each associated class has an instance variable.

- For a one-way association where the multiplicity at the other end is ‘one’ or ‘optional’ - declare a variable of that class (a reference) - For a one-way association where the multiplicity at the other end is ‘many’: - use a collection class implementing List, such as Vector

An example: A Flight Booking System


An example: A Flight Booking System

Creating an association class, given two existing objects class Passenger { private Array. List<Booking> bookings = new Array. List<Booking>(0); private String name; public void make. Booking(Specific. Flight flight, int seat. Number) { new Booking(this, flight, seat. Number); } void add. Link. To. Booking(Booking book. One) // link booking {

class Booking //association class { Passenger passenger; // due to one relation Specific. Flight flight; //due to one relation int seat. Number; Booking(Passenger passenger, Specific. Flight flight, int seat. Number) { this. passenger = passenger; this. passenger. add. Link. To. Booking(this); this. flight = flight; this. flight. add. Link. To. Booking(this); this. seat. Number = seat. Number; }

class Specific. Flight { private Array. List<Booking> booking = new Array. List<Booking>(0); private String name; void add. Link. To. Booking(Booking book. One) { booking. add(book. One); }


Making a bi-directional link between two existing objects; class Specific. Flight { private Calendar date; private Regular. Flight regular. Flight; . . . // Constructor that should only be called from // add. Specific. Flight( Calendar a. Date, Regular. Flight a. Regular. Flight) { date = a. Date; regular. Flight = a. Regular. Flight; } }

class Regular. Flight { private List specific. Flights; . . . // Method that has primary responsibility public void add. Specific. Flight(Calendar a. Date) { Specific. Flight new. Specific. Flight; new. Specific. Flight = new Specific. Flight(a. Date, this); specific. Flights. add(new. Specific. Flight); }. . . }


Creating an object and linking it to an existing object class Specific. Flight { private Flight. Log log; private Regular. Flight regular. Flight; . . . public void create. Flight. Log () { log = new Flight. Log(); } }

class Flight. Log {. . . Flight. Log () // constructor { } }

Searching for an associated instance import java. util. Iterator; class Specific. Flight { private Array. List<Employee. Role> employees = new Array. List<Employee. Role>(0); private Employee. Role an. Employee; . . . public Employee. Role find. Crew. Member (String name) { for (Iterator<Employee. Role> iterator = employees. iterator(); iterator. has. Next(); ) { an. Employee = (Employee. Role) iterator. next(); if an. Employee, getname() == name return an. Employee; } }

class Employee. Role { private String name; . . . public String get. Name() { return name; } () }
- Slides: 27