From 8ea173ab17b772a6037b7013c95a3785e53c746b Mon Sep 17 00:00:00 2001 From: gwen Date: Mon, 4 May 2015 17:38:00 +0200 Subject: [PATCH] ajout des design patterns --- python/formation/DesignPatterns.txt | 94 +++++++++++++++++++++++++++++ python/formation/exercices.txt | 13 ++++ 2 files changed, 107 insertions(+) create mode 100644 python/formation/DesignPatterns.txt diff --git a/python/formation/DesignPatterns.txt b/python/formation/DesignPatterns.txt new file mode 100644 index 0000000..8f0a7e5 --- /dev/null +++ b/python/formation/DesignPatterns.txt @@ -0,0 +1,94 @@ +Les design patterns +===================== + +Les design patterns **ne sont pas** indépendants du langage. +Ils dépendent de l'implémentation. + +Le duck typing +------------------- + +En python, le duck typing est une forme extreme de programmation par interface. +Ne pas hériter dans des directions fausse + +exemple : une voiture ne peut hériter d'un moteur, parce que un moteur n'est pas une voiture. + +hold or wrap ? +-------------- + +**hold**:: + + O.S.method() + +Cela induit un couplage fort (cf la loi de Demeter) + +.. important:: law of Demeter : never more than one dot. + +wrap : a hold by private name, with a:: + + self.S.method() + +Ou bien une méthode getattr:: + + __getattr__() + +Gets coupling right. + + +wrapper can restrict, inheritance cannot restrict +-------------------------------------------------- + +:: + + class RestrictingWrapper(object): + + def __init__(self, w, block): + self._w = w + self._block = block + + def __getattr__(self, n): + if n in self._block: + raise AttributeError, n + return getattr(self._w, n) + +Pattern de création : singleton +------------------------------- + +# utiliser un module au lieu d'une classe + +in `toto.py`:: + + class Toto() + toto = Toto() + +in another module:: + + from toto import toto + +la factory +-------------- + +:: + + def load(pkg, obj): + m = __import__(pkg, {}, {}, [obj]) + return getattr(m, obj) + + cls = load('p1.p2.p3', 'c4') + + +template method (self delegation) +--------------------------------- + +# Abstract base class:: + + def OrganiseMethod() + def org_method(): + def do_this() + def do_that() + + def Concrete(OrganiseMethod) + def do_this(): ... + def do_that(): ... + +il est préférable de lever une NotImplementedError, ce qui revient à faire +une classe abstraite. diff --git a/python/formation/exercices.txt b/python/formation/exercices.txt index 6f5d8fa..e490b0a 100644 --- a/python/formation/exercices.txt +++ b/python/formation/exercices.txt @@ -5,3 +5,16 @@ Voici la liste des liens vers les différents exercices de la formation. .. todolist:: + + +exercices à faire +-------------------- + +- implémenter un script qui lit les arguments de la ligne de commande et qui les écrit sur la sortie standard +- implémenter les sous-classes de shape point et cercle, calculer le périmètre. +- itérer sur une liste et récupérer la valeur maximum de cette liste (la fonction builtin *max*) + + +>>> a = [3,8,3,5,9,1,4] +>>> max(a) +9