UML Class Diagram
UML Class Diagram
Employee
name Name (mandatory)
Attributes
(optional) employee#
department
hire()
fire() Operations
assignproject() (optional)
4
Attribute
type Name of the class
Attribute
name Student
Other Properties
Operation
name Return value
Parameters
5
The instances of a class are called objects.
Objects are represented as:
Fred_Bloggs:Employee
6
Objects do not exist in isolation from one another
A relationship represents a connection among things.
E.g. Fred_Bloggs:employee is associated with the KillerApp:project object
But we will capture these relationships at the class level
7
Associations are semantic connections between
classes.
Ifthere is a link between two objects, there must be an
association between the classes of those objects.
Links are instances of associations just as objects are
instances of classes.
Association
Link
8
Associations may optionally have the following:
Association name
may be prefixed or postfixed with a small black arrowhead to indicate the direction in which the name should be
read;
should be a verb or verb phrase;
Role names
on one or both association ends;
should be a noun or noun phrase describing the semantics of the role;
Multiplicity
The number of objects that can participate in an instantiated relation
Navigability
* *
multiplicity multiplicity
9
Ask questions about the associations:
Can a company exist without any employee?
If yes, then the association is optional at the Employee end - zero or more (0..*)
If no, then it is not optional - one or more (1..*)
If it must have only one employee - exactly one (1)
What about the other end of the association?
Can an employee work for more than one company?
No. So the correct multiplicity is one.
1 * .. 0
Company Employee
10
Multiplicity Multiplicity
A client has A staff member has
exactly one staffmember zero or more clients on
as a contact person His/her clientList
Name
of the
association
:Client
:StaffMember
companyAddress
staffName 1 0..* companyEmail
staff# liaises with companyFax
staffStartDate contact ClientList companyName
person companyTelephone
Direction
The “liaises with”
association should be
read in this direction
Role
The staffmember’s Role
role in this association The clients’ role
is as a contact person in this association
is as a clientList 11
12
Order
+ dateReceived: Date [0..1]
+ isPrepaid: Boolean [1]
+ lineItems: OrderLine [*] {ordered}
0..1 * 1
Date Order Boolean
+dateReceived +isPrepaid
1
* +lineItems {ordered}
OrderLine
13
Generalizationis a relationship
between a more general thing and a
more specific thing:
Child
Subclass
Descendant
More specific element
Leaf
Class inheritance is implicit in a generalization relationship between classes.
Subclasses inherit attributes, associations, & operations from the superclass
aggregati
on
Club Member
* *
:Car :Train
0..1 0..1
:Person 0..*
driver 1 passengers
aggregati
on
18
Aggregation
This is the “Has-a” or “Whole/part” relationship
Composition
Strong form of aggregation that implies ownership:
if the whole is removed from the model, so is the part.
the whole is responsible for the disposition of its parts
Note: Parts can be removed from the composite (where allowed) before the composite is deleted
{ordered} centre
Polygon Point Circle
3..* 1
composition
:Locomotive 1..*
1 0..1
:Car :Train
0..1 0..1
:Person 0..*
driver 1 passengers
aggregati
on
20
Draw the UML class diagram which
represents a file system – containing
files and directories
Dependencies are relationships in which a change to the
supplier affects, or supplies information to, the client.
The client depends on the supplier in some way.
Dependencies are drawn as a dashed arrow from
client to supplier.
Model
View ViewController
Layout
«use»-the client makes use of the supplier in some way -this is
the catch-all.
«call»-the client operation invokes the supplier operation.
«parameter»-the supplier is a parameter or return value from
one of the client's operations.
«instantiate»-the client is an instance of the supplier.
client Supplier
The stereotype is
often omitted
client
Supplier client
<<call>> <<use>>
Collection
List
Order
ArrayList
LineItems [*]
Comments
-- can be used to add comments within a class description
Notes
Date Range
{length = start - end} Start: Date
End: Date
/length: integer
Constraint Rules
Any further constraints {in curly braces}
e.g. {time limit: length must not be more than three months}
26
Division of Responsibility
Operations that objects are responsible for providing
Subclassing
Inheritance, generalization
Navigability / Visibility
When objects need to know about other objects to call their operations
Aggregation / Composition
When objects are part of other objects
Dependencies
When changing the design of a class will affect other classes
Interfaces
Used to reduce coupling between objects
27
What makes a good analysis class?
Its name reflects its intent.
It is a crisp abstraction that models one specific
element of the problem domain.
It maps to a clearly identifiable feature of the
problem domain.
It has a small, well-defined set of responsibilities:
▪ a responsibility is a contract or obligation that a class has to
its clients;
▪ a responsibility is a semantically cohesive set of operations;
▪ there should only be about three to five responsibilities per
class.
It has high cohesion – all features of the class should
help to realize its intent.
It has low coupling – a class should only collaborate
with a small number of other classes to realize its
intent.
What makes a bad analysis class?
CRC Analysis
Use-Case-Based Analysis
Real-World Analysis
1.Collect as much relevant information about the problem
domain as possible; suitable sources of information are:
The requirements model
The use case model
The project glossary
Any other document (architecture, vision documents, etc.)
DataStructure InnerEvenIterator
<<OuterClass>> <<InnerClass>>
Local Classes
• member class that is declared within a
body of a method or instance initializer
• scoping rules make this useful
Anonymous Classes
• A local class that has no name
• One statement declares and creates an
object for the class
– new parentClassName (params) { body}
– new interfaceName() { body}