29
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 5: Modelling with Classes (continued)

Chapter 5: Modelling with Classes (continued)

  • Upload
    finola

  • View
    48

  • Download
    0

Embed Size (px)

DESCRIPTION

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 5: Modelling with Classes (continued). 5.8 The Class Diagram Design Process. You can create UML models at different stages and with different purposes and levels of detail - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 5:  Modelling with Classes (continued)

Object-Oriented Software EngineeringPractical Software Development using UML and Java

Chapter 5:

Modelling with Classes

(continued)

Page 2: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 2

5.8 The Class Diagram Design Process

You can create UML models at different stages and with different purposes and levels of detail

• Exploratory domain model:

—Helps to learn about the domain

E.g., Registration

• System domain model:

—Shows aspects of the domain covered by the system

E.g., Student, TimeSlot, Instructor, CourseOffering

• System model:

—Includes classes for the user interface and system architecture

E.g., course and transcript databases, client-server classes

Page 3: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 3

System domain model vs System model

The system domain model omits many classes that are needed to build a complete system

—Can contain less than half the classes of the system

—Should be developed independently of particular- user interface classes

- architectural classes (client-server, database, etc.)

The complete system model includes

—The system domain model

—User interface classes

—Architectural classes

—Utility classes

Page 4: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 4

Sequencing of Design Activities

1. Identify a first set of candidate classes

2. Add associations and attributes

3. Find generalizations

4. List the main responsibilities of each class

5. Decide on specific operations

6. Iterate over this process until the model is satisfactory

— Add or delete classes, associations, attributes, generalizations, responsibilities or operations

— Identify interfaces

— Apply design patterns (Chapter 6)

Don’t be too disorganized. Don’t be too rigid either.

Page 5: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 5

Identifying classes

1. When developing a domain model you tend to discover classes (i.e., they already exist)

2. When you work on the user interface or the system architecture, you tend to invent classes

— Needed to solve a particular design problem

— (Inventing also occurs in domain modeling)

3. Reuse should always be a concern

— Frameworks

— System extensions

— Similar systems

Page 6: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 6

Tips on discovering domain classes

1. Look at a source material such as a description of requirements

2. Extract the nouns and noun phrases

3. Eliminate nouns that:

— are redundant

— represent instances

— are vague or highly general

— not needed in the application

4. Pay attention to classes that represent types of users or other actors

Page 7: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 7

Identifying associations and attributes

1. Start with classes you think are most central2. Decide on the obvious data each class must contain and its

relationships to other classes. 3. Work outwards toward classes that are less important.4. Avoid adding too many associations/attributes to a class (A

system is simpler if it has less information)5. An association exists if a class

- possesses / controls- is connected to / is related to- is a part of / has as parts- is a member of / has as members

another class in the model6. Specify multiplicity at both ends7. Label the association clearly

Page 8: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 8

Actions versus associations

A common mistake is to represent actions as if they were associations

Bad: associations are not actions

*

LibraryPatron

borrow

****

*

return

CollectionItem

*

*

Loan

borrowedDatedueDatereturnedDate

LibraryPatron

CollectionItem

*

*

Better: Borrow is replaced by a Loan and aborrowedDate, and return sets the returnedDate.

Page 9: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 9

Identifying attributes

• Look for information that must be maintained about each class

• Several nouns rejected as classes, may now become attributes

• An attribute should usually contain a simple value

—E.g. string, number, date

Page 10: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 10

Identifying and specifying valid attributes

• Avoid having many duplicate attributes

• If a group of attributes is coherent, create a distinct class with these attributes. E.g.,

****

*

Person

nameaddresses

addressesPerson

namestreet1municipality1provOrState1country1postalCode1street2municipality2provOrState2country2postalCode2

Person

name

Address

streetmunicipalityprovOrStatecountrypostalcodetype

Bad due toa pluralattribute

Bad due to too manyattributes, and inabilityto add more addresses

Good solution. Thetype indicates whetherit is a home address,business address etc.

Page 11: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 11

Example: attributes and associations

*

supervisor

RegularFlight

timeflightNumber

*

******

Passenger

******

******

******

SpecificFlight

date

nameemployeeNumber

Employee

jobFunction

Booking

seatNumber

namenumber

Page 12: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 12

Identifying generalizations and interfaces

Two ways to identify generalizations:

—bottom-up: group similar classes to create a new superclass

—top-down: find general classes first; specialize them if needed

Create an interface instead of a superclass, if:

—The classes have only a few operations in common

—The classes already have their own superclasses

—Different implementations of the same class might be available

Page 13: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 13

An example (generalization)

*

supervisor

RegularFlight

timeflightNumber

*

******

PassengerRole

******

******

******

SpecificFlight

date

Person

nameidNumber

0..20..20..20..20..20..2

EmployeeRole

jobFunction

Booking

seatNumber

PersonRole

Page 14: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 14

Assigning responsibilities to classes

A responsibility is something that the system is required to do. • Every functional requirement must be assigned to a class

—All of a class’s responsibilities should be clearly related.—A class with too many responsibilities can be split into two

classes—A class with no responsibilities is probably useless —When a responsibility cannot be attributed to any of the

existing classes, then a new class should be created —A responsibility usually denotes a public method or

constructor.

• To find responsibilities:—Read the use cases, and —Look for verbs and nouns describing actions in the system

description

Page 15: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 15

Kinds of responsibilities for classes

• Setting and getting the values of attributes

• Creating and initializing new instances (constructors)

• Loading to and saving from persistent storage (file i/o)

• Destroying instances - destructors (automatic in Java)

• Adding and deleting links of associations

• Copying, converting, transforming, transmitting or outputting

• Computing numerical results

• Navigating and searching

• Other specialized work

Page 16: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 16

An example (responsibilities)

