
Practical Python Design Patterns Pythonic Solutions to Common Problems Wessel Badenhorst Practical Python Design Patterns: Pythonic Solutions to Common Problems Wessel Badenhorst Durbanville, Eastern Cape, South Africa ISBN-13 (pbk): 978-1-4842-2679-7 ISBN-13 (electronic): 978-1-4842-2680-3 https://doi.org/10.1007/978-1-4842-2680-3 Library of Congress Control Number: 2017957538 Copyright © 2017 by 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. Contents 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
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages347 Page
-
File Size-