Вы находитесь на странице: 1из 14

pattern Livre Page I Vendredi, 9.

octobre 2009 10:31 10

Les Design
Patterns en Java
Les 23 modles de conception fondamentaux
Steven John Metsker
et William C. Wake

2009 Pearson Education France Les design patterns en Java Steven John Metsker & William C. Wake

pattern Livre Page II Vendredi, 9. octobre 2009 10:31 10

Pearson Education France a apport le plus grand soin la ralisation de ce livre afin de vous fournir une information complte et fiable. Cependant, Pearson Education France nassume de responsabilits, ni pour son utilisation, ni pour les contrefaons de brevets ou atteintes aux droits de tierces
personnes qui pourraient rsulter de cette utilisation.
Les exemples ou les programmes prsents dans cet ouvrage sont fournis pour illustrer les descriptions
thoriques. Ils ne sont en aucun cas destins une utilisation commerciale ou professionnelle.
Pearson Education France ne pourra en aucun cas tre tenu pour responsable des prjudices
ou dommages de quelque nature que ce soit pouvant rsulter de lutilisation de ces exemples ou
programmes.
Tous les noms de produits ou marques cits dans ce livre sont des marques dposes par leurs
propritaires respectifs.
Publi par Pearson Education France
47 bis, rue des Vinaigriers
75010 PARIS
Tl. : 01 72 74 90 00
www.pearson.fr

Titre original : Design Patterns in Java

Traduit de lamricain par Freenet Sofor ltd

Mise en pages : TyPAO


ISBN : 978-2-7440-2396-5
Copyright 2009 Pearson Education France
Tous droits rservs

ISBN original : 0-321-33302-0


Copyright 2006 by Addison-Wesley
Tous droits rservs

Aucune reprsentation ou reproduction, mme partielle, autre que celles prvues larticle L. 122-5 2 et 3 a) du code de la
proprit intellectuelle ne peut tre faite sans lautorisation expresse de Pearson Education France ou, le cas chant, sans
le respect des modalits prvues larticle L. 122-10 dudit code.
All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or
mechanical, including photocopying, recording or by any information storage retrieval system, without permission from
Pearson Education, Inc.
2009 Pearson Education France Les design patterns en Java Steven John Metsker & William C. Wake

pattern Livre Page III Vendredi, 9. octobre 2009 10:31 10

Table des matires


Prface ..............................................................................................................................

Conventions de codage .................................................................................................


Remerciements .............................................................................................................

1
2

Chapitre 1. Introduction .................................................................................................

Quest-ce quun pattern ? ..............................................................................................


Quest-ce quun pattern de conception ? ......................................................................
Liste des patterns dcrits dans louvrage ................................................................
Java ...............................................................................................................................
UML .............................................................................................................................
Exercices .......................................................................................................................
Organisation du livre .....................................................................................................
Oozinoz .........................................................................................................................
Rsum .........................................................................................................................

3
4
5
7
7
8
9
10
11

Partie I
Patterns dinterface
Chapitre 2. Introduction aux interfaces ........................................................................

15

Interfaces et classes abstraites .......................................................................................


Interfaces et obligations ................................................................................................
Rsum .........................................................................................................................
Au-del des interfaces ordinaires ..................................................................................

16
17
19
19

Chapitre 3. ADAPTER ....................................................................................................

21

Adaptation une interface ............................................................................................


Adaptateurs de classe et dobjet ...................................................................................

21
25

2009 Pearson Education France Les design patterns en Java Steven John Metsker & William C. Wake

pattern Livre Page IV Vendredi, 9. octobre 2009 10:31 10

IV

Table des matires

Adaptation de donnes pour un widget JTable ..........................................................

29

Identification dadaptateurs ..........................................................................................

33

Rsum .........................................................................................................................

34

Chapitre 4. FACADE .......................................................................................................

35

Faades, utilitaires et dmos .........................................................................................

36

Refactorisation pour appliquer FACADE .......................................................................

37

Rsum .........................................................................................................................

46

Chapitre 5. COMPOSITE ..............................................................................................

47

Un composite ordinaire .................................................................................................

47

Comportement rcursif dans les objets composites ......................................................

48

Objets composites, arbres et cycles ..............................................................................

50

Des composites avec des cycles ....................................................................................

55

Consquences des cycles ..............................................................................................

59

Rsum .........................................................................................................................

60

Chapitre 6. BRIDGE .......................................................................................................

61

Une abstraction ordinaire ..............................................................................................

61

