Vous êtes sur la page 1sur 42

Software Engineering

UML
Class Diagrams

B. Wakim
Outline

 Object Oriented Modeling


 Class Diagrams
 Field declaration
 Method declaration
 OO relationships

 Generalization
 Association
 Cardinality

11/11/2020 I3301- BWAKIM 2


Object Oriented Modeling
Interpretation in the Real Interpretation in the
World Model

An object is a thing that can be An object has an identity, a


Object
distinctly identified. state, and a behavior.

A class represents a set of objects with


A class characterizes the
similar characteristics and behavior. This
Class structure of states and behaviors
objects are called the instances of the
that are shared by all instances.
class.

11/11/2020 I3301- BWAKIM 3


Objects
 Each of object has a unique identity.
 The state of an object is composed of a set of fields
(data fields), or attributes.
 Each field has a name, a type, and a value.
 Behaviors are defined by methods.
 Each method has a name, a type, and a value.
 Each method may or may not return a value.
 Methods that do not modify the state of the object are
called accessors.
 Methods that modify the state of the object are called
mutators.

11/11/2020 I3301- BWAKIM 4


Classes
 A class defines a template for creating or instantiating its instances
or objects.
 A class is a description of a set of objects that share the same
attributes, operations, relationships, and semantics. A class defines --
 the names and types of all fields

 the names, types, implementations of all methods

 The values of the fields are not defined or fixed in the class
definition.
 Each instance of the class has its own state.
 Different instances of the class may have different states.
 The implementations of methods are defined in the class definition
and fixed for a given object.

11/11/2020 I3301- BWAKIM 5


Class Diagrams

“Class diagrams show


generic descriptions of
possible systems, and object
diagrams show particular
instantiations of systems
and their behaviour.”

11/11/2020 I3301- BWAKIM


Class diagram

 Better name: “Static structure diagram”


 A class diagram depicts classes and their interrelationships:
 Inheritance -- extension and implementation
 Association -- aggregation and compostion
 Dependency
 Used for describing structure and behavior in the use cases
 Provide a conceptual model of the system in terms of entities
and their relationships
 Used for requirement capture, end-user interaction
 Detailed class diagrams are used for developers

11/11/2020 I3301- BWAKIM 7


UML Notation for Classes
The top compartment shows the
ClassName
class name.
field1 The middle compartment contains
… the declarations of the fields of the
fieldn class.
method1
The bottom compartment contains

the declarations of the methods
methodm
11/11/2020 I3301- BWAKIM 8
Class diagram

Name
Account_Name
- Customer_Name
- Balance Attributes

+addFunds( ) Operations
+withDraw( )
+transfer( )

11/11/2020 I3301- BWAKIM 9


Class diagram
 Each class is represented by a rectangle subdivided into three
compartments
 Name
 Attributes
 Operations

 Modifiers are used to indicate visibility of attributes and


operations.
 ‘+’ is used to denote Public visibility (everyone)
 ‘#’ is used to denote Protected visibility (friends and derived)
 ‘-’ is used to denote Private visibility (no one)

 By default, attributes are hidden and operations are visible.

11/11/2020 I3301- BWAKIM 10


Field Declaration
 The name of the field is required in the field declaration.
 Field declaration may include:
[Visibility][Type]Name[[Multiplicity]][=InitialValue]
[Visibility]Name[[Multiplicity]][:Type][=InitialValue]
 Visibility or accessibility defines the scope:
 ‘+’ is used to denote Public visibility  the feature is accessible to any
class)
 ‘#’ is used to denote Protected visibility  the feature is accessible to
the class itself, all the classes in the same package, and all its
subclasses
 ‘-’ is used to denote Private visibility  the feature is only accessible
within the class itself.
 ‘~’ is used to denote Package visibility  the feature is accessible to
the class itself and all classes in the same package.
 A attribute can be derived: calculated : it will be represented by /

