Module 3
Module 3
Chapter 5:
Modelling with Classes
5.1 What is UML?
2
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
3
UML features
4
What constitutes a good model?
•A model should
• use a standard notation
• be understandable by clients and users
• lead software engineers to have insights about the system
• provide abstraction
5
5.2 Essentials of UML Class Diagrams
6
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
7
5.3 Associations and Multiplicity
•An association is used to show how two classes are related to each
other
• Symbols indicating multiplicity are shown at each end of the
association
8
Labelling associations
• Each association can be labelled, to make explicit the nature of
the association
9
Analyzing and validating associations
• Many-to-one
• A company has many employees,
• An employee can only work for one company.
• This company will not store data about the moonlighting activities of employees!
• A company can have zero employees
• E.g. a ‘shell’ company
• It is not possible to be an employee unless you work for a company
worksFor 1
Employee * Company
10
Analyzing and validating associations
• Many-to-many
• An assistant can work for many managers
• A manager can have many assistants
• Assistants can work in pools
• Managers can have a group of assistants
• Some managers might have zero assistants.
• Is it possible for an assistant to have, perhaps temporarily, zero managers?
Open in Umple
11
Analyzing and validating associations
• One-to-one
• For each company, there is exactly one board of directors
• A board is the board of only one company
• A company must always have a board
• A board must always be of some company
1
Company 1 BoardOfDirectors
Open in Umple
12
Analyzing and validating associations
•Avoid unnecessary one-to-one associations
13
A more complex example
• A booking is always for exactly one passenger
• no booking with zero passengers
• a booking could never involve more than one passenger.
• A Passenger can have any number of Bookings
• a passenger could have no bookings at all
• a passenger could have more than one booking
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
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
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
18
Avoiding unnecessary generalizations
Inappropriate hierarchy of
classes, which should be
instances
19
Avoiding unnecessary generalizations (cont)
Open in Umple
21
Handling multiple discriminators
• Using multiple inheritance
22
Avoiding having instances change class
• An instance should never need to change class
23
5.5 Object Diagrams
• A link is an instance of an association
• In the same way that we say an object is an instance of a class
24
Associations versus generalizations in object
diagrams
• Associations describe the relationships that will exist between instances at
run time.
• When you show an instance diagram generated from a class diagram, there will be an
instance of both classes joined by an association
25
5.6 More Advanced Features: Aggregation
• Aggregations are special associations that represent ‘part-whole’
relationships.
• The ‘whole’ side is often called the assembly or the
aggregate
• This symbol is a shorthand notation association named
isPartOf
26
When to use an aggregation
•As a general rule, you can mark an association as an aggregation if the
following are true:
• You can state that
• the parts ‘are part of’ the aggregate
• or the aggregate ‘is composed of’ the parts
• When something owns or controls the aggregate, then they also own or
control the parts
27
Composition
• A composition is a strong kind of aggregation
• if the aggregate is destroyed, then the parts are destroyed as
well
28
Aggregation hierarchy
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
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
31
Notes and descriptive text
32
5.7 Object Constraint Language (OCL)
33
OCL statements
34
An example: constraints on Polygons
35
5.8 Detailed Example: A Class Diagram for
Genealogy
• Problems
• A person must have two parents
• Marriages not properly accounted for
36
Genealogy example: Possible solutions
Open in Umple
37
5.9 The Process of Developing Class
Diagrams
•You can create UML models at different stages and with different
purposes and levels of details
• Exploratory domain model:
• Developed in domain analysis to learn about the domain
• System domain model:
• Models aspects of the domain represented by the system
• System model:
• Includes also classes used to build the user interface and system architecture
38
System domain model vs System model
39
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 to be used independently of particular sets of
• user interface classes
• architectural classes
• The complete system model includes
• The system domain model
• User interface classes
• Architectural classes
• Utility classes
40
Suggested sequence of activities
• Identify a first set of candidate classes
• Add associations and attributes
• Find generalizations
• List the main responsibilities of each class
• Decide on specific operations
• Iterate over the entire 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.
41
Identifying classes
42
A simple technique for discovering domain
classes
• Look at a source material such as a description of requirements
• Extract the nouns and noun phrases
• Eliminate nouns that:
• are redundant
• represent instances
• are vague or highly general
• not needed in the application
• Pay attention to classes in a domain model that represent types of users or
other actors
43
Identifying associations and attributes
• Start with classes you think are most central and important
• Decide on the clear and obvious data it must contain and its relationships to
other classes.
• Work outwards towards the classes that are less important.
• Avoid adding many associations and attributes to a class
• A system is simpler if it manipulates less information
44
Tips about identifying and specifying valid
associations
• An association should exist if a class
• possesses
• controls
• is connected to
• is related to
• is a part of
• has as parts
• is a member of, or
• has as members
some other class in your model
• Specify the multiplicity at both ends
• Label it clearly.
45
Actions versus associations
• A common mistake is to represent actions as if they were
associations
46
Identifying attributes
47
Tips about identifying and specifying valid attributes
48
An example (attributes and associations)
49
Identifying generalizations and interfaces
50
An example (generalization)
51
Allocating responsibilities to classes
• To determine responsibilities
• Perform use case analysis
• Look for verbs and nouns describing actions in the system
description
52
Categories of responsibilities
53
An example (responsibilities)
54
Prototyping a class diagram on paper
55
Identifying operations
56
An example (class collaboration)
57
SpecificFlight * 0..1 Airplane
58
SpecificFlight 1 0..1
0..1 FlightLog
Class collaboration ‘b’ + createFlightLog [b1] FlightLog [b2]
59
PassengerRole Booking SpecificFlight
+ makeBooking [c1] 1 * * 1
Booking [c2] addLinkToBooking [c3]
addLinkToBooking [c4]
Class
• collaboration ‘c’class, given two existing objects
Creating an association
• 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 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.
60
Airplane
SpecificFlight * 0..1
61
EmployeeRole SpecificFlight
* *
62
5.10 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
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;
}
}
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);
}
...
}
65
5.11 Difficulties and Risks when creating class
diagrams
66