Agile Software Development Principles, Patterns, and Practices Robert Cecil Martin
Alan Apt Series
Prentice Hall Pearson Education, Inc. Upper Saddle River, New Jersey 07458
Brief Contents
Section 1 Agile Development
1
3
Chapter 1
Agile Practices
Chapter 2
Overview of Extreme Programming
11
Chapter 3
Planning
19
Chapter 4
Testing
23
Chapter 5
Refactoring A Programming Episode
31
Chapter 6
Section 2 Agile Design
43
85
Chapter 7
What Is Agile Design?
87
Chapter 8
SRP: The Single-Responsibility Principle
95
Chapter 9
OCP: The Open—Closed Principle
99
Chapter 10 LSP: The Liskov Substitution Principle
111
Chapter 11 DIP: The Dependency-Inversion Principle
127
Chapter 12 ISP: The Interface-Segregation Principle
135
Section 3 The Payroll Case Study
147
Chapter 13 COMMAND and ACTIVE OBJECT
151
Chapter 14 TEMPLATE METHOD & STRATEGY: Inheritance vs. Delegation Chapter 15 FACADE and MEDIATOR
Chapter 16 SINGLETON and MONOSTATE
161 173
177
x
Brief Contents
xi
Chapter 17 NULL OBJECT
189
Chapter 18 The Payroll Case Study: Iteration One Begins Chapter 19 The Payroll Case Study: Implementation
193 205
Section 4 Packaging the Payroll System
251
Chapter 20 Principles of Package Design
253
Chapter 21 FACTORY Chapter 22 The Payroll Case Study (Part 2)
269 275
Section 5 The Weather Station Case Study
291
Chapter 23 COMPOSITE
293
Chapter 24 OBsERvER—Backing into a Pattern Chapter 26 PROXY and STAIRWAY TO HEAVEN: Managing Third Party APIs
297 317 327
Chapter 27 Case Study: Weather Station
355
Section 6 The ETS Case Study
385
Chapter 28 VISITOR
Appendix D The Source Code Is the Design
387 419 443 467 489 507 517
Index
525
Chapter 25 ABSTRACT SERVER, ADAPTER, and BRIDGE
Chapter 29 STATE Chapter 30 The ETS Framework Appendix A UML Notation I: The CGI Example Appendix B UML Notation II: The STATMUX Appendix C A Satire of Two Companies
Contents
Foreword Preface
iii iv
About the Authors
ix
List of Design Patterns Section 1 Agile Development
1 3
Chapter 1 Agile Practices
4 4 6 8 9
The Agile Alliance The Manifesto of the Agile Alliance Principles Conclusion Bibliography
Chapter 2 Overview of Extreme Programming The Practices of Extreme Programming Customer Team Member User Stories Short Cycles Acceptance Tests Pair Programming Test-Driven Development Collective Ownership Continuous Integration Sustainable Pace Open Workspace The Planning Garne Simple Design Refactoring Metaphor
11 11 11 12 12 13 13 14 14 14 15 15 15 15 16 16
17 17
Conclusion Bibliography
xl
i
Contents
Chapter 3 Planning
Initial Exploration
19
Spiking, Splitting, and Velocity
Release Planning Iteration Planning Task Planning
The Halfway Point Iterating Conclusion
Bibliography
Chapter 4 Testing
20 20 20 21 21
22 22 22 22
Test Driven Development
23
An Example of Test-First Design Test Isolation Serendipitous Decoupling Acceptance Tests Example of Acceptance Testing Serendipitous Architecture Conclusion Bibliography
Chapter 5 Refactoring
23 24 25 26 27 27 29 29 29
Generating Primes: A Simple Example of Refactoring The Final Reread Conclusion
Bibliography
The Bowling Garne Conclusion
32 38 42
43
44 82
85
Symptoms of Poor Design Principles Smells and Principles
Bibliography
31
42
Chapter 6 A Programming Episode
Section 2 Agile Design
85 86 86 86
Chapter 7 What Is Agile Design?
87
What Goes Wrong with Software? Design Smells—The Odors of Rotting Software What Stimulates the Software to Rot? Agile Teams Don't Allow the Software to Rot
The "Copy" Program Agile Design of the Copy Example
How Did the Agile Developers Know What to Do?
Keeping the Design As Good As It Can Be Conclusion Bibliography
87 88 89 90 90 93 94 94 94 94
Contents
xiv
Chapter 8 SRP: The Single-Responsibility Principle
95
A CLASS SHOULD HAVE ONLY ONE REASON TO CHANGE.
SRP: The Single-Responsibility Principle What Is a Responsibility? Separating Coupled Responsibilities Persistence
Conclusion Bibliography Chapter 9 OCP: The Open-Closed Principle
95 97 97 98
98 98
99
SOFTWARE ENTITIES (CLASSES, MODULES, FUNCTIONS, ETC.) SHOULD BE OPEN FOR EXTENSION, BUT CLOSED FOR MODIFICATION.
OCP: The Open—Closed Principle Description Abstraction Is the Key The Shape Application Violating the OCP Conforming to the OCP OK, I Lied Anticipation and "Natural" Structure Putting the "Hooks" In Using Abstraction to Gain Explicit Closure Using a "Data-Driven" Approach to Achieve Closure Conclusion
Bibliography Chapter 10 LSP: The Liskov Substitution Principle
99 100 100 101 101 103 104 105 105 106 107
108 109
111
SUBTYPES MUST BE SUBSTITUTABLE FOR 7HEIR BASE TYPES.
LSP: The Liskov Substitution Principle A Simple Example of a Violation of the LSP Square and Rectangle, a More Subtle Violation
111 112 113
The Real Problem Validity Is Not Intrinsic ISA Is about Behavior Design by Contract Specifying Contracts in Unit Tests A Real Example
115 116 116 117 117
Motivation
117 118
Problem A Solution That Does Not Conform to the LSP An LSP-Compliant Solution
119 120 120
Factoring Instead of Deriving Heuristics and Conventions Degenerate Functions in Derivatives Throwing Exceptions from Derivatives
Conclusion Bibliography
121 124 124 124
125 125
Contents
xv
Chapter 11 DIP: The Dependency-Inversion Principle
127
A. HIGH-LEVEL MODULES SHOULD NOT DEPEND UPON LOW-LEVEL MODULES. BOTH SHOULD DEPEND ON ABSTRACTIONS. B. ABSTRACTIONS SHOULD NOT DEPEND ON DETAILS. DETAILS SHOULD DEPEND ON ABSTRACTIONS.
DIP: The Dependency-Inversion Principle Layering An Inversion of Ownership Depend an Abstractions A Simple Example Finding the Underlying Abstraction The Furnace Example Dynamic v. Static Polymorphism Conclusion Bibliography
Chapter 12 ISP: The Interface-Segregation Principle Interface Pollution Separate Clients Mean Separate Interfaces The Backwards Force Applied by Clients Upon Interfaces
127 128 128 129 130 131 132 133 134 134
135 135 137 137
CLIENTS SHOULD NOT BE FORCED TO DEPEND ON METHODS THAT THEY DO NOT USE.
ISP: The Interface-Segregation Principle Class Interfaces v. Object Interfaces Separation through Delegation Separation through Multiple Inheritance The ATM User Interface Example The Polyad v. the Monad Conclusion Bibliography
Section 3 The Payroll Case Study Rudimentary Specification of the Payroll System Exercise Use Case 1: Add New Employee Use Case 2: Deleting an Employee Use Case 3: Post a Time Card Use Case 4: Posting a Sales Receipt Use Case 5: Posting a Union Service Charge Use Case 6: Changing Employee Details Use Case 7: Run the Payroll for Today
137 138 138 139 139 144 145 145
147 148 148 148 149 149 149 150 150 150
Chapter 13 COMIVIAND and ACTIVE OBJECT
151
Simple Commands Transactions Physical and Temporal Decoupling Temporal Decoupling UNDO
152 153 154 154 154
Contents xvi
155 159 159
ACTIVE OBJECT
Conclusion Bibliography
Chapter 14 TEMPLATE METHOD & STRATEGY: Inheritance vs. Delegation TEMPLATE METHOD Pattern Abuse Bubble Sort STRATEGY Sorting Again Conclusion
Bibliography
Chapter 15 FACADE and MEDIATOR
162 164 165
168 170
172 172
173 173 174 176 176
FACADE MEDIATOR Conclusion
Bibliography
Chapter 16 SINGLETON and MONOSTATE SINGLETON Benefits of the SINGLETON Costs of the SINGLETON SINGLETON in
161
Action
177 178 179 179 179
MONOSTATE Benefits of MONOSTATE Costs of MONOSTATE MONOSTATE in Action
180
Conclusion Bibliography
187 187
Chapter 17 NULL OBJECT Conclusion Bibliography
Chapter 18 The Payroll Case Study: Iteration One Begins
182 182 182
189 192 192
193
Introduction
193
Specification Analysis by Use Cases
194
Adding Employees Deleting Employees Posting Time Cards
Posting Sales Receipts Posting a Union Service Charge Changing Employee Details Payday
Reflection: What Have We Learned? Finding the Underlying Abstractions The Schedule Abstraction
193 195 196 196 197 197 198 199
201 201 201
Contents
xvii
Payment Methods Affiliations Conclusion
Bibliography Chapter 19
202 202 203 203
The Payroll Case Study: Implementation
205
Adding Employees
206 207 209 212 213 214 220 224 229 233 235 235 237 241 248 248 249 249 250 250
The Payroll Database Using TEMPLATE METHOD to Add Employees Deleting Employees Global Variables Time Cards, Sales Receipts, and Service Charges
Changing Employees Changing Classification What Was 1 Smoking? Paying Employees Do We Want Developers Making Business Decisions? Paying Salaried Employees Paying Hourly Employees Pay Periods: A Design Problem Main Program The Database Summary of Payroll Design History Resources
Bibliography Section
4 Packaging the Payroll System
251
Chapter
20 Principles of Package Design
253
Designing with Packages? Granularity: The Principles of Package Cohesion The Reuse–Release Equivalence Principle (REP)
253 254 254
THE GRANULE OF REUSE IS THE GRANULE OF RELEASE.
The Common-Reuse Principle (CRP)
255
THE CLASSES INA PACKAGE ARE REUSED TOGETHER. IF YOU REUSE ONE OF THE CLASSES IN A PACKAGE, YOU REUSE THEM ALL.
The Common-Closure Principle (CCP)
256
THE CLASSES INA PACKAGE SHOULD BE CLOSED TOGETHER AGAINST THE SAME KINDS OF CHANGES. A CHANGE THAT AFFECTS A PACKAGE AFFECTS ALL THE CLASSES IN THAT PACKAGE AND NO OTHER PACKAGES.
Summary of Package Cohesion Stability: The Principles of Package Coupling The Acyclic-Dependencies Principle (ADP)
256 256 256
ALLOW NO CYCLES IN THE PACKAGE DEPENDENCY GRAPH.
The Weekly Build Eliminating Dependency Cycles The Effect of a Cycle in the Package Dependency Graph
257 257 258
Contents
xviii
Breaking the Cycle The "Jitters"
Top-Down Design The Stable-Dependencies Principle (SDP)
259 259
260 261
DEPEND IN THE DIRECTION OF STABILITY.
Stability Stability Metrics Not All Packages Should Be Stable Where Do We Put the High-level Design?
The Stable-Abstractions Principle (SAP) A PACKAGE SHOULD BE AS ABSTRACT AS IT IS STABLE. Measuring Abstraction The Main Sequence Distance from the Main Sequence
261 262 263 264
264 265 265 266
Conclusion
268
Chapter 21 FACTORY
269
A Dependency Cycle Substitutable Factories Using Factories for Test Fixtures How Important Is It to Use Factories? Conclusion Bibliography Chapter 22 The Payroll Case Study (Part 2) Package Structure and Notation Applying the Common Closure Principle (CCP) Applying the Reuse–Release Equivalency Principle (REP) Coupling and Encapsulation Metrics Applying the Metrics to the Payroll Application Object Factories The Object Factory for Transactionlmplementation Initializing the Factories Rethinking the Cohesion Boundaries
The Final Package Structure Conclusion Bibliography
271 272 273 274 274 274 275
276 277 278 279 281 282 285 286 286 287
287 290 290
Section 5 The Weather Station Case Study
291
Chapter 23 COMPOSITE Example: Composite Commands Multiplicity or Not Multiplicity
293
Chapter 24 OBSERVER— Backing into a Pattern The Digital Clock
294 295
297 297
Contents
xix
Conclusion The Use of Diagrams in this Chapter
314 314
The OBSERVER Pattern How OBSERVER Manages the Principles of OOD Bibliography
315 316 316
Chapter 25 ABSTRACT SERVER, ADAPTER, and BRIDGE
317
ABSTRACT SERVER
Who Owns the Interface? Adapter The Class Form of ADAPTER The Modem Problem, ADAPTERS and LSP BRIDGE
Conclusion Bibliography
Chapter 26 PROXY and STAIRWAY TO HEAVEN: Managing Third Party APIs PROXY
Proxifying the Shopping Cart Summary of PROXY Dealing with Databases, Middleware, and Other Third Party Interfaces STAIRWAY TO HEAVEN
Example of STAIRWAY TO HEAVEN Other Patterns That Can Be Used with Databases Conclusion Bibliography
Chapter 27 Case Study: Weather Station The Cloud Company The WMS-LC Software Language Selection Nimbus-LC Software Design 24-Hour History and Persistence Implementing the HiLo Algorithms Conclusion Bibliography Nimbus-LC Requirements Overview Usage Requirements 24-Hour History User Setup Administrative Requirements Nimbus-LC Use Cases Actors Use Cases Measurement History Setup Administration Nimbus-LC Release Plan Introduction Release I
318 318 319 319
320 322 324 325
327 327 332 344 345
347 348 353 354 354
355 355 356 357 357 368 371 379 379 379 379 379 379 380 380 380 380 380 381 381 381 381 381
Contents XX
382 382 382 382 383 383 383 383 383 383
Risks Deliverable(s) Release II Use Cases Implemented Risks Deliverable(s) Release HI Use Cases Implemented Risks Deliverable(s)
Section 6 The ETS Case Study
385
Chapter 28 VISITOR Family of Design Patterns The VIKTOR VISITOR a Matrix VISITOR is Like ACYCLIC VISITOR ACYCLIC VISITOR Is Like a Sparse Matrix
Generators Using VISITOR in Report VISITOR Other Uses of DECORATOR
Multiple Decorators
EXTENSION OBJECT
Conclusion
Reminder
Bibliography
Chapter 29 STATE Overview of Finite State Automata Implementation Techniques Nested Switch/Case Statements Interpreting Transition Tables
The STATE Pattern SMC—The State-Machine Compiler Where Should State Machines be Used? High-Level Application Policies for GUIs GUI Interaction Controllers Distributed Processing
Conclusion Listings Turnstile . java Using Table Interpretation Turnstile . j ava Generated by SMC, and Other Support Files
Bibliography Chapter 30 The ETS Framework Introduction Project Overview
387 388 388 391 391 396 396 402 403 406 408 418 418 418 419 419 421 421 424
426 429 432 432 433
433 434 434 434 437 441 443 443 443
Contents
xxi
Early History 1993-1994 Framework?
Framework!
445 445
446
The 1994 Team The Deadline The Strategy Results
446 446 446 447
Framework Design
448
The Common Requirements of the Scoring Applications The Design of the Scoring Framework A Case for TEMPLATE METHOD
448 450
Write a Loop Once The Common Requirements of the Delivery Applications The Design of the Delivery Framework The Taskmaster Architecture Conclusion Bibliography Appendix A UML Notation I: The CGI Example Course Enroliment System: Problem Description Actors Use Cases The Domain Model The Architecture Abstract Classes and Interfaces in Sequence Diagrams Summary Bibliography
453 454 456 457 462 465 466
467 468 469 469 472 476 485 486 487
Appendix B UML Notation II: The STATMUX The Statistical Multiplexor Definition The Software Environment The Real-time Constraints The Input Interrupt Service Routine The Output Service Interrupt Routine The Communications Protocol Conclusion Bibliography
489
Appendix C A Satire of Two Companies
507
Rufus, Inc. Project Kickoff Rupert Industries Project: -Alpha-
489 490 490 491 495 496 506 506
507 507
Appendix D The Source Code Is the Design What Is Software Design? Afterword
517
Index
525
517 523