Contents at a Glance
I: Introduction
1 Introduction to Objects
2 Visual Programming in Python
3 Visual Programming of Tables of Data
4 What Are Design Patterns?
II: Creational Patterns
5 The Factory Pattern
6 The Factory Method Pattern
7 The Abstract Factory Pattern
8 The Singleton Pattern
9 The Builder Pattern
10 The Prototype Pattern
11 Summary of Creational Patterns
III: Structural Patterns
12 The Adapter Pattern
13 The Bridge Pattern
14 The Composite Pattern
15 The Decorator Pattern
16 The Façade Pattern
17 The Flyweight Pattern
18 The Proxy Pattern
19 Summary of Structural Patterns
IV: Behavioral Patterns
20 Chain of Responsibility Pattern
21 The Command Pattern
22 The Interpreter Pattern
23 The Iterator Pattern
24 The Mediator Pattern
25 The Memento Pattern
26 The Observer Pattern
27 The State Pattern
28 The Strategy Pattern
29 The Template Pattern
30 The Visitor Pattern
V: A Brief Introduction to Python
31 Variables and Syntax in Python
32 Making Decisions in Python
33 Development Environments
34 Python Collections and Files
35 Functions
A Running Python programs
Index
Table of Contents
I: Introduction
The tkinter Library
GitHub
1 Introduction to Objects
The Class __init__ Method
Variables Inside a Class
Collections of Classes
Inheritance
Derived Classes Created with Revised Methods
Multiple Inheritance
Drawing a Rectangle and a Square
Visibility of Variables
Properties
Local Variables
Types in Python
Summary
Programs on GitHub
2 Visual Programming in Python
Importing Fewer Names
Creating an Object-Oriented Version
Using Message Boxes
Using File Dialogs
Understanding Options for the Pack Layout Manager
Using the ttk Libraries
Responding to User Input
Adding Two Numbers
Catching the Error
Applying Colors in tkinter
Creating Radio Buttons
Using a Class-Level Variable
Communicating Between Classes
Using the Grid Layout
Creating Checkbuttons
Disabling Check Boxes
Adding Menus to Windows
Using the LabelFrame
Moving On
Examples on GitHub
3 Visual Programming of Tables of Data
Creating a Listbox
Displaying the State Data
Using a Combobox
The Treeview Widget
Inserting Tree Nodes
Moving On
Example Code on GitHub
4 What Are Design Patterns?
Defining Design Patterns
The Learning Process
Notes on Object-Oriented Approaches
Python Design Patterns
References
II: Creational Patterns
5 The Factory Pattern
How a Factory Works
Sample Code
The Two Subclasses
Building the Simple Factory
Using the Factory
A Simple GUI
Factory Patterns in Math Computation
Programs on GitHub
Thought Questions
6 The Factory Method Pattern
The Swimmer Class
The Event Classes
Straight Seeding
Circle Seeding
Our Seeding Program
Other Factories
When to Use a Factory Method
Programs on GitHub
7 The Abstract Factory Pattern
A GardenMaker Factory
How the User Interface Works
Consequences of the Abstract Factory Pattern
Thought Questions
Code on GitHub
8 The Singleton Pattern
Throwing the Exception
Creating an Instance of the Class
Static Classes As Singleton Patterns
Finding the Singletons in a Large Program
Other Consequences of the Singleton Pattern
Sample Code on GitHub
9 The Builder Pattern
An Investment Tracker
Calling the Builders
The List Box Builder
The Checkbox Builder
Displaying the Selected Securities
Consequences of the Builder Pattern
Thought Questions
Sample Code on GitHub
10 The Prototype Pattern
Cloning in Python
Using the Prototype
Consequences of the Prototype Pattern
Sample Code on GitHub
11 Summary of Creational Patterns
III: Structural Patterns
12 The Adapter Pattern
Moving Data Between Lists
Making an Adapter
The Class Adapter
Two-Way Adapters
Pluggable Adapters
Programs on GitHub
13 The Bridge Pattern
Creating the User Interface
Extending the Bridge
Consequences of the Bridge Pattern
Programs on GitHub
14 The Composite Pattern
An Implementation of a Composite
Salary Computation
The Employee Classes
The Boss Class
Building the Employee Tree
Printing the Employee Tree
Creating a Treeview of the Composite
Using Doubly Linked Lists
Consequences of the Composite Pattern
A Simple Composite
Other Implementation Issues
Dealing with Recursive Calls
Ordering Components
Caching Results
Programs on GitHub
15 The Decorator Pattern
Decorating a Button
Using a Decorator
Using Nonvisual Decorators
Decorated Code
The dataclass Decorator
Using dataclass with Default Values
Decorators, Adapters, and Composites
Consequences of the Decorator Pattern
Programs on GitHub
16 The Façade Pattern
Building the Façade Classes
Creating Databases and Tables
Using the SQLite Version
Consequences of the Façade
Programs on GitHub
Notes on MySQL
Using SQLite
References
17 The Flyweight Pattern
What Are Flyweights?
Example Code
Selecting a Folder
Copy-on-Write Objects
Program on GitHub
18 The Proxy Pattern
Using the Pillow Image Library
Displaying an Image Using PIL
Using Threads to Handle Image Loading
Logging from Threads
Copy-on-Write
Comparing Related Patterns
Programs on GitHub
19 Summary of Structural Patterns
IV: Behavioral Patterns
20 Chain of Responsibility Pattern
When to Use the Chain
Sample Code
The Listboxes
Programming a Help System
Receiving the Help Command
The First Case
A Chain or a Tree?
Kinds of Requests
Consequences of the Chain of Responsibility
Programs on GitHub
21 The Command Pattern
When to Use the Command Pattern
Command Objects
A Keyboard Example
Calling the Command Objects
Building Command Objects
The Command Pattern
Consequences of the Command Pattern
Providing the Undo Function
Creating the Red and Blue Buttons
Undoing the Lines
Summary
References
Programs on GitHub
22 The Interpreter Pattern
When to Use an Interpreter
Where the Pattern Can Be Helpful
A Simple Report Example
Interpreting the Language
How Parsing Works
Sorting Using attrgetter()
The Print Verb
The Console Interface
The User Interface
Consequences of the Interpreter Pattern
Programs on GitHub
23 The Iterator Pattern
Why We Use Iterators
Iterators in Python
A Fibonacci Iterator
Getting the Iterator
Filtered Iterators
The Iterator Generator
A Fibonacci Iterator
Generators in Classes
Consequences of the Iterator Pattern
Programs on GitHub
24 The Mediator Pattern
An Example System
Interactions Between Controls
Sample Code
Mediators and Command Objects
Consequences of the Mediator Pattern
Single Interface Mediators
Programs on GitHub
25 The Memento Pattern
When to Use a Memento
Sample Code
Consequences of the Memento Pattern
Programs on GitHub
26 The Observer Pattern
Example Program for Watching Colors Change
The Message to the Media
Consequences of the Observer Pattern
Programs on GitHub
27 The State Pattern
Sample Code
Switching Between States
How the Mediator Interacts with the StateManager
Consequences of the State Pattern
State Transitions
Programs on GitHub
28 The Strategy Pattern
Why We Use the Strategy Pattern
Sample Code
The Context
The Program Commands
The Line and Bar Graph Strategies
Consequences of the Strategy Pattern
Programs on GitHub
29 The Template Pattern
Why We Use Template Patterns
Kinds of Methods in a Template Class
Sample Code
Drawing a Standard Triangle
Drawing an Isosceles Triangle
The Triangle Drawing Program
Templates and Callbacks
Summary and Consequences
Example Code on GitHub
30 The Visitor Pattern
When to Use the Visitor Pattern
Working with the Visitor Pattern
Sample Code
Visiting Each Class
Visiting Several Classes
Bosses Are Employees, Too
Double Dispatching
Traversing a Series of Classes
Consequences of the Visitor Pattern
Example Code on GitHub
V: A Brief Introduction to Python
31 Variables and Syntax in Python
Data Types
Numeric Constants
Strings
Character Constants
Variables
Complex Numbers
Integer Division
Multiple Equal Signs for Initialization
A Simple Python Program
Compiling and Running This Program
Arithmetic Operators
Bitwise Operators
Combined Arithmetic and Assignment Statements
Comparison Operators
The input Statement
PEP 8 Standards
Variable and Function Names
Constants
Class Names
Indentation and Spacing
Comments
Docstrings
String Methods
Examples on GitHub
32 Making Decisions in Python
elif is “else if”
Combining Conditions
The Most Common Mistake
Looping Statements in Python
The for Loop and Lists
Using range in if Statements
Using break and continue
The continue Statement
Python Line Length
The print Function
Formatting Numbers
C and Java Style Formatting
The format string Function
f-string Formatting
Comma-Separated Numbers
Strings
Formatting Dates
Using the Python match Function
Pattern Matching
Reference
Moving On
Sample Code on GitHub
33 Development Environments
IDLE
Thonny
PyCharm
Visual Studio
Other Development Environments
LiClipse
Jupyter Notebook
Google Colaboratory
Anaconda
Wing
Command-Line Execution
CPython, IPython, and Jython
34 Python Collections and Files
Slicing
Slicing Strings
Negative Indexes
String Prefix and Suffix Removal
Changing List Contents
Copying a List
Reading Files
Using the with Loop
Handling Exceptions
Using Dictionaries
Combining Dictionaries
Using Tuples
Using Sets
Using the map Function
Writing a Complete Program
Impenetrable Coding
Using List Comprehension
Sample Programs on GitHub
35 Functions
Returning a Tuple
Where Does the Program Start?
Summary
Programs on GitHub
A Running Python Programs
If You Have Python Installed
Shortcuts
Creating an Executable Python Program
Command-Line Arguments
Index