Author(s): William Sanders
Publisher: O'reilly Media
Year: 2013
Language: English
Pages: 362
Copyright......Page 4
Table of Contents......Page 7
Preface......Page 15
Audience......Page 16
Contents of This Book......Page 17
Conventions Used in This Book......Page 18
Safari® Books Online......Page 19
Acknowledgments......Page 20
Part I. Easing into the Fundamentals of Design Patterns......Page 23
Entering into Intermediate and Advanced Programming......Page 27
Making Problem Solving Easier......Page 28
Modularization......Page 29
Single Responsibility Principle......Page 30
The Client as a Requester Class......Page 31
The Speed of Development and Change......Page 35
Sequential Programming......Page 36
Procedural Programming......Page 37
Pay Me Now or Pay Me Later......Page 38
Abstraction......Page 41
Abstract Classes......Page 42
Abstract Properties and Methods......Page 44
Interfaces......Page 45
Interfaces and Constants......Page 47
Type Hinting: Almost Data Typing......Page 48
Everyday Encapsulation......Page 51
Protecting Encapsulation through Visibility......Page 52
Getters and Setters......Page 55
Inheritance......Page 56
Polymorphism......Page 58
One Name with Many Implementations......Page 60
Easy Does It......Page 61
The MVC Loosens and Refocuses Programming......Page 63
Basic Principles of Design Patterns......Page 65
The First Design Pattern Principle......Page 66
Using Interface Data Types in Code Hinting......Page 67
Abstract Classes and Their Interfaces......Page 68
Basic Composition Using a Client......Page 71
Delegation: The IS-A and HAS-A Difference......Page 75
Organization of Design Patterns......Page 76
What Causes Redesign?......Page 77
What Varies?......Page 78
What Is the Difference Between Design Patterns and
Frameworks?......Page 79
Why Unified Modeling Language (UML)?......Page 81
Class Diagrams......Page 82
Participant Symbols......Page 83
Relationship Notations......Page 85
Acquaintance Relations......Page 86
Aggregation Relationship......Page 88
Inheritance and Implementation Relations......Page 90
Creates Relations......Page 92
Multiple Relations......Page 93
Object Diagrams......Page 94
Interaction Diagrams......Page 95
The Role of Diagrams and Notations in Object-Oriented
Programming......Page 96
Other UMLs......Page 97
Part II. Creational Design Patterns......Page 99
What Is the Factory Method Pattern?......Page 101
When to Use the Factory Method......Page 102
Factory Work......Page 103
The Client......Page 106
Adding Graphic Elements......Page 107
Coordinating Products......Page 109
Changing the Text Product......Page 110
Adding New Products and Parameterized Requests......Page 111
One Factory and Multiple Products......Page 112
The New Factories......Page 113
The New Products......Page 114
Helper Classes......Page 116
Product Changes: Leave the Interface Alone!......Page 118
What Is the Prototype Design Pattern?......Page 121
When to Use the Prototype Pattern......Page 122
The Clone Function......Page 123
Constructor Does Not Relaunch with Clone......Page 124
The Constructor Function Should Do No Real Work......Page 125
Studying Fruit Flies......Page 126
The Modern Business Organization......Page 130
Encapsulation in the Interface......Page 131
The Interface Implementations......Page 132
The Organizational Client......Page 135
Making Changes, Adding Features......Page 138
Dynamic Object Instantiation......Page 139
The Prototype in PHP Land......Page 141
Part III. Structural Design Patterns......Page 143
What Is the Adapter Pattern?......Page 145
When to Use the Adapter Pattern......Page 147
The Adapter Pattern Using Inheritance......Page 148
A Minimal Example of a Class Adapter: The Currency
Exchange......Page 149
From Desktop to Mobile......Page 153
Adapters and Change......Page 162
What Is the Decorator Pattern?......Page 163
When to Use the Decorator Pattern......Page 164
The Component Interface......Page 165
The Decorator Interface......Page 166
Concrete Component......Page 167
Concrete Decorators......Page 168
The Client......Page 170
Primitives in Wrappers......Page 171
Built-in Wrappers in PHP......Page 172
Decorators with Multiple Components......Page 173
The Developer Dating Service......Page 174
HTML User Interface (UI)......Page 181
The Client Class Passing HTML Data......Page 185
From a Variable Name to an Object Instance......Page 186
Adding a Decoration......Page 187
Part IV. Behavioral Design Patterns......Page 189
What Is the Template Method Pattern?......Page 191
When to Use the Template Method......Page 192
The Abstract Class......Page 193
The Client......Page 194
The Hollywood Principle......Page 196
Using the Template Method with Other Design Patterns......Page 197
The Client’s Reduced Workload......Page 198
The Template Method Participants......Page 199
The Factory Method Participants......Page 200
The Hook in the Template Method Design Pattern......Page 203
Setting Up the Hook......Page 206
The Client and Tripping the Hook......Page 207
The Small and Mighty Template Method......Page 209
What Is the State Pattern?......Page 211
When to Use the State Pattern?......Page 212
Light On, Light Off: The Minimal State Design Pattern......Page 214
Context Is King......Page 215
The States......Page 218
The Client Request through the Context......Page 219
Adding States......Page 220
Changing the States......Page 221
Updating the Context Class......Page 224
An Updated Client......Page 225
Setting Up a Matrix Statechart......Page 227
Setting Up the Interface......Page 228
The Context......Page 229
The States......Page 231
The Client Picks a Path......Page 237
The State Pattern and PHP......Page 240
Part V. MySQL and PHP Design Patterns......Page 241
A Simple Interface and Class for MySQL......Page 243
The Pregnant Interface......Page 244
Universal MySQL Connection Class and Static Variables......Page 245
Easy Client......Page 246
The Protection Proxy for Login......Page 247
Setting Up Login Registration......Page 248
Implementing the Login Proxy......Page 252
The Proxy and Real-World Security......Page 259
Encapsulating Algorithms......Page 261
Differentiating the Strategy from the State Design
Pattern......Page 262
No Conditional Statements, Please......Page 263
A Minimalist Strategy Pattern......Page 264
The Client and the Trigger Scripts......Page 266
The Context Class and Strategy Interface......Page 269
The Concrete Strategies......Page 270
A Data Security Helper Class......Page 273
The Survey Table......Page 276
Data Entry Modules......Page 278
The Client Calls for Help......Page 283
The Minor but Major Change in Context Class......Page 284
The Concrete Strategies......Page 285
The Flexible Strategy Pattern......Page 291
Passing the Buck......Page 293
Building and Loading the Response Table......Page 295
The Help Desk Chain of Responsibility......Page 300
Automated Chain of Responsibility and Factory Method......Page 306
The Chain of Responsibility and Date-Driven Requests......Page 307
Factory Method Finishes Job......Page 312
Ease of Update......Page 317
Built-In Observer Interfaces......Page 319
When to Use the Observer Pattern......Page 320
SplSubject......Page 322
The SPL Concrete Subject......Page 323
The SPL Concrete Observer......Page 325
The SPL Client......Page 326
Free Range PHP and the Observer Pattern......Page 327
The Abstract Subject Class and ConcreteSubject
Implementation......Page 328
Observer and Multiple Concrete Observers......Page 329
The Client......Page 331
CMS Utilities......Page 333
The Multiple Device Observer......Page 338
Thinking OOP......Page 352
Index......Page 355
About the Author......Page 362