Practical Python Design Patterns: Pythonic Solutions to Common Problems
Wessel Badenhorst
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book s product page, located at www.apress.com/9781484226797. For more detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
For Tanya, my love, always.
Table of Contents
About the Author xiii
About the Technical Reviewer xv
Acknowledgments xvii
Chapter 1: Before We Begin 1
The Masters 1
Becoming a Better Programmer 3
Deliberate Practice 4
Single Focus 5
Rapid Feedback 5
Stretch Yourself 6
Stand on the Shoulders of Giants 7
How Do You Do This? 7
The Ability to Course Correct 8
Systems Thinking 9
Mental Models 9
The Right Tools for the Job 10
Design Patterns as a Concept 11
What Makes a Design Pattern? 11
Classification 12
The Tools We Will Be Using 13
How to Read This Book 13
Setting Up Your Python Environment 14
On Linux 14
On Mac 15
On Windows 16
VirtualEnv 17
Editors 18
Atom 19
LightTable 19
PyCharm 19
Vim 19
Emacs 20
Sublime Text 20
Summary 21
Chapter 2: The Singleton Pattern 23
The Problem 23
Enter the Objects 29
Cleaning It Up 32
Exercises 35
Chapter 3: The Prototype Pattern 37
The Problem 37
Beyond the First Steps 37
Base for an Actual Game 38
Implementing the Prototype Pattern 51
Shallow Copy vs Deep Copy 52
Shallow Copy 53
Dealing with Nested Structures 54
Deep Copy 55
Using What We Have Learned in Our Project 56
Exercises 59
Chapter 4: Factory Pattern 61
Getting Started 61
The Game Loop 63
The Factory Method 68
The Abstract Factory 70
Summary 72
Exercises 73
Chapter 5: Builder Pattern 75
Anti-Patterns 84
A Note on Abstraction 89
Exercises 90
Chapter 6: Adapter Pattern 91
Don’t Repeat Yourself (DRY) 93
Separation of Concern 93
Sample Problem 96
Class Adapter 97
Object Adapter Pattern 98
Duck Typing 99
Implementing the Adapter Pattern in the Real World 100
Parting Shots 102
Exercises 103
Chapter 7: Decorator Pattern 105
The Decorator Pattern 108
Closures 113
Retaining Function name and doc Attributes 114
Decorating Classes 118
Parting Shots 121
Exercises 121
Chapter 8: Facade Pattern 123
Point of Sale Example 123
Systems Evolution 127
What Sets the Facade Pattern Apart 129
Parting Shots 132
Exercises 132
Chapter 9: Proxy Pattern 133
Memoization 133
The Proxy Pattern 136
Remote Proxy 139
Virtual Proxy 139
Protection Proxy 140
Parting Shots 140
Exercises 141
Chapter 10: Chain of Responsibility Pattern 143
Setting Up a WSGI Server 145
Authentication Headers 146
The Chain of Responsibility Pattern 150
Implementing Chain of Responsibility in Our Project 154
A More Pythonic Implementation 159
Parting Shots 164
Exercises 165
Chapter 11: Command Pattern 167
Controlling the Turtle 167
The Command Pattern 169
Parting Shots 177
Exercises 177
Chapter 12: Interpreter Pattern 179
Domain-Specific Languages 179
Advantages of DSLs 183
Disadvantages of DSLs 183
Composite Pattern 188
Internal DSL Implementation Using the Composite Pattern 189
Implementing the Interpreter Pattern 194
Parting Shots 201
Exercises 201
Chapter 13: Iterator Pattern 203
Python Internal Implementation of the Iterator Pattern 206
Itertools 213
Generator Functions 214
Generator Expression 216
Parting Shots 217
Exercises 217
Chapter 14: Observer Pattern 219
Parting Shots 236
Exercises 237
Chapter15: State Pattern 239
State Pattern 242
Parting Shots 247
Exercises 248
Chapter 16: Strategy Pattern 249
Parting Shots 254
Exercises 255
Chapter 17: Template Method Pattern 257
Parting Shots 269
Exercises 269
Chapter 18: Visitor Pattern 271
The Visitor Pattern 284
Parting Shots 296
Exercises 297
Chapter 19: Model-View-Controller Pattern 299
Model-View-Controller Skeleton 303
Controllers 305
Models 306
Views 306
Bringing It All Together 307
Parting Shots 313
Exercises 314
Chapter 20: Publish–Subscribe Pattern 315
Distributed Message Sender 322
Parting Shots 325
Exercises 326
Appendix A: Design Pattern Quick Reference 327
Quick Checks for the Code 327
Singleton 328
Prototype 329
Factory 329
Builder 330
Adapter 331
Decorator 331
Facade 332
Proxy 333
Chain of Responsibility 334
Alternative 334
Command 335
Composite 336
Interpreter 336
Iterator 337
Alternative 338
Observer 338
State 339
Strategy 340
Template Method 340
Visitor 341
Model–View–Controller 342
Publisher–Subscriber 343
Final Words 344
Index 345
'Practical Python Design Patterns' 카테고리의 다른 글
Builder Pattern (0) | 2023.03.28 |
---|---|
Factory Pattern (0) | 2023.03.28 |
The Prototype Pattern (0) | 2023.03.28 |
The Singleton Pattern (0) | 2023.03.28 |
Before We Begin (0) | 2023.03.28 |
댓글