De labstraction au pattern BRIDGE .............................................................................

64

Des drivers en tant que BRIDGE ...................................................................................

66

Drivers de base de donnes ...........................................................................................

67

Rsum .........................................................................................................................

69

Partie II
Patterns de responsabilit
Chapitre 7. Introduction la responsabilit .................................................................

73

Responsabilit ordinaire ...............................................................................................

73

Contrle de la responsabilit grce la visibilit .........................................................

75

Rsum .........................................................................................................................

77

Au-del de la responsabilit ordinaire ..........................................................................

77

2009 Pearson Education France Les design patterns en Java Steven John Metsker & William C. Wake

pattern Livre Page V Vendredi, 9. octobre 2009 10:31 10

Table des matires

Chapitre 8. SINGLETON ...............................................................................................

79

Le mcanisme de SINGLETON .....................................................................................


Singletons et threads .....................................................................................................
Identification de singletons ...........................................................................................
Rsum .........................................................................................................................

79
81
82
84

Chapitre 9. OBSERVER .................................................................................................

85

Un exemple classique : OBSERVER dans les interfaces utilisateurs ...............................


Modle-Vue-Contrleur ................................................................................................
Maintenance dun objet Observable .........................................................................
Rsum .........................................................................................................................

85
90
96
99

Chapitre 10. MEDIATOR ...............................................................................................

101

Un exemple classique : mdiateur de GUI ...................................................................


Mdiateur dintgrit relationnelle ...............................................................................
Rsum .........................................................................................................................

101
106
112

Chapitre 11. PROXY .......................................................................................................

115

Un exemple classique : proxy dimage .........................................................................


Reconsidration des proxies dimage ...........................................................................
Proxy distant .................................................................................................................
Proxy dynamique ..........................................................................................................
Rsum .........................................................................................................................

115
120
122
128
133

Chapitre 12. CHAIN OF RESPONSABILITY .............................................................

135

Une chane de responsabilits ordinaire .......................................................................


Refactorisation pour appliquer CHAIN OF RESPONSABILITY ...................................
Ancrage dune chane de responsabilits ......................................................................
CHAIN OF RESPONSABILITY sans COMPOSITE .........................................................
Rsum .........................................................................................................................

135
137
140
142
142

Chapitre 13. FLYWEIGHT ............................................................................................

143

Immuabilit ...................................................................................................................
Extraction de la partie immuable dun flyweight .........................................................
Partage des objets flyweight .........................................................................................
Rsum .........................................................................................................................

143
144
146
149

2009 Pearson Education France Les design patterns en Java Steven John Metsker & William C. Wake

pattern Livre Page VI Vendredi, 9. octobre 2009 10:31 10

VI

Table des matires

Partie III
Patterns de construction
Chapitre 14. Introduction la construction ..................................................................

153

Quelques dfis de construction .....................................................................................


Rsum .........................................................................................................................
Au-del de la construction ordinaire .............................................................................

153
155
155

Chapitre 15. BUILDER ...................................................................................................

157

Un objet constructeur ordinaire ....................................................................................


Construction avec des contraintes .................................................................................
Un builder tolrant ........................................................................................................
Rsum .........................................................................................................................

157
160
163
164

Chapitre 16. FACTORY METHOD ...............................................................................

165

Un exemple classique : des itrateurs ...........................................................................


Identification de FACTORY METHOD .............................................................................
Garder le contrle sur le choix de la classe instancier ...............................................
Application de FACTORY METHOD dans une hirarchie parallle .................................
Rsum .........................................................................................................................

165
166
167
169
171

Chapitre 17. ABSTRACT FACTORY ...........................................................................

173

Un exemple classique : le kit de GUI ...........................................................................


Classe FACTORY abstraite et pattern FACTORY METHOD ..............................................
Packages et classes factory abstraites ...........................................................................
Rsum .........................................................................................................................

173
178
182
182

Chapitre 18. PROTOTYPE ............................................................................................

183

Des prototypes en tant quobjets factory ......................................................................


Prototypage avec des clones .........................................................................................
Rsum .........................................................................................................................

183
185
187

Chapitre 19. MEMENTO ...............................................................................................

189

Un exemple classique : dfaire une opration ..............................................................


Dure de vie des mmentos ..........................................................................................

189
196

2009 Pearson Education France Les design patterns en Java Steven John Metsker & William C. Wake

pattern Livre Page VII Vendredi, 9. octobre 2009 10:31 10

Table des matires

VII

Persistance des mmentos entre les sessions ................................................................


