Schweitzer Fachinformationen
Wenn es um professionelles Wissen geht, ist Schweitzer Fachinformationen wegweisend. Kunden aus Recht und Beratung sowie Unternehmen, öffentliche Verwaltungen und Bibliotheken erhalten komplette Lösungen zum Beschaffen, Verwalten und Nutzen von digitalen und gedruckten Medien.
PREFACE xiv
ACKNOWLEDGMENTS xix
1 Why Learn Design Patterns and Why Do So with Help from Harry Potter? 1
1.1 The OO Design Patterns "Bible" by GoF 2
1.2 But What Has Harry Potter Got to Do with OO Design Patterns? 4
1.3 Is Familiarity with Harry Potter a Requirement for Understanding This Book? 6
1.4 How the Pattern Explanations are Organized 7
1.5 The Terminology of Object-Oriented Programming 7
1.6 The UML Notation Used in the Class Diagrams 12
1.6.1 Association as a Relationship Between Classes 14
1.6.2 Aggregation and Composition as Relationships Between Classes 15
1.6.3 Representing Attributes 16
1.6.4 Representing Operations 17
I CREATIONAL PATTERNS 19
2 Abstract Factory 21
2.1 The Concept of a Factory in Software 21
2.2 Intent and Applicability 22
2.3 Introduction to the Abstract Factory Pattern 22
2.4 The Abstract Factory Pattern in Real-World Applications 23
2.5 Harry Potter Story Used to Illustrate the Abstract Factory Pattern 23
2.6 A Top Level View of the Pattern Demonstration 25
2.7 The Abstract Class Factory 27
2.8 The Helper Class FactoryStore 27
2.9 The Abstract Class Enchanted 30
2.10 The Concrete Classes for Magical Objects 31
2.11 The Concrete Factory Classes 32
2.12 The Client Class Diagon AlleyRetailer 34
2.13 Playing with the Code 36
3 Builder 38
3.1 Building Complex Objects 38
3.2 Intent and Applicability 39
3.3 Introduction to the Builder Pattern 39
3.4 The Builder Pattern in Real-World Applications 41
3.5 Harry Potter Story Used to Illustrate the Builder Pattern 42
3.6 A Top-Level View of the Pattern Demonstration 43
3.7 The Abstract Class PotionMaker 45
3.8 The Concrete Extensions of PotionMaker 46
3.9 The Director Class 52
3.10 The Potion Class 54
3.11 The Ingredient Class 55
3.12 The PotionMakingFeasibilityViolation Class 55
3.13 The Client Class 55
3.14 Playing with the Code 56
4 Factory Method 59
4.1 Revisiting the Concept of a Factory in Software 59
4.2 Intent and Applicability 60
4.3 Introduction to the Factory Method Pattern 60
4.4 The Factory Method Pattern in Real-World Applications 61
4.5 Harry Potter Story Used to Illustrate the Factory Method Pattern 62
4.6 A Top Level View of the Pattern Demonstration 63
4.7 The Enchanted Class Hierarchy 65
4.8 The ArtifactFactory Class Hierarchy and the Factory Methods Contained Therein 68
4.9 The Client Class 70
4.10 Playing with the Code 71
5 Prototype 73
5.1 Why Not Make New Objects by Copying Old Objects? 73
5.2 Intent and Applicability 74
5.3 Introduction to the Prototype Pattern 74
5.4 The Prototype Pattern in Real-World Applications 77
5.5 Harry Potter Story Used to Illustrate The Prototype Pattern 78
5.6 A Top Level View of the Pattern Demonstration 79
5.7 The Dragon Class 81
5.8 The PrototypeManagerAndDuplicator Class 84
5.9 The DragonAficionado Class 87
5.10 The UnknownDragonException Class 88
5.11 Playing with the Code 88
6 Singleton 90
6.1 Singular Objects 90
6.2 Intent and Applicability 90
6.3 Introduction to the Singleton Pattern 91
6.4 The Singleton Pattern in Real-World Applications 91
6.5 Harry Potter Story Used to Illustrate the Singleton Pattern 92
6.6 A Top Level View of the Pattern Demonstration 92
6.7 The MinisterForMagic Class 93
6.8 The TestSingleton Class 94
6.9 Variations on the Singleton Pattern 95
6.10 Playing with the Code 97
II STRUCTURAL PATTERNS 99
7 Adapter 101
7.1 Getting Things to Work Together 101
7.2 Intent and Applicability 102
7.3 Introduction to the Adapter Pattern 102
7.4 The Adapter Pattern in Real-World Applications 103
7.5 Harry Potter Story Used to Illustrate the Adapter Pattern 104
7.6 A Top Level View of the Pattern Demonstration 104
7.7 The Target Interface: TeachingDADA 106
7.8 The TeacherForDADA Class 107
7.9 The AdapterForSafeTeaching Class 108
7.10 The Client Class SchoolOfMagic 110
7.11 Object Adapter 111
7.12 Pluggable Adapter 113
7.13 Playing with the Code 119
8 Bridge 122
8.1 Concepts and Their Implementation 122
8.2 Intent and Applicability 123
8.3 Introduction to the Bridge Pattern 123
8.4 The Bridge Pattern in Real-World Applications 126
8.5 Harry Potter Story Used to Illustrate the Bridge Pattern 127
8.6 A Top Level View of the Pattern Demonstration 130
8.7 The Humanoid Class 130
8.8 The Dementor, Goblin, and HouseElf Classes 132
8.9 The Humanoid Imp Class 134
8.10 Implementation Classes for the Dementor, Goblin, and HouseElf Classes 137
8.11 The Client Class MinistryOfMagic 139
8.12 Playing with the Code 140
9 Composite 142
9.1 Relationships That Loop Back 142
9.2 Intent and Applicability 143
9.3 Introduction to the Composite Pattern 144
9.4 The Composite Pattern in Real-World Applications 145
9.5 Harry Potter Story Used to Illustrate the Composite Pattern 146
9.6 A Top Level View of the Pattern Demonstration 147
9.7 The WizardTraits Interface 148
9.8 The Wizard Class 149
9.9 The Auror Class 152
9.10 The Obliviator Class 153
9.11 The DepartmentHead Class 155
9.12 The Minister for Magic Class 156
9.13 The Client Class Test 158
9.14 Playing with the Code 159
10 Decorator 161
10.1 Onion as a Metaphor 161
10.2 Intent and Applicability 162
10.3 Introduction to the Decorator Pattern 162
10.4 The Decorator Pattern in Real-World Applications 164
10.5 Harry Potter Story Used to Illustrate the Decorator Pattern 165
10.6 A Top Level View of the Pattern Demonstration 168
10.7 The CoreMessageDeliveryClass Class 170
10.8 The Decorator Classes 170
10.9 The Client Class Test 172
10.10 Playing with the Code 173
11 Facade 175
11.1 Hiding Complexity 175
11.2 Intent and Applicability 175
11.3 Introduction to the Facade Pattern 176
11.4 The Facade Pattern in Real-World Applications 177
11.5 Harry Potter Story Used to Illustrate the Facade Pattern 177
11.6 A Top Level View of the Pattern Demonstration 179
11.7 The Abstract Root Class Facade 181
11.8 The Network Class 183
11.9 The Node, Link, and Path Classes 193
11.10 The Three Concrete Facade Classes 198
11.11 Testing the Demonstration Code 203
11.12 Playing with the Code 207
12 Flyweight 212
12.1 The Idea of Customized Duplications 212
12.2 Intent and Applicability 213
12.3 Introduction to the Flyweight Pattern 213
12.4 The Flyweight Pattern in Real-World Applications 214
12.5 Harry Potter Story Used to Illustrate the Flyweight Pattern 214
12.6 A Top Level View of the Pattern Demonstration 215
12.7 The HeadMasterPortrait Class 218
12.8 The FlyweightImageStore Class 218
12.9 The ImageManager Class 219
12.10 The PortraitBorderChoices Class 225
12.11 The ImageNotAvailableException Class 226
12.12 The PortraitMakerAssignment Class 226
12.13 Playing with the Code 228
12.14 Acknowledgment 229
13 Proxy 230
13.1 Is It Always Necessary to Have the Real Thing? 230
13.2 Intent and Applicability 230
13.3 Introduction to the Proxy Pattern 231
13.4 The Proxy Pattern in Real-World Applications 232
13.5 Harry Potter Story Used to Illustrate the Proxy Pattern 233
13.6 A Top Level View of the Pattern Demonstration 235
13.7 The Abstract Class Wizard 235
13.8 The DarkWizardTraits Interface 237
13.9 The DarkWizard Class 238
13.10 The DarkLord Class 242
13.11 The ClientClass Class 245
13.12 Playing with the Code 246
III BEHAVIORAL PATTERNS 249
14 Chain of Responsibility 253
14.1 Passing the Buck 253
14.2 Intent and Applicability 254
14.3 Introduction to the Chain of Responsibility Pattern 254
14.4 The Chain of Responsibility Pattern in Real-World Applications 255
14.5 Harry Potter Story Used to Illustrate the Chain of Responsibility Pattern 256
14.6 A Top Level View of the Pattern Demonstration 258
14.7 The Interface Class Violations 260
14.8 The Abstract Class Adjudicator 261
14.9 The Player Class 263
14.10 The Classes with the Authority to Resolve Violations 265
14.11 Testing the Code 268
14.12 Playing with the Code 269
15 Command 272
15.1 Actions Versus the Actors 272
15.2 Intent and Applicability 273
15.3 Introduction to the Command Pattern 274
15.4 The Command Pattern in Real-World Applications 274
15.5 Harry Potter Story Used to Illustrate the Command Pattern 276
15.6 A Top Level View of the Pattern Demonstration 277
15.7 The Command Interface 279
15.8 The MyPlaces Interface 279
15.9 The ProtectHarryPotter Class 279
15.10 The Wizard Class 282
15.11 The Squib Class 284
15.12 The Invoker Class 285
15.13 The UnableToProtectHarryPotterException Class 286
15.14 The Client Class 286
15.15 Playing with the Code 288
16 Interpreter 290
16.1 Parsing versus Interpretation 290
16.2 Intent and Applicability 292
16.3 Introduction to the Interpreter Pattern 292
16.4 The Interpreter Pattern in Real-World Applications 293
16.5 Harry Potter Story Used to Illustrate the Interpreter Pattern 294
16.6 A Parser Front-End for the Interpreter Pattern 296
16.7 A Top Level View of the Pattern Demonstration 304
16.8 The Driver Class 307
16.9 The Interpreter Sentence Class 310
16.10 The Worker Classes for Interpretation 312
16.11 The Utility Class ShowSyntaxTree 315
16.12 Playing with the Code 316
17 Iterator 321
17.1 Storing Object Collections and Interacting with Them 321
17.2 Intent and Applicability 323
17.3 Introduction to the Iterator Pattern 323
17.4 The Iterator Pattern in Real-World Applications 324
17.5 Harry Potter Story Used to Illustrate the Iterator Pattern 325
17.6 A Top Level View of the Pattern Demonstration 326
17.7 A Utility Interface for Demonstrating the Iterator Pattern 328
17.8 The Iterator Interface 330
17.9 The Fresher Class 330
17.10 The SortingHat Class 334
17.11 The MagicCollection Interface 344
17.12 The MagicList and MagicSet Classes 344
17.13 The Class Range 349
17.14 Playing with the Code 349
17.15 Credits 352
18 Mediator 353
18.1 The Role of Mediation in Collaborative Problem Solving 353
18.2 Intent and Applicability 354
18.3 Introduction to the Mediator Pattern 354
18.4 The Mediator Pattern in Real-World Applications 355
18.5 Harry Potter Story Used to Illustrate the Mediator Pattern 357
18.6 A Top Level View of the Pattern Demonstration 357
18.7 The Abstract Class Mediator 359
18.8 The Interface TrialElements 360
18.9 The MinistryOfMagicTrialMediator Class 361
18.10 The Witness Class 366
18.11 The Trial Interface 367
18.12 The HarryPotterTrial Class 367
18.13 Playing with the Code 369
19 Memento 371
19.1 Recalling the Past 371
19.2 Intent and Applicability 372
19.3 Introduction to the Memento Pattern 372
19.4 The Memento Pattern in Real-World Applications 374
19.5 Harry Potter Story Used to Illustrate the Memento Pattern 374
19.6 A Top Level View of the Pattern Demonstration 377
19.7 The HogwartsHappening Class and The Inner Memento Class 378
19.8 The Client Class 380
19.9 Playing with the Code 384
20 Observer 386
20.1 Subscription-Based Broadcasting 386
20.2 Intent and Applicability 387
20.3 Introduction to the Observer Pattern 387
20.4 The Observer Pattern in Real-World Applications 388
20.5 Harry Potter Story Used to Illustrate the Observer Pattern 389
20.6 A Top Level View of the Pattern Demonstration 391
20.7 The Observer Interface 391
20.8 The Observable Interface 393
20.9 The DarkLord Class 393
20.10 The DeathEater Class 396
20.11 The GodProcess Class 399
20.12 Playing with the Code 401
21 State 405
21.1 Contextual Dependence of Behaviors 405
21.2 Intent and Applicability 406
21.3 Introduction to the State Pattern 406
21.4 The State Pattern in Real-World Applications 406
21.5 Harry Potter Story Used to Illustrate the State Pattern 407
21.6 A Top Level View of the Pattern Demonstration 408
21.7 The DADA State Interface 410
21.8 The Year-by-Year Implementation Classes for the State 411
21.9 The TeachingDADA Class 417
21.10 The Hogwarts Class 418
21.11 Playing with the Code 419
22 Strategy 421
22.1 Strategies in the Pursuit of Goals 421
22.2 Intent and Applicability 422
22.3 Introduction to the Strategy Pattern 422
22.4 The Strategy Pattern in Real-World Applications 423
22.5 Harry Potter Story Used to Illustrate the Strategy Pattern 424
22.6 A Top Level View of the Pattern Demonstration 425
22.7 The Abstract Root Class for Strategies: StrategyAbstractRoot 428
22.8 The Concrete Strategy Classes 429
22.9 The Champion Class 432
22.10 The SecondTaskManager Class 434
22.11 Playing with the Code 436
23 Template Method 437
23.1 Customizable Recipes 437
23.2 Intent and Applicability 438
23.3 Introduction to the Template Method Pattern 438
23.4 The Template Method Pattern in Real-World Applications 440
23.5 Harry Potter Story Used to Illustrate the Template Method Pattern 441
23.6 A Top Level View of the Pattern Demonstration 442
23.7 The Abstract Root of Narrative Generation Classes 444
23.8 Concrete Classes for Narrative Generation 445
23.9 The Executable Class 449
23.10 Playing with the Code 450
24 Visitor 453
24.1 Hooks, Good and Evil 453
24.2 Intent and Applicability 454
24.3 Introduction to the Visitor Pattern 454
24.4 The Visitor Pattern in Real-World Applications 457
24.5 Harry Potter Story Used to Illustrate the Visitor Pattern 457
24.6 A Top Level View of the Pattern Demonstration 459
24.7 The Visitor Interface 460
24.8 Two Concrete Implementations of the Visitor Interface 462
24.9 A Re-Implementation of the Wizard Hierarchy of the Composite Pattern 463
24.10 The Executable Class Client 469
24.11 Playing with the Code 470
REFERENCES 473
INDEX 474
Readers who have been following the progress of my Objects Trilogy project will be pleased to see this third and final book of the Trilogy - even if it is almost two years behind schedule.
As to the cause for the delay, the primary culprit was finding the right medium for explaining the design patterns. When I first announced my Objects Trilogy project several years back, my plan, as stated then, was to explain the patterns through famous short stories of the world. By famous I meant classic and universal - the sort that would not need to be re-told when used for explaining the patterns. These would be the stories by Hans Christian Anderson, the Brothers Grimm, and their counterparts from around the world. But, unfortunately, when I started mapping the patterns onto the stories, it became increasingly clear that such stories did not possess the level of complexity that was needed for the patterns. That's when I turned to Harry Potter.
I should also mention that, at the beginning of this book project, Harry Potter was the farthest thing from my mind. Not that I was unaware of the worldwide phenomenon that Harry Potter had become. After all, who could have missed media reports like the mile-long lines of kids and parents waiting for the stores to open with the release of each new Harry Potter book. But to me, having grown up with time-honored and celebrated short stories of the sort mentioned above, it seemed unnecessary to have to wade though a very long story (seven books) in order to explore its possibilities for explaining the design patterns.
However, after realizing the limitations of traditional folklore and fairy tales for this project, I turned to Harry Potter with a greater seriousness of purpose. As I started reading the series, I was completely blown away not only by the story-telling powers of J. K. Rowling, but also by the delightful complexity of the subnarratives within the overall multi-volume story. As I wended my way though successive Harry Potter books, every once in a while I'd have an "Aha!" moment when I encountered a segment of the story that appeared to have the complexity I was looking for.1 Despite numerous such moments, it still took multiple readings of the series - besides conversations with family and friends and, especially, the children of family and friends who had devoured Harry Potter many times over - before I had any confidence in using a particular part of the story for explaining a pattern.
I would not be surprised if the initial reaction of a reader to my use of Harry Potter for explaining the patterns would be something like: "Where is the need for using a literary medium of any sort for such explanations?" Such readers are likely to add: "If the current explanations of the patterns are not satisfactory, why not base any further explanations on real-world examples as opposed to stories from a magical world?" I am going to address these questions next.
In addition to the seminal book "Design Patterns - Elements of Reusable Object-Oriented Software" by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides that first introduced the patterns to the programming world, there now exist virtually hundreds of explanations of these patterns in various books and on the internet. I maintain that the large quantity of this material has not made it any easier to understand many of the more difficult patterns. Rather, the more difficult patterns continue to be as opaque today as they were at the time of their first presentation by Gamma et al. And the fact that they were quite difficult to understand even through the original explanations provided by Gamma et al. can be inferred from the following comment in the Preface of their book: "A word of warning and encouragement: Don't worry if you don't understand this book completely on the first reading. We didn't understand it all on the first writing!"
Let me now address the issue of using real-world examples to improve upon the current stock of explanations of the patterns. Note that for a large majority of the explanations that are currently out there, the authors have indeed tried to use real-world examples. But these explanations do not work well for various reasons. The main reason, probably, is that the typical space constraints prevent the authors from fully presenting a real-world application.2 As such, what a reader gets is a highly abbreviated version of some realworld example that is often difficult to appreciate from the standpoint of its relevance to the pattern. Additionally, a reader may have zero interest in the real-world application being used by an author. If for whatever reason a reader cannot connect with the so-called realworld examples, why not use examples from a familiar literary work in the hope that the reader would find those easier to relate to.
Based on my own experience with the teaching of object-oriented (OO) design patterns, I have observed that when a student tries to grasp the software abstraction in a pattern through an instructor's highly abbreviated presentation of some real-world application that fails to motivate, what you get is an immediate disconnect between the student and the instructor. And if it should happen that a pattern explanation is based on an application that the instructor is not familiar with, the student is likely to receive a presentation that lacks clarity and conviction.
I believe it's due to a lack of any explanatory material that can hold the attention of the students and that, at the same time, lends itself to teaching without placing a large burden on the instructors that the OO patterns, despite their great importance, are taught mostly in a piecemeal fashion. In most cases, a software engineering instructor teaches just a half dozen patterns that he/she is comfortable with. It's rarely the case that all of the patterns are taught in any concerted fashion.
I must emphasize that not all patterns are difficult to understand. In fact, several that deal with object creation are downright easy and can be comprehended immediately with the simplest of explanations. But those patterns that involve fairly complex run-time interactions between different objects can require significant effort before they begin to sink in. It is these more complex patterns that Gamma et al. were surely referring to in the quote from their preface that I showed earlier. The goal of my book is to make it easier to understand these patterns in particular with the help of stories drawn from Harry Potter.
It is possible that some of you are thinking that Harry Potter is already passé - since it is not as constantly in the news as it used to be - so I might be hitching my wagon to a fading star for explaining the patterns. Yes, it does appear that the extremely intense media hype that surrounded Harry Potter for several years has passed. But that's because nothing lasts forever in the media, and that applies as much to themes that are merely ephemeral as it does to themes that are timeless. I still see a lot of young people reading and enjoying Harry Potter. My sense is that Harry Potter is as timeless as, say, Dr. Seuss. Neither receives much media attention any longer, but both continue to be widely enjoyed around the world.
I am obviously under no illusion that all readers who have had difficulty understanding the more complex patterns with the currently available crop of explanations will suddenly have "Aha!" moments when they read my Harry Potter-based explanations for the same. I am also reasonably certain I'll be at the receiving end of brickbats for melding the serious subject of OO design with the airy material of Harry Potter. Nonetheless, at the very least, this book will demonstrate that it is possible to construct explanatory analogies for OO design patterns using themes that we resonate to emotionally (through our fantasies or otherwise). Even if this book only encouraged other authors to write books in the future that use unconventional analogies - analogies that we can relate to on the basis of our shared human experiences - for explaining ideas that are generally considered to be complex and dry, I'd be satisfied.
That brings me to the subject of the Harry Potter stories I have used in this book. If you have read the series, you probably have your own favorite stories. As to what those might be is likely to depend on several factors, including possibly your educational background and cultural perspectives. However, if you are of a scientific bent of mind, it is difficult for me to imagine that the stories related to the following magical objects did not leave indelible impressions on your mind: the Sorting Hat for placing each year's fresh crop of admittees in the four Hogwarts houses; the Remembrall as a memory aid; the Time-Turner for time travel; the Howlers for conveying emotions in the strongest possible sense; the Floo network for quick travel around London; the Death Mark for instantaneous signaling; and several others. What these stories convey is not too far from our own fantasies that are borne partly of our scientific exuberance and partly from our desires to cope with everyday life and all its frustrations. I have used several of these stories in the explanations of the patterns in this book.
With regard to the organization of the book, it consists of three parts: the first is devoted to Creational Patterns, the second to Structural Patterns, and the last to Behavioral Patterns. Within each part, each chapter is devoted to a...
Dateiformat: ePUBKopierschutz: Adobe-DRM (Digital Rights Management)
Systemvoraussetzungen:
Das Dateiformat ePUB ist sehr gut für Romane und Sachbücher geeignet – also für „fließenden” Text ohne komplexes Layout. Bei E-Readern oder Smartphones passt sich der Zeilen- und Seitenumbruch automatisch den kleinen Displays an. Mit Adobe-DRM wird hier ein „harter” Kopierschutz verwendet. Wenn die notwendigen Voraussetzungen nicht vorliegen, können Sie das E-Book leider nicht öffnen. Daher müssen Sie bereits vor dem Download Ihre Lese-Hardware vorbereiten.Bitte beachten Sie: Wir empfehlen Ihnen unbedingt nach Installation der Lese-Software diese mit Ihrer persönlichen Adobe-ID zu autorisieren!
Weitere Informationen finden Sie in unserer E-Book Hilfe.