Practical Python Design Patterns - Pythonic Solutions to Common Problems
von: Wessel Badenhorst
Apress, 2017
ISBN: 9781484226803
Sprache: Englisch
352 Seiten, Download: 2774 KB
Format: PDF, auch als Online-Lesen
Table of Contents | 5 | ||
About the Author | 12 | ||
About the Technical Reviewer | 13 | ||
Acknowledgments | 14 | ||
Chapter 1: Before We Begin | 15 | ||
The Masters | 15 | ||
Becoming a Better Programmer | 17 | ||
Deliberate Practice | 18 | ||
Single Focus | 19 | ||
Rapid Feedback | 19 | ||
Stretch Yourself | 20 | ||
Stand on the Shoulders of Giants | 21 | ||
How Do You Do This? | 21 | ||
The Ability to Course Correct | 22 | ||
Systems Thinking | 23 | ||
Mental Models | 23 | ||
The Right Tools for the Job | 24 | ||
Design Patterns as a Concept | 25 | ||
What Makes a Design Pattern? | 25 | ||
Classification | 26 | ||
Creational | 26 | ||
Structural | 26 | ||
Behavioral | 27 | ||
The Tools We Will Be Using | 27 | ||
How to Read This Book | 27 | ||
Setting Up Your Python Environment | 28 | ||
On Linux | 28 | ||
On Mac | 29 | ||
On Windows | 30 | ||
VirtualEnv | 31 | ||
Editors | 32 | ||
Atom | 33 | ||
LightTable | 33 | ||
PyCharm | 33 | ||
Vim | 33 | ||
Emacs | 34 | ||
Sublime Text | 34 | ||
Summary | 35 | ||
Chapter 2: The Singleton Pattern | 36 | ||
The Problem | 36 | ||
Enter the Objects | 42 | ||
Cleaning It Up | 45 | ||
Exercises | 48 | ||
Chapter 3: The Prototype Pattern | 49 | ||
The Problem | 49 | ||
Beyond the First Steps | 49 | ||
Base for an Actual Game | 50 | ||
Implementing the Prototype Pattern | 63 | ||
Shallow Copy vs. Deep Copy | 64 | ||
Shallow Copy | 65 | ||
Dealing with Nested Structures | 66 | ||
Deep Copy | 67 | ||
Using What We Have Learned in Our Project | 68 | ||
Exercises | 71 | ||
Chapter 4: Factory Pattern | 73 | ||
Getting Started | 73 | ||
The Game Loop | 75 | ||
The Factory Method | 80 | ||
The Abstract Factory | 82 | ||
Summary | 84 | ||
Exercises | 85 | ||
Chapter 5: Builder Pattern | 86 | ||
Anti-Patterns | 95 | ||
A Note on Abstraction | 100 | ||
Exercises | 101 | ||
Chapter 6: Adapter Pattern | 102 | ||
Don’t Repeat Yourself (DRY) | 104 | ||
Separation of Concern | 104 | ||
Sample Problem | 107 | ||
Class Adapter | 108 | ||
Object Adapter Pattern | 109 | ||
Duck Typing | 110 | ||
Implementing the Adapter Pattern in the Real World | 111 | ||
Parting Shots | 113 | ||
Exercises | 114 | ||
Chapter 7: Decorator Pattern | 115 | ||
The Decorator Pattern | 118 | ||
Closures | 123 | ||
Retaining Function __name__ and __doc__ Attributes | 124 | ||
Decorating Classes | 128 | ||
Parting Shots | 131 | ||
Exercises | 131 | ||
Chapter 8: Facade Pattern | 132 | ||
Point of Sale Example | 132 | ||
Systems Evolution | 136 | ||
What Sets the Facade Pattern Apart | 138 | ||
Parting Shots | 141 | ||
Exercises | 141 | ||
Chapter 9: Proxy Pattern | 142 | ||
Memoization | 142 | ||
The Proxy Pattern | 145 | ||
Remote Proxy | 148 | ||
Virtual Proxy | 148 | ||
Protection Proxy | 149 | ||
Parting Shots | 149 | ||
Exercises | 150 | ||
Chapter 10: Chain of Responsibility Pattern | 151 | ||
Setting Up a WSGI Server | 153 | ||
Authentication Headers | 154 | ||
The Chain of Responsibility Pattern | 158 | ||
Implementing Chain of Responsibility in Our Project | 162 | ||
A More Pythonic Implementation | 167 | ||
Parting Shots | 172 | ||
Exercises | 173 | ||
Chapter 11: Command Pattern | 174 | ||
Controlling the Turtle | 174 | ||
The Command Pattern | 176 | ||
Parting Shots | 184 | ||
Exercises | 184 | ||
Chapter 12: Interpreter Pattern | 185 | ||
Domain-Specific Languages | 185 | ||
Advantages of DSLs | 189 | ||
Disadvantages of DSLs | 189 | ||
Composite Pattern | 194 | ||
Internal DSL Implementation Using the Composite Pattern | 195 | ||
Implementing the Interpreter Pattern | 200 | ||
Parting Shots | 207 | ||
Exercises | 207 | ||
Chapter 13: Iterator Pattern | 209 | ||
Python Internal Implementation of the Iterator Pattern | 212 | ||
Itertools | 219 | ||
Generator Functions | 220 | ||
Generator Expression | 222 | ||
Parting Shots | 223 | ||
Exercises | 223 | ||
Chapter 14: Observer Pattern | 224 | ||
Parting Shots | 241 | ||
Exercises | 242 | ||
Chapter 15: State Pattern | 243 | ||
State Pattern | 246 | ||
Parting Shots | 251 | ||
Exercises | 252 | ||
Chapter 16: Strategy Pattern | 253 | ||
Parting Shots | 258 | ||
Exercises | 259 | ||
Chapter 17: Template Method Pattern | 260 | ||
Parting Shots | 272 | ||
Exercises | 272 | ||
Chapter 18: Visitor Pattern | 274 | ||
The Visitor Pattern | 287 | ||
Parting Shots | 299 | ||
Exercises | 300 | ||
Chapter 19: Model-View-Controller Pattern | 301 | ||
Model-View-Controller Skeleton | 305 | ||
Controllers | 307 | ||
Models | 308 | ||
Views | 308 | ||
Bringing It All Together | 309 | ||
Parting Shots | 315 | ||
Exercises | 316 | ||
Chapter 20: Publish–Subscribe Pattern | 317 | ||
Distributed Message Sender | 324 | ||
Parting Shots | 327 | ||
Exercises | 328 | ||
Appendix ADesign Pattern Quick Reference | 329 | ||
Quick Checks for the Code | 329 | ||
Singleton | 330 | ||
Prototype | 331 | ||
Factory | 331 | ||
Builder | 332 | ||
Adapter | 333 | ||
Decorator | 333 | ||
Facade | 334 | ||
Proxy | 335 | ||
Chain of Responsibility | 336 | ||
Alternative | 336 | ||
Command | 337 | ||
Composite | 338 | ||
Interpreter | 338 | ||
Iterator | 339 | ||
Alternative | 340 | ||
Observer | 340 | ||
State | 341 | ||
Strategy | 342 | ||
Template Method | 342 | ||
Visitor | 343 | ||
Model–View–Controller | 344 | ||
Publisher–Subscriber | 345 | ||
Final Words | 346 | ||
Index | 347 |