Rsum .........................................................................................................................

197
200

Partie IV
Patterns dopration
Chapitre 20. Introduction aux oprations .....................................................................

203

Oprations et mthodes .................................................................................................


Signatures .....................................................................................................................
Exceptions .....................................................................................................................
Algorithmes et polymorphisme ....................................................................................
Rsum .........................................................................................................................
Au-del des oprations ordinaires ................................................................................

203
205
205
206
208
209

Chapitre 21. TEMPLATE METHOD ...........................................................................

211

Un exemple classique : algorithme de tri ......................................................................


Compltion dun algorithme .........................................................................................
Hooks ............................................................................................................................
Refactorisation pour appliquer TEMPLATE METHOD ....................................................
Rsum .........................................................................................................................

211
215
218
219
221

Chapitre 22. STATE ........................................................................................................

223

Modlisation dtats ......................................................................................................


Refactorisation pour appliquer STATE .........................................................................
Etats constants ..............................................................................................................
Rsum .........................................................................................................................

223
227
231
233

Chapitre 23. STRATEGY ...............................................................................................

235

Modlisation de stratgies ............................................................................................


Refactorisation pour appliquer STRATEGY ...................................................................
Comparaison de STRATEGY et STATE ..........................................................................
Comparaison de STRATEGY et TEMPLATE METHOD .....................................................
Rsum .........................................................................................................................

236
238
242
243
243

2009 Pearson Education France Les design patterns en Java Steven John Metsker & William C. Wake

pattern Livre Page VIII Vendredi, 9. octobre 2009 10:31 10

VIII

Table des matires

Chapitre 24. COMMAND ...............................................................................................


Un exemple classique : commandes de menus .............................................................
Emploi de COMMAND pour fournir un service ................................................................
Hooks ............................................................................................................................
COMMAND en relation avec dautres patterns ..................................................................
Rsum .........................................................................................................................

245
245
248
249
251
252

Chapitre 25. INTERPRETER ........................................................................................


Un exemple de INTERPRETER .....................................................................................
Interprteurs, langages et analyseurs syntaxiques ........................................................
Rsum .........................................................................................................................

253
254
265
266

Partie V
Patterns dextension
Chapitre 26. Introduction aux extensions .....................................................................
Principes de la conception oriente objet .....................................................................
Le principe de substitution de Liskov ...........................................................................
La loi de Demeter .........................................................................................................
Elimination des erreurs potentielles ..............................................................................
Au-del des extensions ordinaires ................................................................................
Rsum .........................................................................................................................

269
269
270
271
273
273
274

Chapitre 27. DECORATOR ...........................................................................................


Un exemple classique : flux dE/S et objets Writer ...................................................
Enveloppeurs de fonctions ............................................................................................
DECORATOR en relation avec dautres patterns ..............................................................
Rsum .........................................................................................................................

277
277
285
292
293

Chapitre 28. ITERATOR ................................................................................................


Itration ordinaire .........................................................................................................
Itration avec scurit inter-threads ..............................................................................
Itration sur un objet composite ...................................................................................
Ajout dun niveau de profondeur un numrateur ...............................................
Enumration des feuilles .........................................................................................
Rsum .........................................................................................................................

295
295
297
303
310
311
313

2009 Pearson Education France Les design patterns en Java Steven John Metsker & William C. Wake

pattern Livre Page IX Vendredi, 9. octobre 2009 10:31 10

Table des matires

IX

Chapitre 29. VISITOR ....................................................................................................

315

Application de VISITOR ..............................................................................................


Un VISITOR ordinaire ..................................................................................................
Cycles et VISITOR .......................................................................................................
Risques de VISITOR ....................................................................................................
Rsum .........................................................................................................................

315
318
323
328
330

Partie VI
Annexes
Annexe A. Recommandations ........................................................................................

333

Tirer le meilleur parti du livre .......................................................................................


Connatre ses classiques ...............................................................................................
Appliquer les patterns ...................................................................................................
Continuer dapprendre ..................................................................................................

333
334
334
336

Annexe B. Solutions .........................................................................................................

337

Introduction aux interfaces ...........................................................................................


Solution 2.1 .............................................................................................................
Solution 2.2 .............................................................................................................
Solution 2.3 .............................................................................................................
ADAPTER ....................................................................................................................
Solution 3.1 .............................................................................................................
Solution 3.2 .............................................................................................................
Solution 3.3 .............................................................................................................
Solution 3.4 .............................................................................................................
Solution 3.5 .............................................................................................................
Solution 3.6 .............................................................................................................
FACADE .......................................................................................................................
Solution 4.1 .............................................................................................................
Solution 4.2 .............................................................................................................
Solution 4.3 .............................................................................................................
Solution 4.4 .............................................................................................................