11/11/2020 I3301- BWAKIM 11


Field Declaration

11/11/2020 I3301- BWAKIM 12


Visibility syntax in Java and UML
Visibility Java Syntax UML Syntax

public public +

protected protected #

package ~

private private -

11/11/2020 I3301- BWAKIM 13


Fields Examples

Java Syntax UML Syntax

Date birthday Birthday:Date

Public int duration = 100 +duration:int = 100

Private Student students[0..MAX_Size] -students[0..MAX_Size]:Student

11/11/2020 I3301- BWAKIM 14


Method Declaration
 The name of the method is required in the method
declaration.
 Method declaration may include:

[Visibility][Type]Name([Parameter, ...])
[Visibility]Name([Parameter, ...])[:Type]
 Each parameter of a method can be specified by --
Type Name

11/11/2020 I3301- BWAKIM 15


Examples

Java Syntax UML Syntax

void move(int dx, int dy) ~move(int dx, int dy)

public int getSize() +int getSize()

11/11/2020 I3301- BWAKIM 16


Different Levels of Specifying
Classes

11/11/2020 I3301- BWAKIM 17


Attributes and Methods – Class level
 Underlined in the class
 Class Attribute  common value to all instances of that class
 Class method  operation on the class itself (creating new
instance for example).

11/11/2020 I3301- BWAKIM 18


Message Passing or
Method Invocation

 Objects communicate with one another through message


passing.
 A message represent a command sent to an object --
recipient
 A message consists of the receiving object, the method to be
invoked and the arguments to method.
Recipient p1
p1.move(10,20) Method move()
Arguments (10,20)
11/11/2020 I3301- BWAKIM 19
OO Relationships

 There are two kinds of Relationships


 Generalization (parent-child relationship)
 Association (student enrolls in course)

 Associations can be further classified as


 Aggregation
 Composition

11/11/2020 I3301- BWAKIM 20


OO Relationships: Generalization

Supertype Example: Customer

Regular Loyalty
Customer Customer

Subtype1 Subtype2
-Inheritance is a required feature of object orientation
-Generalization expresses a parent/child relationship among related classes.
-Used for abstracting details in several layers

11/11/2020 I3301- BWAKIM 21


Inheritance

 Define a relationship among classes and


interfaces
 Inheritance model -- the is-a(n)
relationship

11/11/2020 I3301- BWAKIM 22


Inheritance : Example

11/11/2020 I3301- BWAKIM 23


Class Diagram: Inheritance
Article

