UML class diagrams - the notation - Dipartimento di Ingegneria dell

Transcript

UML class diagrams - the notation - Dipartimento di Ingegneria dell
Enrico Vicario - AA 13/14
SW Engineering
Software Engineering - A.A. 13/14
UML class diagrams - the notation
In the style of Martin Fowler, "UML distilled"
Enrico Vicario
Dipartimento di Ingegneria dell'Informazione
Laboratorio Scienza e Tecnologia del Software
Università di Firenze
[email protected], www.dsi.unifi.it/~vicario
1/42
Enrico Vicario - AA 13/14
SW Engineering
UML class diagrams
UML: Unified Modeling Language
Language: a notation, later accompanied by a method of use
Modeling: suitable for abstraction, mainly Object Oriented
Unified: a suite of diagrams from different approaches,
some with more prominent role (core diagrams)
Class diagram
A core diagram in the UML suite
Natively intended as a way to capture an OO implementation,
but equally relevant in other levels of abstraction and for different intents
We start here with the implementation perspective
Not the most relevant, but the easiest one at this point
2/42
Enrico Vicario - AA 13/14
SW Engineering
Class diagrams
Class Diagrams serve to capture the organization of "classes"
Allocation and partition of responsibilities
Relations of specialization, association, composition, use, creation
Provides a static view
Open to several potential instantiations and behaviors
(more answers later with object diagrams and sequence diagrams)
3/42
Class
Enrico Vicario - AA 13/14
SW Engineering
Name + attributes + methods of a class
Stack
intended as a construct of some OO language
TOS
Size
init()
push()
pop()
isEmpty()
isFull()
Visibility (access modifiers) of attributes and methods
+ public: visible for any class
# protected: visibile (within the package and) for sub-classes
~ Package-protected
- private: visibile within the class
4/42
Enrico Vicario - AA 13/14
SW Engineering
Types of signature and returned values
Types of attributes, signature of parameters and returned value
Pascal-like notation (sigh!)
virtual methods (for c++ more than Java)
Operation()
Operation()=0
<<virtual>> operation()
5/42
Enrico Vicario - AA 13/14
SW Engineering
Class stereotypes
Stereotypes
Add specialized semantics
according to some profile
(More later)
To be used with judice (and incompleteness)
The final goal is to communicate among developers,
within some context
• a lab, an enterprise, a set of enterprises involved in a common project, …
6/42
Enrico Vicario - AA 13/14
SW Engineering
Association
Documents a structural relation between two classes
The objects of one class
maintain a reference to some object in the other class
Navigation: which class has a reference to the other one
bi-directional association suppresses both end-arrows
Multiplicity
All variants, but most useful to distinguish between one and many (*)
7/42
Enrico Vicario - AA 13/14
SW Engineering
Roles
document the role played by an association end
basically, the name of the reference in the opposite end class
Association name
Less useful than the role
8/42
Association
Enrico Vicario - AA 13/14
SW Engineering
Associations of subtype <<uses>> and <<creates>>
Creates
documents the situation where methods of one class
create instances of objects in another class
Uses
documents the situation where methods of one class
reer to methods or attributes of another class
Different meaning wrt an association:
uses does not correspond to a class variable,
but rather to some parameter passed in some method invocation
Dashed line
with stereotypes <<uses>> and <<creates>>
<<creates>> usually suppressed
9/42
Enrico Vicario - AA 13/14
SW Engineering
Associazione e uso
L’associazione documenta una relazione strutturale tra due tipi
non un riferimento temporaneo entro i limiti di una operazione
Un link temporaneo è meglio rappresentato come relazione d’uso,
tipicamente omessa nel modello per il bene della semplicità
il link tra un medico e i suoi assistiti è una associazione
Il link tra un medico e gli assistiti che si sono iscritti per le visite oggi
è probabile che sia una relazione di uso
La differenza è nitida nella prospettiva dell’implementazione
• L’associazione è un data member
che ha valore nel corso dell’intera computazione
• La relazione di uso è un puntatore
ricevuto come parametro nella invocazione di un metodo
Si evita di rappresentare l’attributo che realizza l’associazione
10/42
Enrico Vicario - AA 13/14
SW Engineering
Aggregation and composition
Aggregazione documents a relation “Is part of”
The same part can be aggregated within multiple contexts
E.g. un corso di laurea include un modulo di insegnamento
Ma lo stesso insegnamento può esser parte di più corsi
Composition has the stronger meaning of inclusion
The composed objects exists only in the context
E.g. il libretto di uno studente
!?
11/42
Enrico Vicario - AA 13/14
SW Engineering
Generalization
The relation of generalization documents substitutability or inheritance
A class extends some other class or implements some interface
represents inheritance, and substitutability with the same notation
About substitutability:
Un medico può essere sostituito da un medico pediatra:
qualunque cosa può fare un medico la può fare anche un pediatra
Il viceversa non è vero
12/42
Enrico Vicario - AA 13/14
SW Engineering
Multiple inheritance
Class extension and interface implementation
13/42
Generalization
Enrico Vicario - AA 13/14
SW Engineering
14/42
Package
Enrico Vicario - AA 13/14
SW Engineering
15/42
Internal class structure
Enrico Vicario - AA 13/14
SW Engineering
Represent class instances within a class diagram
16/42
Class instances
Enrico Vicario - AA 13/14
SW Engineering
The UML Object diagram
Documents organization of class instances
in some significant or exemplificative stage of execution
Represents objects, with their valued attributes,
and relations between objects
Not a core diagram
Useful to understand the consequences of a class diagram
(… and to document complex relations in the conceptual perspective):
e.g. un medico è in relazione con i suoi assistiti,
ma egli stesso e’ un assistito a cui è attribuito un altro medico (?!)
Is a static diagram
17/42
Object diagram
Enrico Vicario - AA 13/14
SW Engineering
On the different perspectives of types and instances
When wrting code you see classes
… but run-time execution is made by instances
Client
Component
+component
+Operation()
ConcreateComponent
Decorator
ConcreteDecorator1
Object2 : Client
18/42
Object3 : ConcreteDecorator2
ConcreteDecorator2
Object4 : ConcreteDecorator1
Object5 : ConcreteComponent
Enrico Vicario - AA 13/14
SW Engineering
More on stereotypes and profiles
Stereotypes
Specializes the semantics of some construct of the UML diagram
Denoted by << >> brackets
Sometimes associated with a graphical notation
E.g. the signs for composition, aggregation, use or creation, interface, …
19/42
Enrico Vicario - AA 13/14
SW Engineering
Profile
Defines a reference conceptual model for some domain
Specializes UML through stereotypes capturing domain specific concepts
Shared and agreed within some context of development
or standardized by the Object Management Group
SysML: System Modeling language
MARTE: Modeling and Analysis of Real Time and Embedded Systems
…and many more
http://www.omg.org/technology/documents/profile_catalog.htm
20/42
Enrico Vicario - AA 13/14
SW Engineering
Profile
A UML profile is a specification
that does one or more of the following:
Identifies a subset of the UML metamodel.
Specifies “well-formedness rules” beyond those specified by the identified
subset of the UML metamodel.
“Well-formedness rule” is a term used in the normative UML metamodel
specification to describe a set of constraints written in UML’s Object
Constraint Language (OCL) that contributes to the definition of a metamodel
element.
Specifies “standard elements” beyond those specified by the identified subset
of the UML metamodel.
“Standard element” is a term used in the UML metamodel specification to
describe a standard instance of a UML stereotype, tagged value or constraint.
Specifies semantics, expressed in natural language, beyond those specified
by the identified subset of the UML metamodel.
Specifies common model elements, expressed in terms of the profile.
Used not only in the implementation perspective
21/42
Enrico Vicario - AA 13/14
SW Engineering
SysML
MARTE
22/42
Some standardized Profiles 1/3
Enrico Vicario - AA 13/14
SW Engineering
23/42
Some standardized Profiles 2/3
Enrico Vicario - AA 13/14
SW Engineering
…
24/42
Some standardized Profiles 3/3
Enrico Vicario - AA 13/14
SW Engineering
25/42
Can all this be used to write classes? 1/2
Enrico Vicario - AA 13/14
SW Engineering
Can all this be used to write classes? 2/2
Is this convenient ?
possibly for round-trip engineering
in Model Driven development
Not for abstraction code is the best complete representation of itself
… not a practice for "real" SW developers
26/42
Enrico Vicario - AA 13/14
SW Engineering
On the detail of class diagrams in the implementation perspective
In principle an implementation model could be 1:1 wrt the code
Javadoc, Doxygen, …
Round trip engineering
… but in this case it would loose much of its "modeling" aim
Hide details such as local classes, private and methodes and attributes, …
Avoid definition of unessential or undetermined choices
The right way: provide a “Close enough” representation
27/42
A class diagram in the implementation perspective: the Observer pattern
Enrico Vicario - AA 13/14
SW Engineering
Close enough representation
28/42
Enrico Vicario - AA 13/14
SW Engineering
Class diagrams can serve for different abstractions and intents
3 levels of abstraction
Implementation perspective
• rappresenta il modo con cui è realizzato un sistema SW
• Un oggetto nella rappresentazione
corrisponde a un oggetto in un linguaggio OO (Java, c++, …)
Specification perspective
• Rappresenta le astrazioni che saranno realizzate in un sistema SW
• Un oggetto della specifica è tipicamente realizzato
attraverso più oggetti dell’implementazione
• Non necessariamente viene realizzata con un linguaggio OO
Conceptual perspective
• Descrive le entità di un domino applicativo
• Non necessariamente rappresentate in una realizzazione SW
2 representation intents
Specification: of something that shall be implemented
Description: of something that already exists
Different combinations of 3x2 along the lifecycle of a SW system
29/42
Enrico Vicario - AA 13/14
SW Engineering
Class diagrams in the specification perspective
Classes capture abstractions implemented within an information system
E.g. internal and external interfaces implemented within a package,
allocation of responsibilities
May abstract from constructs (or idioms)
of the specific language or framework used for the implementation
Essential for "high-level" design
Enable discussion on SW architecture, at different levels of granularity
Document the organization of an existing component
Plan integration or maintenance
30/42
Enrico Vicario - AA 13/14
SW Engineering
Some more constructs: association classes
Association classes
Provide attributes and also methods for associations
Used in the specification (and concpetual) perspective
to drive implementation
Much useful also in the conceptual perspective
31/42
Enrico Vicario - AA 13/14
SW Engineering
Some more constructs: independent generalizations
Generalization may refer to multiple orthogonal directions,
with a discriminator for each direction
E.g. medici pediatri o di base / convenzionati o esterni
Requires design in the step to implementation
Much useful also in the conceptual perspective
32/42
Enrico Vicario - AA 13/14
SW Engineering
Some more constructs: constraints, comments, annotations
Constraints
Within curly brackets {}
Comments
Callouts boxes with free text
Annotations
Supported by the editing tool
Much useful to avoid over-specification and detail clutter
Also in the conceptual perspective
33/42
Class diagrams in the conceptual perspective
Enrico Vicario - AA 13/14
SW Engineering
Classes capture the concepts of some domain
Independently from their possible implementation
within an information system
Primarily intended for analysis and requirements definition
E.g. concepts involved in the process of examinations
Facoltà
Triennale
PersonaleStrutturato
1
nome
email
telefono
matricola
CorsoDiLaurea
Insegnamento
codice
nome
CFU
Magistrale
Docente
+titolare
1
SettoreScientifico
+afferenza Dipartimento
1..*
2
1
+commissario
{1 dei 2 Docenti commissari
è il titolare dell'Insegnamento}
Studente
matricola
nome
cognome
email
34/42
EsameSostenuto
Libretto
1
0..*
data
voto
argomentiDiscussi
Enrico Vicario - AA 13/14
SW Engineering
Class diagrams in the conceptual perspective
A class diagram in conceptual perspective:
medical guidelines and protocols
35/42
Enrico Vicario - AA 13/14
SW Engineering
Which are the right modeling artifacts to produce?
Different artifacts
suited for different stages of the SW lifecycle,
with different responsibility for parties (developer, customer, …)
Enable incremental refinement and natural transition,
possibly supported by some methodology (e.g. Iconix)
The choice depends on process characteristics
duration of development process, number of involved roles and people,
contractual relation among parties, …
Cost of documentation maintenance, reverse engineering and
Model Driven Development
expected lifecycle of the product, duration, maintenance and evolution
… which in turn should depend on the charateristics of the product
complexity of functions, size of implementation structure, quality attributes
Much about agility vs discipline
I.e. about the trade-off between lightness and robustness
Well represented in eXtreme Programming vs Unified Process
(More answers later, when dealing with SW lifecycle)
36/42
Enrico Vicario - AA 13/14
SW Engineering
Dynamic view diagrams
Vista statica
Class diagram: tipi e relazioni tra gli oggetti nei tipi
Object diagram: una particolare istanza dei tipi
Interfaccia tra il sistema e il suo ambiente
Use case diagrams
(Much relevant, but discussed later)
Vista dinamica (Interaction diagrams)
Collaboration diagram e Sequence diagram
Documentano la sequenza dei messaggi
scambiati tra un insieme di oggetti in uno scenario di esecuzione
Activity diagram: Aggiunge concetti di parallelismo e concorrenza
37/42
Enrico Vicario - AA 13/14
SW Engineering
Sequence diagram
Tipicamente per documentare la dinamica su un class diagram
38/42
Enrico Vicario - AA 13/14
SW Engineering
Sequence diagram
Oggetti attivati in uno scenario di esecuzione
Tempo di vita e tempo di attivazione
Messaggi scambiati
Creazione
invocazione di metodi
self-reference
La direzione verticale
è il tempo
Quella orizzontale
sono gli oggetti
E’ una vista dinamica
39/42
Utile per descrivere la dinamica associata a schemi statici
pericolosissimo nella progettazione, anche peggio nell’analisi
Applicabile a class diagrams ma anche a use cases
Enrico Vicario - AA 13/14
SW Engineering
Collaboration diagram
Rappresenta l’ordine degli eventi numerandoli esplicitamente
numerazione strutturata: 1 ->2 ->3.1->3.2->3.3->4
Equivale ad un sequence diagram
I due tradotti in modo automatico
viste di uno stesso “interaction diagram”
Privilegia la rappresentazione dell’organizzazione degli oggetti
40/42
Enrico Vicario - AA 13/14
SW Engineering
Star UML
Open source
… but under windows !
Not the only one
41/42
tools
Enrico Vicario - AA 13/14
SW Engineering
References
Martin Fowler, “UML Distilled: Guida rapida al linguaggio di modellazione
standard” - terza edizione, Pearson Education Italia, Febbraio 2004 .
Martin Fowler, “UML Distilled: a brief guide to the standard object
modeling language”, third edition (Addison Wesley).
42/42