Zum Hauptinhalt springen
Dekorationsartikel gehören nicht zum Leistungsumfang.
Python Programming with Design Patterns
Taschenbuch von James Cooper
Sprache: Englisch

58,65 €*

inkl. MwSt.

Versandkostenfrei per Post / DHL

Lieferzeit 1-2 Wochen

Kategorien:
Beschreibung

Improve Students' 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 newcomersincluding those moving from other languageswill 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 withso students 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 to 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

Improve Students' 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 newcomersincluding those moving from other languageswill 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 withso students 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 to 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
Über den Autor

James W. Cooper holds a PhD in chemistry and worked in academia, for the scientific instrument industry, and for IBM for 25 years, primarily as a computer scientist at IBM's Thomas J. Watson Research Center. Now retired, he is the author of 20 books, including 3 on design patterns in various languages. His most recent books are Flameout: The Rise and Fall of IBM Instruments (2019) and Food Myths Debunked (2014).

James holds 11 patents and has written 60 columns for JavaPro Magazine. He has also written nearly 1,000 columns for the now vanished [...] on foods and chemistry, and he currently writes his own blog: [...]. Recently, he has written columns on Python for [...] and Substack.

He is also involved in local theater groups and is the treasurer for Troupers Light Opera, where he performs regularly.

Inhaltsverzeichnis

Preface xxi

PART I: INTRODUCTION 1

The tkinter Library 2

GitHub 2

Chapter 1 Introduction to Objects 5

The Class __init__ Method 6

Variables Inside a Class 6

Collections of Classes 7

Inheritance 8

Derived Classes Created with Revised Methods 8

Multiple Inheritance 8

Drawing a Rectangle and a Square 10

Visibility of Variables 12

Properties 13

Local Variables 13

Types in Python 13

Summary 14

Programs on GitHub 15

Chapter 2 Visual Programming in Python 17

Importing Fewer Names 19

Creating an Object-Oriented Version 19

Using Message Boxes 21

Using File Dialogs 22

Understanding Options for the Pack Layout Manager 23

Using the ttk Libraries 24

Responding to User Input 25

Adding Two Numbers 26

Catching the Error 26

Applying Colors in tkinter 27

Creating Radio Buttons 27

Using a Class-Level Variable 30

Communicating Between Classes 30

Using the Grid Layout 30

Creating Checkbuttons 32

Disabling Check Boxes 32

Adding Menus to Windows 35

Using the LabelFrame 39

Moving On 40

Examples on GitHub 40

Chapter 3 Visual Programming of Tables of Data 41

Creating a Listbox 42

Displaying the State Data 44

Using a Combobox 46

The Treeview Widget 47

Inserting Tree Nodes 50

Moving On 51

Example Code on GitHub 51

Chapter 4 What Are Design Patterns? 53

Defining Design Patterns 54

The Learning Process 55

Notes on Object-Oriented Approaches 56

Python Design Patterns 57

References 57

PART II: CREATIONAL PATTERNS 59

Chapter 5 The Factory Pattern 61

How a Factory Works 61

Sample Code 62

The Two Subclasses 62

Building the Simple Factory 63

Using the Factory 63

A Simple GUI 64

Factory Patterns in Math Computation 65

Programs on GitHub 65

Thought Questions 66

Chapter 6 The Factory Method Pattern 67

The Swimmer Class 68

The Event Classes 69

Straight Seeding 70

Circle Seeding 71

Our Seeding Program 72

Other Factories 74

When to Use a Factory Method 74

Programs on GitHub 74

Chapter 7 The Abstract Factory Pattern 75

A GardenMaker Factory 75

How the User Interface Works 77

Consequences of the Abstract Factory Pattern 77

Thought Questions 78

Code on GitHub 78

Chapter 8 The Singleton Pattern 79

Throwing the Exception 80

Creating an Instance of the Class 80

Static Classes As Singleton Patterns 81

Finding the Singletons in a Large Program 81

Other Consequences of the Singleton Pattern 82

Sample Code on GitHub 82

Chapter 9 The Builder Pattern 83

An Investment Tracker 84

Calling the Builders 86

The List Box Builder 87

The Checkbox Builder 88

Displaying the Selected Securities 89

Consequences of the Builder Pattern 89

Thought Questions 89