—Creating a new regular flight

—Searching for a flight

—Modifying attributes of a flight

—Creating a specific flight

—Booking a passenger

—Canceling a booking

*

supervisor

RegularFlight

timeflightNumber

*

******

PassengerRole

******

******

******

SpecificFlight

date

******

******Person

nameidNumber

0..20..20..20..20..20..2

EmployeeRole

jobFunction

Booking

seatNumber

PersonRoleAirline

Page 17: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 17

Prototyping a class diagram in Eclipse

1. Open a new .ucd file in a package of a project.

2. As you identify classes, create and name them in the workspace

3. As you identify attributes and responsibilities, add them to the classes where they belong.

— If you cannot fit all the responsibilities into one class, consider creating two related classes.

4. Move the classes around on the workspace to arrange them into a class diagram.

5. Draw lines between the classes to capture associations and generalizations.

Page 18: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 18

Identifying operations (methods)

Operations realize the responsibilities of each class

• There may be several operations per responsibility

• The main methods that implement a responsibility are normally the public ones

• Other methods that collaborate to perform the responsibility must be as private as possible

Page 19: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 19

An example (modeling class collaborations)

Airplane

addLinkToSpecificFlight [a2, d3]deleteLinkToSpecificFlight [d2]

SpecificFlight

+ specifyAirplane [a1]+ createFlightLog [b1]

+ makeBooking [c1]

+ changeAirplane [d1]+ findCrewMember [e1]

EmployeeRole

+ getName [e2]

FlightLog

FlightLog [b2]

Booking

Booking [c2]

PassengerRole

addLinkToBooking [c4]

*

******

0..1

*

*

******

crewMember

0..1

addLinkToBooking [c3]

Page 20: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 20

Class collaboration A

Making a bi-directional link between two existing objects; e.g. adding a link between an instance of

SpecificFlight and an instance of Airplane. 1. (public) The instance of SpecificFlight

— makes a one-directional link to the instance of Airplane

— then calls operation 2.2. (non-public) The instance of Airplane

— makes a one-directional link back to the instance of SpecificFlight

Airplane

addLinkToSpecificFlight [a2, d3]

SpecificFlight

+ specifyAirplane [a1]

* 0..1

Page 21: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 21

Class collaboration B

Creating an object and linking it to an existing objecte.g. creating a FlightLog, and linking it to a

SpecificFlight. 1. (public) The instance of SpecificFlight

—calls the constructor of FlightLog (operation 2)

—then makes a one-directional link to the new instance of FlightLog.

2. (non-public) Class FlightLog’s constructor—makes a one-directional link back to the

instance of SpecificFlight.

SpecificFlight

+ createFlightLog [b1]

FlightLog

FlightLog [b2]

0..10..10..10..10..10..1

Page 22: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 22

Class collaboration C

Creating an association class, given two existing objectse.g. creating an instance of Booking, which will link a

SpecificFlight to a PassengerRole.1. (public) The instance of PassengerRole

— calls the constructor of Booking (operation 2).2. (non-public) Class Booking’s constructor, among its other actions

— makes a one-directional link back to the instance of PassengerRole

— makes a one-directional link to the instance of SpecificFlight— calls operations 3 and 4.

3. (non-public) The instance of SpecificFlight— makes a one-directional link to the instance of Booking.

4. (non-public) The instance of PassengerRole— makes a one-directional link to the instance of Booking.

Page 23: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 23

Class collaboration D

Changing the destination of a linke.g. changing the Airplane of to a SpecificFlight, from

airplane1 to airplane2 1. (public) The instance of SpecificFlight

—deletes the link to airplane1—makes a one-directional link to airplane2—calls operation 2—then calls operation 3.

2. (non-public) airplane1—deletes its one-directional link to the instance of SpecificFlight.

3. (non-public) airplane2—makes a one-directional link to the instance of SpecificFlight.

Airplane

addLinkToSpecificFlight [a2, d3]deleteLinkToSpecificFlight [d2]

SpecificFlight

+ changeAirplane [d1]

* 0..1

Page 24: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 24

Class collaboration E

Searching for an associated instance

e.g. searching for a crew member associated with a SpecificFlight that has a certain name.

 

1. (public) The instance of SpecificFlight

— creates an Iterator over all the crewMember links of the SpecificFlight

— for each of them call operation 2, until it finds a match.

2. (may be public) The instance of EmployeeRole returns its name.

SpecificFlight

+ findCrewMember [e1]

EmployeeRole

+ getName [e2]

* *crewMember

Page 25: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 25

5.9 Implementing Class Diagrams in Java

1. Attributes are implemented as instance variables

2. Generalizations are implemented using extends

3. Interfaces are implemented using implements

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

Page 26: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 26

Example: SpecificFlight

class SpecificFlight{ private Calendar date; private RegularFlight regularFlight; private TerminalOfAirport destination; private Airplane airplane; private FlightLog flightLog; private ArrayList crewMembers; // of EmployeeRole private ArrayList bookings ...}

Page 27: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 27

Example: SpecificFlight

// Constructor that should only be called from

// addSpecificFlight

SpecificFlight(

Calendar aDate,

RegularFlight aRegularFlight)

{

date = aDate;

regularFlight = aRegularFlight;

}

Page 28: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 28

Example: RegularFlightclass RegularFlight{ private ArrayList specificFlights; ... // Method that has primary // responsibility

public void addSpecificFlight( Calendar aDate) { SpecificFlight newSpecificFlight; newSpecificFlight = new SpecificFlight(aDate, this); specificFlights.add(newSpecificFlight); } ...}

Page 29: Chapter 5:  Modelling with Classes (continued)

© Lethbridge/Laganière 2001 Chapter 5: Modelling with classes 29

Initial StressFree domain model: some classes