S̳t̳r̳a̳t̳é̳g̳i̳e̳ 1) Comportement difficile à mixer 2) Création d'objets selon les comportement 3) Factorisation non optimisée Fonction de tri → stratégie multiple ┌───────────────────┐ │ Agrégat │ <> ├───────────────────┤ ┌───────────────────┐ │ +setTri(Tri) │ tri │ Tri │ │ +trier() │◊------------------>├───────────────────┤ │ +setMax(Max) │ │+faire(Agregat) │ │ +max() │ └───────────────────┘ └───────────────────┘ △ ◊ | | | | ┌───────────────────┐ <> │ TriA │ ┌───────────────────┐ ├───────────────────┤ │ Max │ │ │ ├───────────────────┤ └───────────────────┘ │+faire(Agregat) │ └───────────────────┘ Agregat::trier() { ⋮ tri.faire(this); ⋮ } Agregat::Agregat() { ⋮ tri = new TriA(); // ← tri par défaut } Exemple : { Agregat a = new Agregat(); // a.setTri(new TriA()); a.trier(); } P̳r̳o̳x̳y̳ ⎧ Objet distant ⎨ Accès à un objet à sécuriser (délicat) ⎩ Accès à un objet volumineux → Se substituer à une autre chose ◦ créer une classe proxy ayant la même interface que l'objet réel ◦ ajouter une association vers l'objet réel pour lui déléguer l'action réelle <> ┌───────────────────┐ │ LaClasse │ ├───────────────────┤ │ │ └───────────────────┘ △ △ / \ / \ / \ / \ / \ / \ <> | ┌───────────────────┐ ┌───────────────────┐ │ Proxy │ │ LaClasseVrai │ ├───────────────────┤ réel ├───────────────────┤ │ +m1() │----------->│ +m1() │ └───────────────────┘ └───────────────────┘ ┌───────┐ ┌─────────────┐ ┌──────────────┐ │ Appli │ │ proxy:Proxy │ │ LaClasseVrai │ └───────┘ └─────────────┘ └──────────────┘ ¦ ¦ ¦ ¦ <> ¦ ¦ -------------------------▷⫾ ¦ ¦ ⫾ create ¦ ¦ ⫾-------------------------------▷⫾ ¦ ⫾ ⫾ ¦ mc1 ⫾ ⫾ -------------------------▷⫾ ⫾ ¦ ⫾ mc1() ⫾ ¦ ⫾-------------------------------▷⫾ ¦ ⫾ ⫾ ¦ ⫾ ⫾ ¦ ⫾ ⫾ C̳o̳m̳p̳o̳s̳i̳t̳e̳ ⎧ Arbre binaire… ⎨ Gestion des fichiers et dossiers Représente des arbres ⎨ Arbre des processus ⎨ XML/JSON ⎩ Gestion objet graphique (IHM avec des composants qui en contiennent d'autres) <> ┌───────────────────┐ * │ Component │ ––––––––––––––––––––––––––––––––––├───────────────────┤ | children │ +paint() │ | └───────────────────┘ | △ | | | –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––⋯⋯⋯ | | | | | <> <> <> | ┌──────────────────────────────┐ ┌──────────┐ ┌───────────┐ | │ Panel │ │ Button │ │ Textfield │ | ├──────────────────────────────┤ ├──────────┤ ├───────────┤ | │ +addComponent(Component) │ │ +paint() │ │ +paint() │ –––––◊│ +removeComponent(Component) │ └──────────┘ └───────────┘ │ +paint() │ │ +getChildren() : Component[] │ └──────────────────────────────┘ Panel::paint() { for(Component c: children) { c.paint(); } }