Python Programming with Design Patterns

Book description

Improve Your Python Code with Modern Object-Oriented Design Patterns

To write clean, efficient, maintainable code, developers everywhere turn to design patterns. Now there’s a Python-specific patterns guide that’s friendly and practical enough for every Python developer, regardless of experience.

Best-selling patterns author James W. Cooper presents visual, example-driven explanations of 23 proven patterns for writing superior object-oriented code. Through clear and intuitive code samples, he introduces modern techniques for creating Python objects that interact effectively in powerful, flexible programs. Python newcomers--including those moving from other languages--will find a succinct introduction designed to get them up to speed fast.

Cooper’s wide-ranging patterns coverage addresses abstract classes, multiple inheritance, GUI programming and widgets, graphical classes, drawing and plotting, math, databases, Python decorators, images, threads, iterators, creating executable code, and more. Throughout, his informal visual presentation makes patterns far easier to work with--so you can confidently build sophisticated programs that use Python’s rich capabilities.

  • Review the essentials of Python objects and visual programming

  • Learn what design patterns are, and how they help you write better code

  • Use creational patterns to enhance flexibility and avoid unnecessary complexity

  • Apply structural patterns to ensure that program elements work together well in large programs

  • Optimize communication between objects with behavioral patterns

Table of contents

  1. Cover Page
  2. About This eBook
  3. Halftitle Page
  4. Title Page
  5. Copyright Page
  6. Pearson’s Commitment to Diversity, Equity, and Inclusion
  7. Dedication Page
  8. Contents at a Glance
  9. Table of Contents
  10. Preface
    1. Book Organization
  11. Register Your Book
  12. Acknowledgments
  13. About the Author
  14. Part I: Introduction
    1. The tkinter Library
    2. GitHub
    3. 1. Introduction to Objects
      1. The Class __init__ Method
      2. Variables Inside a Class
      3. Collections of Classes
      4. Inheritance
      5. Derived Classes Created with Revised Methods
      6. Multiple Inheritance
      7. Drawing a Rectangle and a Square
      8. Visibility of Variables
      9. Types in Python
      10. Summary
      11. Programs on GitHub
    4. 2. Visual Programming in Python
      1. Importing Fewer Names
      2. Creating an Object-Oriented Version
      3. Using Message Boxes
      4. Using File Dialogs
      5. Understanding Options for the Pack Layout Manager
      6. Using the ttk Libraries
      7. Responding to User Input
      8. Applying Colors in tkinter
      9. Creating Radio Buttons
      10. Communicating Between Classes
      11. Using the Grid Layout
      12. Creating Checkbuttons
      13. Adding Menus to Windows
      14. Using the LabelFrame
      15. Moving On
      16. Examples on GitHub
    5. 3. Visual Programming of Tables of Data
      1. Creating a Listbox
      2. Using a Combobox
      3. The Treeview Widget
      4. Moving On
      5. Example Code on GitHub
    6. 4. What Are Design Patterns?
      1. Defining Design Patterns
      2. The Learning Process
      3. Notes on Object-Oriented Approaches
      4. Python Design Patterns
      5. References
  15. Part II: Creational Patterns
    1. 5. The Factory Pattern
      1. How a Factory Works
      2. Sample Code
      3. The Two Subclasses
      4. Building the Simple Factory
      5. Factory Patterns in Math Computation
      6. Programs on GitHub
      7. Thought Questions
    2. 6. The Factory Method Pattern
      1. The Swimmer Class
      2. The Event Classes
      3. Straight Seeding
      4. Our Seeding Program
      5. Other Factories
      6. When to Use a Factory Method
      7. Programs on GitHub
    3. 7. The Abstract Factory Pattern
      1. A GardenMaker Factory
      2. How the User Interface Works
      3. Consequences of the Abstract Factory Pattern
      4. Thought Questions
      5. Code on GitHub
    4. 8. The Singleton Pattern
      1. Throwing the Exception
      2. Creating an Instance of the Class
      3. Static Classes As Singleton Patterns
      4. Finding the Singletons in a Large Program
      5. Other Consequences of the Singleton Pattern
      6. Sample Code on GitHub
    5. 9. The Builder Pattern
      1. An Investment Tracker
      2. Calling the Builders
      3. Displaying the Selected Securities
      4. Consequences of the Builder Pattern
      5. Thought Questions
      6. Sample Code on GitHub
    6. 10. The Prototype Pattern
      1. Cloning in Python
      2. Using the Prototype
      3. Consequences of the Prototype Pattern
      4. Sample Code on GitHub
    7. 11. Summary of Creational Patterns
  16. Part III: Structural Patterns
    1. 12. The Adapter Pattern
      1. Moving Data Between Lists
      2. Making an Adapter
      3. Programs on GitHub
    2. 13. The Bridge Pattern
      1. Creating the User Interface
      2. Extending the Bridge
      3. Consequences of the Bridge Pattern
      4. Programs on GitHub
    3. 14. The Composite Pattern
      1. An Implementation of a Composite
      2. Salary Computation
      3. The Employee Classes
      4. Building the Employee Tree
      5. Printing the Employee Tree
      6. Creating a Treeview of the Composite
      7. Using Doubly Linked Lists
      8. Consequences of the Composite Pattern
      9. A Simple Composite
      10. Other Implementation Issues
      11. Programs on GitHub
    4. 15. The Decorator Pattern
      1. Decorating a Button
      2. Using a Decorator
      3. Using Nonvisual Decorators
      4. The dataclass Decorator
      5. Using dataclass with Default Values
      6. Decorators, Adapters, and Composites
      7. Consequences of the Decorator Pattern
      8. Programs on GitHub
    5. 16. The Façade Pattern
      1. Building the Façade Classes
      2. Creating Databases and Tables
      3. Using the SQLite Version
      4. Consequences of the Façade
      5. Programs on GitHub
      6. Notes on MySQL
      7. Using SQLite
      8. References
    6. 17. The Flyweight Pattern
      1. What Are Flyweights?
      2. Example Code
      3. Copy-on-Write Objects
      4. Program on GitHub
    7. 18. The Proxy Pattern
      1. Using the Pillow Image Library
      2. Displaying an Image Using PIL
      3. Using Threads to Handle Image Loading
      4. Logging from Threads
      5. Copy-on-Write
      6. Comparing Related Patterns
      7. Programs on GitHub
    8. 19. Summary of Structural Patterns
  17. Part IV: Behavioral Patterns
    1. 20. Chain of Responsibility Pattern
      1. When to Use the Chain
      2. Sample Code
      3. The Listboxes
      4. Programming a Help System
      5. Receiving the Help Command
      6. The First Case
      7. A Chain or a Tree?
      8. Kinds of Requests
      9. Consequences of the Chain of Responsibility
      10. Programs on GitHub
    2. 21. The Command Pattern
      1. When to Use the Command Pattern
      2. Command Objects
      3. A Keyboard Example
      4. Calling the Command Objects
      5. Building Command Objects
      6. The Command Pattern
      7. Consequences of the Command Pattern
      8. Providing the Undo Function
      9. Summary
      10. References
      11. Programs on GitHub
    3. 22. The Interpreter Pattern
      1. When to Use an Interpreter
      2. Where the Pattern Can Be Helpful
      3. A Simple Report Example
      4. Interpreting the Language
      5. How Parsing Works
      6. The Console Interface
      7. The User Interface
      8. Consequences of the Interpreter Pattern
      9. Programs on GitHub
    4. 23. The Iterator Pattern
      1. Why We Use Iterators
      2. Iterators in Python
      3. A Fibonacci Iterator
      4. Filtered Iterators
      5. The Iterator Generator
      6. A Fibonacci Iterator
      7. Generators in Classes
      8. Consequences of the Iterator Pattern
      9. Programs on GitHub
    5. 24. The Mediator Pattern
      1. An Example System
      2. Interactions Between Controls
      3. Sample Code
      4. Mediators and Command Objects
      5. Consequences of the Mediator Pattern
      6. Single Interface Mediators
      7. Programs on GitHub
    6. 25. The Memento Pattern
      1. When to Use a Memento
      2. Sample Code
      3. Consequences of the Memento Pattern
      4. Program on GitHub
    7. 26. The Observer Pattern
      1. Example Program for Watching Colors Change
      2. The Message to the Media
      3. Consequences of the Observer Pattern
      4. Programs on GitHub
    8. 27. The State Pattern
      1. Sample Code
      2. Switching Between States
      3. How the Mediator Interacts with the StateManager
      4. Consequences of the State Pattern
      5. State Transitions
      6. Program on GitHub
    9. 28. The Strategy Pattern
      1. Why We Use the Strategy Pattern
      2. Sample Code
      3. The Context
      4. The Program Commands
      5. The Line and Bar Graph Strategies
      6. Consequences of the Strategy Pattern
      7. Programs on GitHub
    10. 29. The Template Pattern
      1. Why We Use Template patterns
      2. Kinds of Methods in a Template Class
      3. Sample Code
      4. The Triangle Drawing Program
      5. Templates and Callbacks
      6. Summary and Consequences
      7. Example Code on GitHub
    11. 30. The Visitor Pattern
      1. When to Use the Visitor Pattern
      2. Working with the Visitor Pattern
      3. Sample Code
      4. Visiting Several Classes
      5. Bosses Are Employees, Too
      6. Double Dispatching
      7. Traversing a Series of Classes
      8. Consequences of the Visitor Pattern
      9. Example Code on GitHub
  18. Part V: A Brief Introduction to Python
    1. 31. Variables and Syntax in Python
      1. Data Types
      2. Numeric Constants
      3. Strings
      4. Character Constants
      5. Variables
      6. Complex Numbers
      7. Integer Division
      8. Multiple Equal Signs for Initialization
      9. A Simple Python Program
      10. Compiling and Running This Program
      11. Arithmetic Operators
      12. Combined Arithmetic and Assignment Statements
      13. Comparison Operators
      14. The input Statement
      15. PEP 8 Standards
      16. String Methods
      17. Examples on GitHub
    2. 32. Making Decisions in Python
      1. elif is “else if”
      2. Combining Conditions
      3. The Most Common Mistake
      4. Looping Statements in Python
      5. Using break and continue
      6. Python Line Length
      7. The print Function
      8. Formatting Numbers
      9. Formatting Dates
      10. Using the Python match Function
      11. Reference
      12. Moving On
      13. Sample Code on GitHub
    3. 33. Development Environments
      1. IDLE
      2. Thonny
      3. PyCharm
      4. Visual Studio
      5. Other Development Environments
      6. Command-Line Execution
      7. CPython, IPython, and Jython
    4. 34. Python Collections and Files
      1. Slicing
      2. Slicing Strings
      3. Changing List Contents
      4. Copying a List
      5. Reading Files
      6. Handling Exceptions
      7. Using Dictionaries
      8. Using Tuples
      9. Using Sets
      10. Using the map Function
      11. Writing a Complete Program
      12. Using List Comprehension
      13. Sample Programs on GitHub
    5. 35. Functions
      1. Returning a Tuple
      2. Where Does the Program Start?
      3. Summary
      4. Programs on GitHub
  19. Appendix A: Running Python Programs
    1. If You Have Python Installed
    2. Creating an Executable Python Program
    3. Command-Line Arguments
  20. Index
  21. Code Snippets

Product information

  • Title: Python Programming with Design Patterns
  • Author(s): James W. Cooper
  • Release date: December 2021
  • Publisher(s): Addison-Wesley Professional
  • ISBN: 9780137579921