UML+Python

12
Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0 UML + Python Python UML Le minimum

Transcript of UML+Python

Page 1: UML+Python

Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0

UML + Python

Python

UML

Le minimum

Page 2: UML+Python

Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0

UML + Python

CLASSE

Attribut:chaque CompteCourant a le sien

Opération:chaque CompteCourant sait le faire

Page 3: UML+Python

Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0

UML + Python

CLASSE

class CompteCourant(object): def __init__(self): # A la création, le solde est à zéro self.solde = 0 # Solde en cents! def crediter(self, montant): self.solde += montant def debiter(self, montant): if self.solde < montant: raise OperationRejetee(); self.solde -= montant

Page 4: UML+Python

Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0

UML + Python

INTERFACE

Tous les Comptes possèdent les mêmes opérations

Page 5: UML+Python

Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0

UML + Python

INTERFACE

Pas besoin de coder les interfaces

Python est dynamique

class CompteSurLivret(object): def __init__(self, plafond): # ...

# ...

class CompteCourant(object): def __init__(self): # ...

# ...

Page 6: UML+Python

Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0

UML + Python

INTERFACE – option 1

Créer un stub pour l'interface

class CompteSurLivret(Compte): def __init__(self, plafond): # ...

# ...

class CompteCourant(Compte): def __init__(self): # ...

# ...

class Compte(object): def crediter(self, montant): pass def debiter(self, montant): pass

Faire hériterles classes

Page 7: UML+Python

Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0

UML + Python

INTERFACE – option 2Variante avec 

exceptions

class CompteSurLivret(Compte): def __init__(self, plafond): # ...

# ...

class CompteCourant(Compte): def __init__(self): # ...

# ...

class Compte(object): def crediter(self, montant): raise NonMisEnOeuvre() def debiter(self, montant): raise NonMisEnOeuvre()

Faire hériterles classes

Page 8: UML+Python

Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0

UML + Python

HÉRITAGEQuand la classe de base fait 

quelque­chose d'utile, on parle d'héritage

class Compte(object): def afficheSolde(self): print "%+10.2f" % (self.solde / 100.0) # ...

class CompteSurLivret(Compte): # ...

if __name__ == '__main__': compte = CompteSurLivret(10000) compte.crediter(10000) compte.afficheSolde()

Page 9: UML+Python

Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0

UML + Python

AGRÉGATION

class Client(object): def __init__(self, compte): self.compte = compte

if __name__ == '__main__': # John possède un compte courant john = Client( CompteCourant() ) # Paul possède un compte sur livret paul = Client( CompteSurLivret(5000000) )

Chaque Client possède un Compte

Page 10: UML+Python

Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0

UML + Python

AGRÉGATION

Chaque Client possède des Comptes

class Client(object): def __init__(self): self.compte = [] def ajouteCompte(self, compte): self.compte.append(compte)

if __name__ == '__main__': ringo = Client() ringo.ajouteCompte( CompteCourant() ) ringo.ajouteCompte( CompteCourant(5000000) )

Page 11: UML+Python

Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0

UML + Python

DIAGRAMME DE SÉQUENCE

if __name__ == '__main__': unCompte = CompteCourant() unCompte.crediter(10000) unCompte.afficheSolde()

Page 12: UML+Python

Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0

UML + Python

A VOUS DE JOUER!

QR&