337
337
338
338
338
338
339
340
341
341
342
342
342
343
343
344

2009 Pearson Education France Les design patterns en Java Steven John Metsker & William C. Wake

pattern Livre Page X Vendredi, 9. octobre 2009 10:31 10

Table des matires

COMPOSITE ................................................................................................................
Solution 5.1 .............................................................................................................
Solution 5.2 .............................................................................................................
Solution 5.3 .............................................................................................................
Solution 5.4 .............................................................................................................
Solution 5.5 .............................................................................................................
Solution 5.6 .............................................................................................................
BRIDGE .......................................................................................................................
Solution 6.1 .............................................................................................................
Solution 6.2 .............................................................................................................
Solution 6.3 .............................................................................................................
Solution 6.4 .............................................................................................................
Solution 6.5 .............................................................................................................
Introduction la responsabilit .....................................................................................
Solution 7.1 .............................................................................................................
Solution 7.2 .............................................................................................................
Solution 7.3 .............................................................................................................
Solution 7.4 .............................................................................................................
SINGLETON ................................................................................................................
Solution 8.1 .............................................................................................................
Solution 8.2 .............................................................................................................
Solution 8.3 .............................................................................................................
Solution 8.4 ............................................................................................................
OBSERVER ..................................................................................................................
Solution 9.1 .............................................................................................................
Solution 9.2 .............................................................................................................
Solution 9.3 .............................................................................................................
Solution 9.4 .............................................................................................................
Solution 9.5 .............................................................................................................
Solution 9.6 .............................................................................................................
Solution 9.7 .............................................................................................................
MEDIATOR ..................................................................................................................
Solution 10.1 ...........................................................................................................
Solution 10.2 ...........................................................................................................
Solution 10.3 ...........................................................................................................
Solution 10.4 ...........................................................................................................
Solution 10.5 ...........................................................................................................

345
345
346
346
347
347
348
348
348
348
349
349
350
350
350
351
352
353
353
353
353
353
354
354
354
355
356
356
357
357
358
359
359
360
361
361
362

2009 Pearson Education France Les design patterns en Java Steven John Metsker & William C. Wake

pattern Livre Page XI Vendredi, 9. octobre 2009 10:31 10

Table des matires

XI

PROXY .........................................................................................................................
Solution 11.1 ...........................................................................................................
Solution 11.2 ...........................................................................................................
Solution 11.3 ...........................................................................................................
Solution 11.4 ...........................................................................................................
Solution 11.5 ...........................................................................................................
CHAIN OF RESPONSABILITY .................................................................................
Solution 12.1 ...........................................................................................................
Solution 12.2 ...........................................................................................................
Solution 12.3 ...........................................................................................................
Solution 12.4 ...........................................................................................................
Solution 12.5 ...........................................................................................................
FLYWEIGHT ...............................................................................................................
Solution 13.1 ...........................................................................................................
Solution 13.2 ...........................................................................................................
Solution 13.3 ...........................................................................................................
Solution 13.4 ...........................................................................................................
Introduction la construction .......................................................................................
Solution 14.1 ...........................................................................................................
Solution 14.2 ...........................................................................................................
Solution 14.3 ...........................................................................................................
BUILDER .....................................................................................................................
Solution 15.1 ...........................................................................................................
Solution 15.2 ...........................................................................................................
Solution 15.3 ...........................................................................................................
Solution 15.4 ...........................................................................................................
FACTORY METHOD ..................................................................................................
Solution 16.1 ...........................................................................................................
Solution 16.2 ...........................................................................................................
Solution 16.3 ...........................................................................................................
Solution 16.4 ...........................................................................................................
Solution 16.5 ...........................................................................................................
Solution 16.6 ...........................................................................................................
Solution 16.7 ...........................................................................................................
ABSTRACT FACTORY ...............................................................................................
Solution 17.1 ...........................................................................................................
Solution 17.2 ...........................................................................................................

362
362
363
363
363
364
364
364
365
366
366
367
368
368
369
370
370
371
371
372
372
373
373
373
374
374
375
375
376
376
376
377
378
378
379
379
380

2009 Pearson Education France Les design patterns en Java Steven John Metsker & William C. Wake

pattern Livre Page XII Vendredi, 9. octobre 2009 10:31 10