Sample Code on GitHub 89

Chapter 10 The Prototype Pattern 91

Cloning in Python 91

Using the Prototype 92

Consequences of the Prototype Pattern 94

Sample Code on GitHub 94

Chapter 11 Summary of Creational Patterns 95

PART III: STRUCTURAL PATTERNS 97

Chapter 12 The Adapter Pattern 99

Moving Data Between Lists 99

Making an Adapter 101

The Class Adapter 103

Two-Way Adapters 103

Pluggable Adapters 103

Programs on GitHub 103

Chapter 13 The Bridge Pattern 105

Creating the User Interface 107

Extending the Bridge 108

Consequences of the Bridge Pattern 109

Programs on GitHub 110

Chapter 14 The Composite Pattern 111

An Implementation of a Composite 112

Salary Computation 112

The Employee Classes 112

The Boss Class 113

Building the Employee Tree 114

Printing the Employee Tree 114

Creating a Treeview of the Composite 116

Using Doubly Linked Lists 117

Consequences of the Composite Pattern 118

A Simple Composite 119

Other Implementation Issues 119

Dealing with Recursive Calls 119

Ordering Components 120

Caching Results 120

Programs on GitHub 120

Chapter 15 The Decorator Pattern 121

Decorating a Button 121

Using a Decorator 122

Using Nonvisual Decorators 123

Decorated Code 124

The dataclass Decorator 125

Using dataclass with Default Values 126

Decorators, Adapters, and Composites 126

Consequences of the Decorator Pattern 126

Programs on GitHub 127

Chapter 16 The Facade Pattern 129

Building the Façade Classes 131

Creating Databases and Tables 135

Using the SQLite Version 136

Consequences of the Façade 137

Programs on GitHub 137

Notes on MySQL 137

Using SQLite 138

References 138

Chapter 17 The Flyweight Pattern 139

What Are Flyweights? 139

Example Code 140

Selecting a Folder 142

Copy-on-Write Objects 143

Program on GitHub 143

Chapter 18 The Proxy Pattern 145

Using the Pillow Image Library 145

Displaying an Image Using PIL 146

Using Threads to Handle Image Loading 146

Logging from Threads 149

Copy-on-Write 149

Comparing Related Patterns 149

Programs on GitHub 150

Chapter 19 Summary of Structural Patterns 151

PART IV: BEHAVIORAL PATTERNS 153

Chapter 20 Chain of Responsibility Pattern 155

When to Use the Chain 156

Sample Code 156

The Listboxes 159

Programming a Help System 160

Receiving the Help Command 161

The First Case 162

A Chain or a Tree? 163

Kinds of Requests 164

Consequences of the Chain of Responsibility 164

Programs on GitHub 165

Chapter 21 The Command Pattern 167

When to Use the Command Pattern 167

Command Objects 168

A Keyboard Example 168

Calling the Command Objects 170

Building Command Objects 171

The Command Pattern 172

Consequences of the Command Pattern 172

Providing the Undo Function 172

Creating the Red and Blue Buttons 175

Undoing the Lines 175

Summary 176

References 176

Programs on GitHub 176

Chapter 22 The Interpreter Pattern 177

When to Use an Interpreter 177

Where the Pattern Can Be Helpful 177

A Simple Report Example 178

Interpreting the Language 179

How Parsing Works 180

Sorting Using attrgetter() 181

The Print Verb 182

The Console Interface 182

The User Interface 183

Consequences of the Interpreter Pattern 184

Programs on GitHub 185

Chapter 23 The Iterator Pattern 187

Why We Use Iterators 187

Iterators in Python 187

A Fibonacci Iterator 188

Getting the Iterator 189

Filtered Iterators 189

The Iterator Generator 191

A Fibonacci Iterator 191

Generators in Classes 192

Consequences of the Iterator Pattern 192

Programs on GitHub 193

Chapter 24 The Mediator Pattern 195

An Example System 195

Interactions Between Controls 197

Sample Code 198

Mediators and Command Objects 199

Consequences of the Mediator Pattern 200

Single Interface Mediators 200

Programs on GitHub 201

Chapter 25 The Memento Pattern 203

When to Use a Memento 203

Sample Code 204

Consequences of the Memento Pattern 209

Programs on GitHub 209