Référence
Désignation
PrixHT
Quantité { Classe de base
classe mère
super-classe
GENERALISATION

SPECIALISATION

PrixTTC ()
PrixTransport ()
Retirer ()
Ajouter ()

Électroménager ArticleDeLuxe Vêtement

DuréeGarantie
Poids
Coloris
Taille { Classes filles
classes dérivées
sous-classes
ValiderGarantie PrixTTC ()
()

11/11/2020 I3301- BWAKIM 24


Class Diagram: Inheritance
Expression des contraintes et des discriminants :

Véhicule
{non disjointes} ou {inclusif}
Motorisation Milieu

Véhicule à moteur Véhicule à Voile Véhicule terrestre Véhicule marin

Contraintes prédéfinies :
{disjoint} : une instance de sous-classe ne peut être instance d ’une autre sous-classe
{overlapping} : une instance peut appartenir à plusieurs sous-classes
{complete} : l ’arbre d ’héritage est exhaustif
{incomplete} : d ’autres sous-classes peuvent être ajoutées à l ’arbre des sous-classes
Discriminant : Le discriminant sert à préciser le critère de classification utilisé
11/11/2020 I3301- BWAKIM 25
Relations in Java

 class Contrat { Client


bénéficiaire; ... }

• Class ClientProfessionnel
extends Client { ... }

11/11/2020 I3301- BWAKIM 26


Abstract Classes
 Not allowed to have objects.
 Used only for inheritance
 Described common attributes and behavior for other classes.
 Class without an instance.
 Undefined operations in Italic
 Class name in Italic or use the stereotype « abstract »
 Operation defined in the different sub-classes (specific to these sub-classes).

11/11/2020 I3301- BWAKIM 27


Abstract Class Hierarchy Diagram
Vehicle {abstract}
drives *
Person
color

drive () {abstract}

Car Boat

drive () drive ()

11/11/2020 I3301- BWAKIM 28


Abstract Class Hierarchy Diagram

* Figure {abstract}
Figures

# position: Pos

draw () {abstract}

Group Polygon

draw () draw ()

11/11/2020 I3301- BWAKIM 29


Java Implementation
Abstract public class Figure {
abstract public void draw();
protected Pos position; }
public class Group extends Figure {
public void draw () {
for (int i= 0; i < consist_of.size(); i++) {
consist_of[i].draw(); }
private FigureVector figures; /* FigureVector is a specialized class of the standard Vector
class that implements a dynamic array.
FigureVector adds hard type checking */
}
public class Polygon extends Figure {
public void draw () {
// draw polygon code
}}

11/11/2020 I3301- BWAKIM 30


OO Relationships: Association

 Represent relationship between instances of classes


 Student enrolls in a course
 Courses have students
 Courses have exams
 Etc.

 Association has two ends


 Role names (e.g. enrolls)
 Multiplicity (e.g. One course can have many students)
 Navigability (unidirectional, bidirectional)

11/11/2020 I3301- BWAKIM 31


OO Relationships: Association

11/11/2020 I3301- BWAKIM 32


Association

 Association represents binary


relationship between classes
* enroll *
Student Course
advisee * *

teach

1
1
Faculty
adviser

11/11/2020 I3301- BWAKIM 33


Association: Multiplicity and Roles
student
1 *
University Person

0..1 *
employer teacher

Multiplicity Role
Symbol Meaning
1 One and only one Role
0..1 Zero or one “A given university groups many people;
M..N From M to N (natural language)
some act as students, others as teachers.
A given student belongs to a single
* From zero to any positive integer university; a given teacher may or may not
0..* From zero to any positive integer be working for the university at a particular
1..* From one to any positive integer time.”

11/11/2020 I3301- BWAKIM 34


Class Diagram : Cardinality

classe Exactement 1 instance

* Plusieurs (0 ou plus)
classe

0..1 Optionnel (0 ou 1)
classe

1..* 1 ou plus
classe

1-3, 5 Cas particuliers


classe

* {ordered}
classe Avec ordonnancement

11/11/2020 I3301- BWAKIM 35


Components of Class Diagrams
 Multiplicities
 How many of
each?

 Labels to indicate
how reference is
viewed

 Role and
Association classes

11/11/2020 I3301- BWAKIM 36


Class diagram

11/11/2020 I3301- BWAKIM 37


Class Diagram: Association direction

If the association is oriented, means class A can access class B , but B cannot access A.

By default, an association is bi-directional (a simple line).

11/11/2020 I3301- BWAKIM 38


Class Diagram: Association
Un seul fabriquant (qui est une entreprise) crée un ensemble produits de type articles de luxe

1 Fabrique *
Entreprise ArticleDeLuxe
fabricant produit fabriqué
Nom : Chaîne Référence : Entier
AdresseSiège: Chaîne Désignation : Chaîne
AdresseUsine: Chaîne PrixHT : Réel
Quantité : Entier

Plusieurs revendeurs (boutiques de luxe) peuvent vendre des articles de luxe

* Vend *
Boutique de luxe ArticleDeLuxe
revendeur produit diffusé
{ordered}

11/11/2020 I3301- BWAKIM 39


EXAMPLE

11/11/2020 I3301- BWAKIM 40


Use Case

41
Class Diagram

42

Vous aimerez peut-être aussi