Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert...

15
Agile Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin Upper Saddle River, NJ • Boston • Indianapolis • San Francisco • • New York • Toronto • Montreal • London • Munich • Paris • Madrid HALL Capetown • Sydney • Tokyo • Singapore • Mexico City

Transcript of Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert...

Page 1: Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin ... Management of OOD Principles 492 Conclusion 493 Bibliography

AgilePrinciples, Patterns, and

Practices inC#

Robert C. MartinMicah Martin

Upper Saddle River, NJ • Boston • Indianapolis • San Francisco

• • New York • Toronto • Montreal • London • Munich • Paris • Madrid

HALL Capetown • Sydney • Tokyo • Singapore • Mexico City

Page 2: Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin ... Management of OOD Principles 492 Conclusion 493 Bibliography

Contents

Forewords xix

Preface xxiii

Acknowledgments xxxi

About the Authors xxxiii

Section I: Agile Development , 1

Chapter 1: Agile Practices 3The Agile Alliance 4

Individuals and Interactions over Processes and Tools 5Working Software over Comprehensive Documentation 6Customer Collaboration over Contract Negotiation 6Responding to Change over Following a Plan : 7

Principles 8Conclusion 10Bibliography 11

Chapter 2: Overview of Extreme Programming 13The Practices of Extreme Programming 14v Whole Team ;....14

User Stories 14Short Cycles 15Acceptance Tests 15Pair Programming 16Test-Driven Development (TDD) 17

Page 3: Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin ... Management of OOD Principles 492 Conclusion 493 Bibliography

vi Contents

Collective Ownership 17Continuous Integration ." 17Sustainable Pace 18Open Workspace 18The Planning Game 19Simple Design 19Refactoring 20Metaphor 21

Conclusion 22Bibliography 22

Chapter 3: Planning 23Initial Exploration 24

Spiking, Splitting, and Velocity 24Release Planning 25Iteration Planning 25Defining "Done" 26Task Planning 26Iterating 27Tracking 28Conclusion 29Bibliography 29

Chapter 4: Testing 31Test-Driven Development 32

Example of Test-First Design 32Test Isolation 33Serendipitous Decoupling 36

^Acceptance Tests 36Serendipitous Architecture 37Conclusion 38Bibliography 39

Page 4: Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin ... Management of OOD Principles 492 Conclusion 493 Bibliography

Contents vii

Chapter 5: Refactoring 41A Simple Example of Refactoring: Generating Primes 42

Unit Testing 44Refactoring 1..45The Final Reread 49

Conclusion 53Bibliography 54

Chapter 6: A Programming Episode 55The Bowling Game ....56Conclusion 98Overview of the Rules of Bowling 99

Section II: Agile Design 101

Chapter 7: What Is Agile Design? 103Design Smells 104

Design Smells—The Odors of Rotting Software 104Rigidity 105Fragility..... '. 105Immobility 105Viscosity 105Needless Complexity 106Needless Repetition 106Opacity 107

Why Software Rots 107The Copy Program..... 108

••* A Familiar Scenario 108Agile Design of the Copy Program. ..7T. I l l

Conclusion 113Bibliography 114

Page 5: Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin ... Management of OOD Principles 492 Conclusion 493 Bibliography

viii Contents

Chapter 8: The Single-Responsibility Principle (SRP) 115Defining a Responsibility 117Separating Coupled Responsibilities 119Persistence 119Conclusion 119Bibliography 120

Chapter 9: The Open/Closed Principle (OCP) 121.Description of OCP ,122The Shape Application 124

Violating OCP 124Conforming to OCP 127Anticipation and "Natural" Structure 128Putting the "Hooks" In 129Using Abstraction to Gain Explicit Closure 130Using a Data-Driven Approach to Achieve Closure 131

Conclusion '. 132Bibliography 133

Chapter 10: The Liskov Substitution Principle (LSP) 135Violations of LSP , 136

A Simple Example 136A More Subtle Violation 138A Real-World Example 143

Factoring Instead of Deriving 148Heuristics and Conventions , 150Conclusion 151Bibliography. , 151

Chapter 11: The Dependency-Inversion Principle (DIP) ...153Layering 154

Ownership Inversion 155Dependence on Abstractions 156

A Simple DIP Example 157Finding the Underlying Abstraction 158

The Furnace Example 160Conclusion 161Bibliography 162

Page 6: Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin ... Management of OOD Principles 492 Conclusion 493 Bibliography

Contents ix

Chapter 12: The Interface Segregation Principle (ISP) 163Interface Pollution 163Separate Clients Mean Separate Interfaces 165Class Interfaces versus Object Interfaces 166