Chapter 26 The Observer Pattern 211

Example Program for Watching Colors Change 212

The Message to the Media 215

Consequences of the Observer Pattern 215

Programs on GitHub 215

Chapter 27 The State Pattern 217

Sample Code 217

Switching Between States 221

How the Mediator Interacts with the State

State Transitions 224

Programs on GitHub 224

Chapter 28 The Strategy Pattern 225

Why We Use the Strategy Pattern 225

Sample Code 226

The Context 227

The Program Commands 227

The Line and Bar Graph Strategies 228

Consequences of the Strategy Pattern 230

Programs on GitHub 231

Chapter 29 The Template Pattern 233

Why We Use Template Patterns 233

Kinds of Methods in a Template Class 234

Sample Code 234

Drawing a Standard Triangle 235

Drawing an Isosceles Triangle 236

The Triangle Drawing Program 237

Templates and Callbacks 238

Summary and Consequences 238

Example Code on GitHub 238

Chapter 30 The Visitor Pattern 239

When to Use the Visitor Pattern 239

Working with the Visitor Pattern 241

Sample Code 241

Visiting Each Class 242

Visiting Several Classes 242

Bosses Are Employees, Too 243

Double Dispatching 245

Traversing a Series of Classes 245

Consequences of the Visitor Pattern 245

Example Code on GitHub 245

PART V: A BRIEF INTRODUCTION TO PYTHON 247

Chapter 31 Variables and Syntax in Python 249

Data Types 250

Numeric Constants 250

Strings 250

Character Constants 251

Variables 252

Complex Numbers 253

Integer Division...

Details
Erscheinungsjahr: 2022
Fachbereich: Programmiersprachen
Genre: Importe, Informatik
Rubrik: Naturwissenschaften & Technik
Medium: Taschenbuch
Inhalt: Kartoniert / Broschiert
ISBN-13: 9780137579938
ISBN-10: 0137579934
Sprache: Englisch
Einband: Kartoniert / Broschiert
Autor: Cooper, James
Hersteller: Pearson Education (US)
Verantwortliche Person für die EU: preigu, Ansas Meyer, Lengericher Landstr. 19, D-49078 Osnabrück, mail@preigu.de
Maße: 233 x 180 x 20 mm
Von/Mit: James Cooper
Erscheinungsdatum: 23.02.2022
Gewicht: 0,572 kg
Artikel-ID: 120412315
Über den Autor

James W. Cooper holds a PhD in chemistry and worked in academia, for the scientific instrument industry, and for IBM for 25 years, primarily as a computer scientist at IBM's Thomas J. Watson Research Center. Now retired, he is the author of 20 books, including 3 on design patterns in various languages. His most recent books are Flameout: The Rise and Fall of IBM Instruments (2019) and Food Myths Debunked (2014).

James holds 11 patents and has written 60 columns for JavaPro Magazine. He has also written nearly 1,000 columns for the now vanished [...] on foods and chemistry, and he currently writes his own blog: [...]. Recently, he has written columns on Python for [...] and Substack.

He is also involved in local theater groups and is the treasurer for Troupers Light Opera, where he performs regularly.

Inhaltsverzeichnis

Preface xxi

PART I: INTRODUCTION 1

The tkinter Library 2

GitHub 2

Chapter 1 Introduction to Objects 5

The Class __init__ Method 6

Variables Inside a Class 6

Collections of Classes 7

Inheritance 8

Derived Classes Created with Revised Methods 8

Multiple Inheritance 8

Drawing a Rectangle and a Square 10

Visibility of Variables 12

Properties 13

Local Variables 13

Types in Python 13

Summary 14

Programs on GitHub 15

Chapter 2 Visual Programming in Python 17

Importing Fewer Names 19

Creating an Object-Oriented Version 19

Using Message Boxes 21

Using File Dialogs 22

Understanding Options for the Pack Layout Manager 23

Using the ttk Libraries 24

Responding to User Input 25

Adding Two Numbers 26

Catching the Error 26

Applying Colors in tkinter 27

Creating Radio Buttons 27

Using a Class-Level Variable 30

Communicating Between Classes 30

Using the Grid Layout 30

Creating Checkbuttons 32

Disabling Check Boxes 32

Adding Menus to Windows 35

