Post on 11-Jul-2015
Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0
UML + Python
Python
UML
Le minimum
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
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
Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0
UML + Python
INTERFACE
Tous les Comptes possèdent les mêmes opérations
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): # ...
# ...
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
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
Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0
UML + Python
HÉRITAGEQuand la classe de base fait
quelquechose 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()
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
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) )
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()
Sylvain Leroux – www.chicoree.fr – 2009 – Licence CC-BY3.0
UML + Python
A VOUS DE JOUER!
QR&