Here is a book that takes the sting out of learning object-oriented design patterns! Using vignettes from the fictional world of Harry Potter, author Avinash C. Kak provides a refreshing alternative to the typically abstract and dry object-oriented design literature.
Designing with Objects is unique. It explains design patterns using the short-story medium instead of sterile examples. It is the third volume in a trilogy by Avinash C. Kak, following Programming with Objects (Wiley, 2003) and Scripting with Objects (Wiley, 2008). Designing with Objects confronts how difficult it is for students to learn complex patterns based on conventional scenarios that they may not be able to relate to. In contrast, it shows that stories from the fictional world of Harry Potter provide highly relatable and engaging models. After explaining core notions in a pattern and its typical use in real-world applications, each chapter shows how a pattern can be mapped to a Harry Potter story. The next step is an explanation of the pattern through its Java implementation. The following patterns appear in three sections: Abstract Factory, Builder, Factory Method, Prototype, and Singleton; Adapter, Bridge, Composite, Decorator, Facade, Flyweight, and Proxy; and the Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, and Visitor. For readers’ use, Java code for each pattern is included in the book’s companion website.
- All code examples in the book are available for download on a companion website with resources for readers and instructors.
- A refreshing alternative to the abstract and dry explanations of the object-oriented design patterns in much of the existing literature on the subject.
- In 24 chapters, Designing with Objects explains well-known design patterns by relating them to stories from the fictional Harry Potter series
表中的内容
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 Go F 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 Factory Store 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 Alley Retailer 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 Potion Maker 45
3.8 The Concrete Extensions of Potion Maker 46
3.9 The Director Class 52
3.10 The Potion Class 54
3.11 The Ingredient Class 55
3.12 The Potion Making Feasibility Violation 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 Artifact Factory 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 Prototype Manager And Duplicator Class 84
5.9 The Dragon Aficionado Class 87
5.10 The Unknown Dragon Exception 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 Minister For Magic Class 93
6.8 The Test Singleton 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: Teaching DADA 106
7.8 The Teacher For DADA Class 107
7.9 The Adapter For Safe Teaching Class 108
7.10 The Client Class School Of Magic 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 House Elf Classes 132
8.9 The Humanoid Imp Class 134
8.10 Implementation Classes for the Dementor, Goblin, and House Elf Classes 137
8.11 The Client Class Ministry Of Magic 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 Wizard Traits Interface 148
9.8 The Wizard Class 149
9.9 The Auror Class 152
9.10 The Obliviator Class 153
9.11 The Department Head 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 Core Message Delivery Class 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 Head Master Portrait Class 218
12.8 The Flyweight Image Store Class 218
12.9 The Image Manager Class 219
12.10 The Portrait Border Choices Class 225
12.11 The Image Not Available Exception Class 226
12.12 The Portrait Maker Assignment 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 Dark Wizard Traits Interface 237
13.9 The Dark Wizard Class 238
13.10 The Dark Lord Class 242
13.11 The Client Class 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 My Places Interface 279
15.9 The Protect Harry Potter Class 279
15.10 The Wizard Class 282
15.11 The Squib Class 284
15.12 The Invoker Class 285
15.13 The Unable To Protect Harry Potter Exception 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 Show Syntax Tree 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 Sorting Hat Class 334
17.11 The Magic Collection Interface 344
17.12 The Magic List and Magic Set 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 Trial Elements 360
18.9 The Ministry Of Magic Trial Mediator Class 361
18.10 The Witness Class 366
18.11 The Trial Interface 367
18.12 The Harry Potter Trial 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 Hogwarts Happening 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 Dark Lord Class 393
20.10 The Death Eater Class 396
20.11 The God Process 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 Teaching DADA 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: Strategy Abstract Root 428
22.8 The Concrete Strategy Classes 429
22.9 The Champion Class 432
22.10 The Second Task Manager 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
关于作者
Avinash C. Kak is Professor of Electrical and Computer Engineering at Purdue University. His areas of interest include software engineering, computer and network security, sensor networks, and computer vision (especially with camera networks). Professor Kak has been awarded numerous Best Teacher of the Year awards at Purdue University. He is the author of Scripting with Objects and Programming with Objects, both published by Wiley.