Using the LabelFrame 39

Moving On 40

Examples on GitHub 40

Chapter 3 Visual Programming of Tables of Data 41

Creating a Listbox 42

Displaying the State Data 44

Using a Combobox 46

The Treeview Widget 47

Inserting Tree Nodes 50

Moving On 51

Example Code on GitHub 51

Chapter 4 What Are Design Patterns? 53

Defining Design Patterns 54

The Learning Process 55

Notes on Object-Oriented Approaches 56

Python Design Patterns 57

References 57

PART II: CREATIONAL PATTERNS 59

Chapter 5 The Factory Pattern 61

How a Factory Works 61

Sample Code 62

The Two Subclasses 62

Building the Simple Factory 63

Using the Factory 63

A Simple GUI 64

Factory Patterns in Math Computation 65

Programs on GitHub 65

Thought Questions 66

Chapter 6 The Factory Method Pattern 67

The Swimmer Class 68

The Event Classes 69

Straight Seeding 70

Circle Seeding 71

Our Seeding Program 72

Other Factories 74

When to Use a Factory Method 74

Programs on GitHub 74

Chapter 7 The Abstract Factory Pattern 75

A GardenMaker Factory 75

How the User Interface Works 77

Consequences of the Abstract Factory Pattern 77

Thought Questions 78

Code on GitHub 78

Chapter 8 The Singleton Pattern 79

Throwing the Exception 80

Creating an Instance of the Class 80

Static Classes As Singleton Patterns 81

Finding the Singletons in a Large Program 81

Other Consequences of the Singleton Pattern 82

Sample Code on GitHub 82

Chapter 9 The Builder Pattern 83

An Investment Tracker 84

Calling the Builders 86

The List Box Builder 87

The Checkbox Builder 88

Displaying the Selected Securities 89

Consequences of the Builder Pattern 89

Thought Questions 89

Sample Code on GitHub 89

Chapter 10 The Prototype Pattern 91

Cloning in Python 91

Using the Prototype 92

Consequences of the Prototype Pattern 94

Sample Code on GitHub 94

Chapter 11 Summary of Creational Patterns 95

PART III: STRUCTURAL PATTERNS 97

Chapter 12 The Adapter Pattern 99

Moving Data Between Lists 99

Making an Adapter 101

The Class Adapter 103

Two-Way Adapters 103

Pluggable Adapters 103

Programs on GitHub 103

Chapter 13 The Bridge Pattern 105

Creating the User Interface 107

Extending the Bridge 108

Consequences of the Bridge Pattern 109

Programs on GitHub 110

Chapter 14 The Composite Pattern 111

An Implementation of a Composite 112

Salary Computation 112

The Employee Classes 112

The Boss Class 113

Building the Employee Tree 114

Printing the Employee Tree 114

Creating a Treeview of the Composite 116

Using Doubly Linked Lists 117

Consequences of the Composite Pattern 118

A Simple Composite 119

Other Implementation Issues 119

Dealing with Recursive Calls 119

Ordering Components 120

Caching Results 120

Programs on GitHub 120

Chapter 15 The Decorator Pattern 121

Decorating a Button 121

Using a Decorator 122

Using Nonvisual Decorators 123

Decorated Code 124

The dataclass Decorator 125

Using dataclass with Default Values 126

Decorators, Adapters, and Composites 126

Consequences of the Decorator Pattern 126

Programs on GitHub 127

Chapter 16 The Facade Pattern 129

Building the Façade Classes 131

Creating Databases and Tables 135

Using the SQLite Version 136

Consequences of the Façade 137

Programs on GitHub 137

Notes on MySQL 137

Using SQLite 138

References 138

Chapter 17 The Flyweight Pattern 139

What Are Flyweights? 139

Example Code 140

Selecting a Folder 142

Copy-on-Write Objects 143

Program on GitHub 143

Chapter 18 The Proxy Pattern 145

Using the Pillow Image Library 145

Displaying an Image Using PIL 146

Using Threads to Handle Image Loading 146

Logging from Threads 149

Copy-on-Write 149

Comparing Related Patterns 149

Programs on GitHub 150

Chapter 19 Summary of Structural Patterns 151

PART IV: BEHAVIORAL PATTERNS 153

Chapter 20 Chain of Responsibility Pattern 155

