Lloseng CH 05 E2
Lloseng CH 05 E2
Lloseng CH 05 E2
Lethbridge/Laganire 2005
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 how systems behave internally Component and deployment diagrams show how the various components of systems are arranged logically and physically
Lethbridge/Laganire 2005
UML features
It has detailed semantics It has extension mechanisms It has an associated textual language Object Constraint Language (OCL)
Lethbridge/Laganire 2005
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
Generalizations
- group classes into inheritance hierarchies
Lethbridge/Laganire 2005
Classes
A class is simply represented as a box with the name of the class inside The diagram may also show the attributes and operations The complete signature of an operation is:
operationName(parameterName: parameterType ): returnType
Lethbridge/Laganire 2005
Lethbridge/Laganire 2005
Labelling associations
Each association can be labelled, to make explicit the nature of the association
Lethbridge/Laganire 2005
Company
Lethbridge/Laganire 2005
10
Manager
Open in Umple
Lethbridge/Laganire 2005
11
BoardOfDirectors
Open in Umple
Lethbridge/Laganire 2005
12
Avoid this
do this
Lethbridge/Laganire 2005
13
The frame around this diagram is an optional feature that any UML 2.0 may possess.
Lethbridge/Laganire 2005
14
Association classes
Sometimes, an attribute that concerns two associated classes cannot be placed in either of the classes The following are equivalent
15
Reflexive associations
It is possible for an association to connect a class to itself
Open in Umple
Lethbridge/Laganire 2005
16
Directionality in associations
Associations are by default bi-directional It is possible to limit the direction of an association by adding an arrow at one end
Open in Umple
Lethbridge/Laganire 2005
17
5.4 Generalization
Specializing a superclass into two or more subclasses A generalization set is a labeled group of generalizations with a common superclass The label (sometimes called the discriminator) describes the criteria used in the specialization
Lethbridge/Laganire 2005
18
Lethbridge/Laganire 2005
20
Lethbridge/Laganire 2005
21
Lethbridge/Laganire 2005
22
Lethbridge/Laganire 2005
23
Lethbridge/Laganire 2005
24
25
Lethbridge/Laganire 2005
26
Lethbridge/Laganire 2005
27
Composition
A composition is a strong kind of aggregation if the aggregate is destroyed, then the parts are destroyed as well
Lethbridge/Laganire 2005
28
Aggregation hierarchy
Lethbridge/Laganire 2005
29
Propagation
A mechanism where an operation in an aggregate is implemented by having the aggregate perform that operation on its parts At the same time, properties of the parts are often propagated back to the aggregate Propagation is to aggregation as inheritance is to generalization. The major difference is:
- inheritance is an implicit mechanism - propagation has to be programmed when required
Lethbridge/Laganire 2005
30
Interfaces
An interface describes a portion of the visible behaviour of a set of objects. An interface is similar to a class, except it lacks instance variables and implemented methods
Lethbridge/Laganire 2005
31
Lethbridge/Laganire 2005
32
Lethbridge/Laganire 2005
33
OCL statements
OCL statements can be built from: References to role names, association names, attributes and the results of operations The logical values true and false Logical operators such as and, or, =, >, < or <> (not equals) String values such as: a string Integers and real numbers Arithmetic operations *, /, +, -
Lethbridge/Laganire 2005
34
Lethbridge/Laganire 2005
35
Problems A person must have two parents Marriages not properly accounted for
Lethbridge/Laganire 2005
36
Open in Umple
Lethbridge/Laganire 2005
37
38
Lethbridge/Laganire 2005
39
The complete system model includes The system domain model User interface classes Architectural classes Utility classes
Lethbridge/Laganire 2005
40
41
Identifying classes
When developing a domain model you tend to discover classes When you work on the user interface or the system architecture, you tend to invent classes Needed to solve a particular design problem (Inventing may also occur when creating a domain model) Reuse should always be a concern Frameworks System extensions Similar systems
Lethbridge/Laganire 2005
42
Lethbridge/Laganire 2005
43
Lethbridge/Laganire 2005
44
some other class in your model Specify the multiplicity at both ends Label it clearly.
Lethbridge/Laganire 2005
45
Better: The borrow operation creates a Loan , and the return operation sets the returnedDate attribute.
Lethbridge/Laganire 2005
46
Identifying attributes
Look for information that must be maintained about each class Several nouns rejected as classes, may now become attributes An attribute should generally contain a simple value E.g. string, number
Lethbridge/Laganire 2005
47
Lethbridge/Laganire 2005
48
Lethbridge/Laganire 2005
49
top-down
- Look for more general classes first, specialize them if needed
Create an interface, instead of a superclass if The classes are very dissimilar except for having a few operations in common One or more of the classes already have their own superclasses Different implementations of the same class might be available
Lethbridge/Laganire 2005
50
An example (generalization)
Lethbridge/Laganire 2005
51
52
Categories of responsibilities
Setting and getting the values of attributes Creating and initializing new instances Loading to and saving from persistent storage Destroying instances Adding and deleting links of associations Copying, converting, transforming, transmitting or outputting Computing numerical results Navigating and searching Other specialized work
Lethbridge/Laganire 2005
53
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
Lethbridge/Laganire 2005
54
Move the cards around on a whiteboard to arrange them into a class diagram. Draw lines among the cards to represent associations and generalizations.
Lethbridge/Laganire 2005
55
Identifying operations
Operations are needed to realize the responsibilities of each class There may be several operations per responsibility The main operations that implement a responsibility are normally declared public Other methods that collaborate to perform the responsibility must be as private as possible
Lethbridge/Laganire 2005
56
Lethbridge/Laganire 2005
57
Class collaboration a
SpecificFlight *
0..1
Airplane
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
Lethbridge/Laganire 2005
58
Class collaboration b
SpecificFlight
+ createFlightLog [b1]
0..1 0..1
FlightLog
FlightLog [b2]
Creating an object and linking it to an existing object e.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 FlightLogs constructor makes a one-directional link back to the instance of SpecificFlight.
Lethbridge/Laganire 2005
59
PassengerRole
Class collaboration c
Booking
* Booking [c2] *
1
SpecificFlight
addLinkToBooking [c3]
Creating an association class, given two existing objects e.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 Bookings 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.
Chapter 5: Modelling with classes 60
Lethbridge/Laganire 2005
Class collaboration d
SpecificFlight
+ changeAirplane [d1]
0..1
Airplane
addLinkToSpecificFlight [a2, d3] deleteLinkToSpecificFlight [d2]
Changing the destination of a link e.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 SpecificFlight.
Lethbridge/Laganire 2005
link
to
the
instance
of
61
Class collaboration e
EmployeeRole
SpecificFlight
+ findCrewMember [e1]
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.
Lethbridge/Laganire 2005
62
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
Lethbridge/Laganire 2005
63
Example: SpecificFlight
class SpecificFlight { private Calendar date; private RegularFlight regularFlight; ... // Constructor that should only be called from // addSpecificFlight SpecificFlight( Calendar aDate, RegularFlight aRegularFlight) { date = aDate; regularFlight = aRegularFlight; } }
Lethbridge/Laganire 2005
64
Example: RegularFlight
class RegularFlight { private List specificFlights; ... // Method that has primary responsibility public void addSpecificFlight(Calendar aDate) { SpecificFlight newSpecificFlight; newSpecificFlight = new SpecificFlight(aDate, this); specificFlights.add(newSpecificFlight); } ... }
Lethbridge/Laganire 2005
65
Lethbridge/Laganire 2005
66