XII

Table des matires

Solution 17.3 ...........................................................................................................


Solution 17.4 ...........................................................................................................
Solution 17.5 ...........................................................................................................
PROTOTYPE ................................................................................................................
Solution 18.1 ...........................................................................................................
Solution 18.2 ...........................................................................................................
Solution 18.3 ...........................................................................................................
Solution 18.4 ...........................................................................................................
MEMENTO ..................................................................................................................
Solution 19.1 ...........................................................................................................
Solution 19.2 ...........................................................................................................
Solution 19.3 ...........................................................................................................
Solution 19.4 ...........................................................................................................
Solution 19.5 ...........................................................................................................
Introduction aux oprations ..........................................................................................
Solution 20.1 ...........................................................................................................
Solution 20.2 ...........................................................................................................
Solution 20.3 ...........................................................................................................
Solution 20.4 ...........................................................................................................
Solution 20.5 ...........................................................................................................
TEMPLATE METHOD ................................................................................................
Solution 21.1 ...........................................................................................................
Solution 21.2 ...........................................................................................................
Solution 21.3 ...........................................................................................................
Solution 21.4 ...........................................................................................................
STATE ...........................................................................................................................
Solution 22.1 ...........................................................................................................
Solution 22.2 ...........................................................................................................
Solution 22.3 ...........................................................................................................
Solution 22.4 ...........................................................................................................
STRATEGY ..................................................................................................................
Solution 23.1 ...........................................................................................................
Solution 23.2 ...........................................................................................................
Solution 23.3 ...........................................................................................................
Solution 23.4 ...........................................................................................................

380
381
381
382
382
383
383
384
384
384
385
385
386
386
387
387
387
388
388
388
389
389
389
390
390
390
390
390
391
391
392
392
392
392
393

2009 Pearson Education France Les design patterns en Java Steven John Metsker & William C. Wake

pattern Livre Page XIII Vendredi, 9. octobre 2009 10:31 10

Table des matires

XIII

COMMAND .................................................................................................................
Solution 24.1 ...........................................................................................................
Solution 24.2 ...........................................................................................................
Solution 24.3 ...........................................................................................................
Solution 24.4 ...........................................................................................................
Solution 24.5 ...........................................................................................................
Solution 24.6 ...........................................................................................................
INTERPRETER ............................................................................................................
Solution 25.1 396
Solution 25.2 ...........................................................................................................
Solution 25.3 ...........................................................................................................
Solution 25.4 ...........................................................................................................
Introduction aux extensions ..........................................................................................
Solution 26.1 398
Solution 26.2 ...........................................................................................................
Solution 26.3 ...........................................................................................................
Solution 26.4 ...........................................................................................................
DECORATOR ..............................................................................................................
Solution 27.1 399
Solution 27.2 ...........................................................................................................
Solution 27.3 ...........................................................................................................
Solution 27.4 ...........................................................................................................
ITERATOR ...................................................................................................................
Solution 28.1 401
Solution 28.2 ...........................................................................................................
Solution 28.3 ...........................................................................................................
Solution 28.4 ...........................................................................................................
VISITOR .......................................................................................................................
Solution 29.1 403
Solution 29.2 ...........................................................................................................
Solution 29.3 ...........................................................................................................
Solution 29.4 ...........................................................................................................
Solution 29.5 ...........................................................................................................

393
393
393
395
395
396
396
396

Annexe C. Code source dOozinoz ...............................................................................


Obtention et utilisation du code source ........................................................................
Construction du code dOozinoz ..................................................................................

405
405
406

397
397
397
398
398
398
399
399
400
401
401
401
402
402
402
403
403
403
404
404

2009 Pearson Education France Les design patterns en Java Steven John Metsker & William C. Wake

pattern Livre Page XIV Vendredi, 9. octobre 2009 10:31 10

XIV

Table des matires

Test du code avec JUnit ................................................................................................


Localiser les fichiers .....................................................................................................
Rsum .........................................................................................................................

406
406
407

Annexe D. Introduction UML .....................................................................................


Classes ..........................................................................................................................
Relations entre classes ..................................................................................................
Interfaces .......................................................................................................................
Objets ............................................................................................................................
Etats ..............................................................................................................................

409
409
412
414
414
416

Glossaire ............................................................................................................................

417

Bibliographie .....................................................................................................................

425

Index ..................................................................................................................................

427

2009 Pearson Education France Les design patterns en Java Steven John Metsker & William C. Wake

Вам также может понравиться