When to Use the Chain 156

Sample Code 156

The Listboxes 159

Programming a Help System 160

Receiving the Help Command 161

The First Case 162

A Chain or a Tree? 163

Kinds of Requests 164

Consequences of the Chain of Responsibility 164

Programs on GitHub 165

Chapter 21 The Command Pattern 167

When to Use the Command Pattern 167

Command Objects 168

A Keyboard Example 168

Calling the Command Objects 170

Building Command Objects 171

The Command Pattern 172

Consequences of the Command Pattern 172

Providing the Undo Function 172

Creating the Red and Blue Buttons 175

Undoing the Lines 175

Summary 176

References 176

Programs on GitHub 176

Chapter 22 The Interpreter Pattern 177

When to Use an Interpreter 177

Where the Pattern Can Be Helpful 177

A Simple Report Example 178

Interpreting the Language 179

How Parsing Works 180

Sorting Using attrgetter() 181

The Print Verb 182

The Console Interface 182

The User Interface 183

Consequences of the Interpreter Pattern 184

Programs on GitHub 185

Chapter 23 The Iterator Pattern 187

Why We Use Iterators 187

Iterators in Python 187

A Fibonacci Iterator 188

Getting the Iterator 189

Filtered Iterators 189

The Iterator Generator 191

A Fibonacci Iterator 191

Generators in Classes 192

Consequences of the Iterator Pattern 192

Programs on GitHub 193

Chapter 24 The Mediator Pattern 195

An Example System 195

Interactions Between Controls 197

Sample Code 198

Mediators and Command Objects 199

Consequences of the Mediator Pattern 200

Single Interface Mediators 200

Programs on GitHub 201

Chapter 25 The Memento Pattern 203

When to Use a Memento 203

Sample Code 204

Consequences of the Memento Pattern 209

Programs on GitHub 209

Chapter 26 The Observer Pattern 211

Example Program for Watching Colors Change 212

The Message to the Media 215

Consequences of the Observer Pattern 215

Programs on GitHub 215

Chapter 27 The State Pattern 217

Sample Code 217

Switching Between States 221

How the Mediator Interacts with the State

State Transitions 224

Programs on GitHub 224

Chapter 28 The Strategy Pattern 225

Why We Use the Strategy Pattern 225

Sample Code 226

The Context 227

The Program Commands 227

The Line and Bar Graph Strategies 228

Consequences of the Strategy Pattern 230

Programs on GitHub 231

Chapter 29 The Template Pattern 233

Why We Use Template Patterns 233

Kinds of Methods in a Template Class 234

Sample Code 234

Drawing a Standard Triangle 235

Drawing an Isosceles Triangle 236

The Triangle Drawing Program 237

Templates and Callbacks 238

Summary and Consequences 238

Example Code on GitHub 238

Chapter 30 The Visitor Pattern 239

When to Use the Visitor Pattern 239

Working with the Visitor Pattern 241

Sample Code 241

Visiting Each Class 242

Visiting Several Classes 242

Bosses Are Employees, Too 243

Double Dispatching 245

Traversing a Series of Classes 245

Consequences of the Visitor Pattern 245

Example Code on GitHub 245

PART V: A BRIEF INTRODUCTION TO PYTHON 247

Chapter 31 Variables and Syntax in Python 249

Data Types 250

Numeric Constants 250

Strings 250

Character Constants 251

Variables 252

Complex Numbers 253

Integer Division...

Details
Erscheinungsjahr: 2022
Fachbereich: Programmiersprachen
Genre: Importe, Informatik
Rubrik: Naturwissenschaften & Technik
Medium: Taschenbuch
Inhalt: Kartoniert / Broschiert
ISBN-13: 9780137579938
ISBN-10: 0137579934
Sprache: Englisch
Einband: Kartoniert / Broschiert
Autor: Cooper, James
Hersteller: Pearson Education (US)
Verantwortliche Person für die EU: preigu, Ansas Meyer, Lengericher Landstr. 19, D-49078 Osnabrück, mail@preigu.de
Maße: 233 x 180 x 20 mm
Von/Mit: James Cooper
Erscheinungsdatum: 23.02.2022
Gewicht: 0,572 kg
Artikel-ID: 120412315
Sicherheitshinweis