Separation Through Delegation .....167Separation Through Multiple Inheritance 168

The ATM User Interface Example 169Conclusion 174Bibliography 175

Chapter 13: Overview of UML for C# Programmers 177Class Diagrams 180Object Diagrams 182

Sequence Diagrams 182Collaboration Diagrams 183State Diagrams 184Conclusion 185Bibliography ...185

Chapter 14: Working with Diagrams 187Why Model? 187

Why Build Models of Software? '.: : 188Should We Build Comprehensive Designs Before Coding? 188

Making Effective Use of UML 189Communicating with Others 189Road Maps 191Back-End Documentation 192What to Keep and What to Throw Away 192

^Iterative Refinement 194Behavior First 194Check the Structure 196Envisioning the Code 198Evolution of Diagrams 199

When and How to Draw Diagrams 200When to Draw Diagrams and When to Stop 200CASE Tools 201But What About Documentation? 202

Conclusion 202

Page 7: Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin ... Management of OOD Principles 492 Conclusion 493 Bibliography

x Contents

Chapter 15: State Diagrams 203The Basics ° 204

Special Events 205Superstates 206Initial and Final Pseudostates 207

Using FSM Diagrams 208Conclusion 209

Chapter 16: Object Diagrams 211A Snapshot in Time 212Active Objects 213Conclusion 217

Chapter 17: Use Cases 219Writing Use Cases 220

Alternate Courses 221What Else? 221

Diagramming Use Cases 222Conclusion 223Bibliography 223

Chapter 18: Sequence Diagrams. 225The Basics 226

Objects, Lifelines, Messages, and Other Odds and Ends 226Creation and Destruction : 227Simple Loops 228Cases and Scenarios 228

Advanced Concepts 232Loops and Conditions '.. 232Messages That Take Time 233Asynchronous Messages 235Multiple Threads 239Active Objects ...240Sending Messages to Interfaces 240

Conclusion 241

Page 8: Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin ... Management of OOD Principles 492 Conclusion 493 Bibliography

Contents xi

Chapter 19: Class Diagrams 243The Basics 244

Classes 244Association 245Inheritance : 246

An Example Class Diagram 247The Details 249

Class Stereotypes.... 249Abstract Classes 250Properties 251Aggregation •. 252Composition 253Multiplicity 254Association Stereotypes 255Nested Classes 256Association Classes 256Association Qualifiers.. ; , 257

Conclusion 258Bibliography 258

Chapter 20: Heuristics and Coffee 259The Mark IV Special Coffee Maker , 260

Specification 260A Common but Hideous Solution 263Imaginary Abstraction 265An Improved Solution 266Implementing the Abstract Model 270The Benefits of This Design 277

-OOverkill 279Bibliography !....292

Page 9: Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin ... Management of OOD Principles 492 Conclusion 493 Bibliography

xii Contents

Section III: The Payroll Case Study 293Rudimentary Specification of the Payroll System 294Exercise 295

Use Case 1: Add New Employee 295Use Case 2: Deleting an Employee 295Use Case 3: Post a Time Card 296Use Case 4: Posting a Sales Receipt 296

^ .. Use Case 5: Posting a Union Service Charge 296Use Case 6: Changing Employee Details 296Use Case 7: Run the Payroll for Today 297

Chapter 21: Command and Active Object: Versatilityand Multitasking 299

Simple Commands 300Transactions 302

Physical and Temporal Decoupling 304Temporal Decoupling 304

Undo Method , 304Active Object 305Conclusion 310Bibliography 310

Chapter 22: Template Method and Strategy: Inheritanceversus Delegation 311

Template Method 312Pattern Abuse '. 315Bubble Sor t 316

Strategy 319Conclusion 324Bibliography 324

Chapter 23: Facade and Mediator 325Facade 325Mediator 327Conclusion 329Bibliography 329

Page 10: Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin ... Management of OOD Principles 492 Conclusion 493 Bibliography

Contents xiii

Chapter 24: Singleton and Monostate ...331Singleton 332

Benefits .• 334Costs 334Singleton in Action : :...... 334

Monostate 336Benefits 337Costs 338Menpstate in Action 338

Conclusion 343Bibliography 343

Chapter 25: Null Object 345Description 345Conclusion 348Bibliography 348

Chapter 26: The Payroll Case Study: Iteration 1 ...349Rudimentary Specification 350Analysis by Use Cases ...351

Adding Employees 352Deleting Employees 353Posting Time Cards 354Posting Sales Receipts 354Posting a Union Service Charge 355Changing Employee Details 356Payday 358

Reflection: Finding the Underlying Abstractions 360" Employee Payment 360

Payment Schedule 360Payment Methods 362Affiliations 362

Conclusion 363Bibliography .....363

Page 11: Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin ... Management of OOD Principles 492 Conclusion 493 Bibliography

xiv Contents

Chapter 27: The Payroll Case Study: Implementation 365Transactions 366

Adding Employees.... 366Deleting Employees 372Time Cards, Sales Receipts, and Service Charges 373Changing Employees 381What Was I Smoking? 390Paying Employees 393Paying Salaried Employees 396Paying Hourly Employees 398

Main Program 408The Database 409Conclusion 411About This Chapter i 411Bibliography 412

Section IV: Packaging the Payroll System 413

Chapter 28: Principles of Packageand Component Design 415

Packages and Components 416Principles of Component Cohesion: Granularity 417

The Reuse/Release Equivalence Principle (REP) 417The Common Reuse Principle (CRP) 418The Common Closure Principle (CCP) 419Summary of Component Cohesion 420

Principles of Component Coupling: Stability 420The Acyclic Dependencies Principle (ADP) 420The Stable-Dependencies Principle (SDP) 426

% The Stable-Abstractions Principle (SAP) 431Conclusion ; 435

Chapter 29: Factory 437A Dependency Problem 440Static versus Dynamic Typing 441

Page 12: Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin ... Management of OOD Principles 492 Conclusion 493 Bibliography

Contents xv

Substitutable Factories 442Using Factories for Test Fixtures" 443Importance of Factories 444Conclusion 445Bibliography 445

Chapter 30: The Payroll Case Study: Package Analysis ....447Component Structure and Notation 448Applying the Common Closure Principle (CCP) 450Applying the Reuse/Release Equivalence Principle (REP) 452Coupling and Encapsulation 454Metrics 455Applying the Metrics to the Payroll Application 457

Object Factories 460Rethinking the Cohesion Boundaries 461

The Final Packaging Structure 463Conclusion 465Bibliography 465

Chapter 31: Composite 467Composite Commands 469Multiplicity or No Multiplicity 470Conclusion 470

Chapter 32: Observer: Evolving into a Pattern 471The Digital Clock ....: 472The Observer Pattern.... 491

Models 491, Management of OOD Principles 492

Conclusion 493Bibliography .„ > 494

Page 13: Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin ... Management of OOD Principles 492 Conclusion 493 Bibliography

xvi Contents

Chapter 33: Abstract Server, Adapter, and Bridge 495Abstract Server .; 496Adapter 498

The Class Form of Adapter 498The Modem Problem, Adapters, and LSP 499

Bridge 503Conclusion 505Bibliography ......506

Chapter 34: Proxy and Gateway: ManagingThird-Party APIs.... 507

Proxy 508Implementing Proxy 512Summary 525

Databases, Middleware, and Other Third-Party Interfaces 526Table Data Gateway 528

Testing and In-Memory TDGs 535Testing the DB Gateways 536

Using Other Patterns with Databases 539Conclusion 541Bibliography 541

Chapter 35: Visitor .543VISITOR 544

Acyclic Visitor 548Uses of VISITOR 552

Decorator 560Extension Object 565Conclusion 576Bibliography ...577

Chapter 36: State 579Nested Switch/Case Statements 580

The Internal Scope State Variable 583Testing the Actions 583Costs and Benefits 583

Page 14: Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin ... Management of OOD Principles 492 Conclusion 493 Bibliography

Contents xvii

Transition Tables 584Using Table Interpretation r 585Costs and Benefits ....586

The State Pattern ...586State versus Strategy 589Costs and Benefits 590The State Machine Compiler (SMC) .....591Turnstile.es Generated by SMC, and Other Support Files 593

Classes of State Machine Application 598High-Level Application Policies for GUIs 599GUI Interaction Controllers 600Distributed Processing 601

Conclusion 602Bibliography 602

Chapter 37: The Payroll Case Study: The Database 603Building the Database 604AFlaw in the Code Design 605Adding an Employee 607Transactions 618Loading an Employee 623What Remains? 636

Chapter 38: The Payroll User Interface: Model ViewPresenter 637

The Interface 639Implementation 640Building a Window 650

* The Payroll Window 657The Unveiling 669Conclusion 670Bibliography '. 670

Page 15: Agile Principles, Patterns, and Practices in C# · Principles, Patterns, and Practices in C# Robert C. Martin Micah Martin ... Management of OOD Principles 492 Conclusion 493 Bibliography

xviii Contents

Appendix A: A Satire of Two Companies 671Rufus Inc.: Project Kickoff" 671Rupert Industries: Project Alpha 671

Appendix B: What Is Software? 687

Index 699