UML for Java Programmers Robert Cecil Martin Object Mentor Inc Prentice Hall, Englewood Cliffs, New Jersey 07632 Don’t Panic Do n’t Panic Do n’t Panic Do n’t Panic Don ’t Panic Don ’t Panic Don ’t Panic Don ’t Panic Don’t Panic Don’t Panic Don’t Panic Don’t Panic Don’t Panic Don’t Panic Don’t Panic Don’t Panic Don’t Pan ic Don’t Pan ic Don’t Pan ic Don’t Panic Don’t Panic Don’t Panic Do n’t Panic Do n’t Panic Do n’t Panic Do n’t Panic Don ’t Panic Don ’t Panic Don ’t Panic Martin, Robert Cecil The Principles, Practices, & Patterns of Agile Software Development /Robert Cecil Martin p cm “An Alan R Apt Book.” Includes index ISBNxxxxxxxxx Publisher: Alan Apt Production Editor: Cover Designer: Copy Editor: © 2002 by Prentice-Hall, Inc A Simon & Schuster Company The author and publisher of this book have used their best efforts in preparing this book These efforts include the development, research, and testing of the theories and programs to determine their effectiveness The author and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs All rights reserved No part of this book may be reproduced, in any form or by any means, without permission in writing from the publisher Printed in the United States of America 10 ISBN 0-13-203837-4 PRENTICE-HALL INTERNATIONAL (UK) Limited, London PRENTICE-HALL OF A USTRALIA PTY LIMITED, Sydney PRENTICE-HALL CANADA, I NC., Toronto PRENTICE-HALL HISPANOAMERICANA, S.A., Mexico PRENTICE-HALL OF I NDIA PRIVATE LIMITED, PRENTICE-HALL OF J APAN, INC., New Delhi Tokyo SIMON & SCHUSTER ASIA PTE LTD., Singapore EDITORA PRENTICE-H ALL DO BRASIL , LTDA., Rio de Janeiro This book is dedicated to my grandchildren: XXX: the son of Micah and Angelique Alexis: the daughter of Angela and Matt It has been said that grandchildren are the desert of life If that’s so, what am I supposed to with all the many main courses I’m not done with yet? Source Code and Contact Information: Much of the source code presented in this book can be obtained from the Object Mentor Inc web site www.objectmentor.com/UMLFJP Robert C Martin: unclebob@objectmentor.com Object Mentor Inc.: info@objectmentor.com www.objectmentor.com i Chapter : Chapter 1: Overview of UML for Java Programmers Diagram Types Class Diagrams Object Diagrams .5 Sequence Diagrams Collaboration Diagrams .6 State Diagrams Conclusion Bibliography Chapter 2: Working with Diagrams Why Model? Why build models of software? .10 Why should we build comprehensive designs before coding? 10 Making Effective use of UML 10 Communicating with Others .11 Back end Documentation 13 What to keep, and What to throw away 14 Iterative Refinement 15 Behavior first 15 Check the structure 17 Envisioning the code 19 Iterative Refinement 20 Minimalism 21 When and how to draw diagrams 21 When to draw diagrams, and when to stop 21 CASE Tools 22 But what about documentation? .23 And Javadocs? 23 Conclusion 24 Chapter 3: Class Diagrams 25 ii The Basics 25 Classes 25 Association .26 Multiplicity .26 Inheritance 27 An Example Class Diagram .28 The Details 30 Class Stereotypes 30 Abstract classes 31 Properties 31 Aggregation 32 Composition .33 Multiplicity .34 Association Stereotypes 35 Inner Classes 36 Anonymous Inner Classes 36 Association classes 37 Association Qualifiers 38 Conclusion 38 Bibliography 39 Chapter 4: Sequence Diagrams 41 The Basics 41 Objects, Lifelines, Messages, and other odds and ends 41 Creation and Destruction 43 Simple Loops 44 Cases and Scenarios 44 Advanced Concepts 48 Loops and Conditions .48 Messages that take time 49 Asynchronous Messages .51 Multiple Threads 53 Active Objects 54 Sending Messages to Interfaces 54 Conclusion 56 iii Chapter : Chapter 5: Use Cases .57 Writing Use Cases .57 What is a use case 58 The Primary Course 58 Alternate Courses .59 What else? 59 Use Cases Diagrams 60 System Boundary Diagram 60 Use Case Relationships 61 Conclusion 61 Chapter 6: Principles of OOD 63 Design Quality .63 Design Smells 63 Dependency Management 64 The Single Reponsibility Principle (SRP) 64 The Open Closed Principle (OCP) 66 The Liskov Substitution Principle (LSP) 78 The Dependency Inversion Principle (DIP) 80 The Interface Segregation Principle 81 Conclusion 82 Bibliography 83 Chapter 7: The Practices: dX .85 Iterative Development .85 The Initial Exploration .85 Estimating the features .86 Spikes .87 Planning .87 Planning Releases 87 Planning Iterations 87 The midpoint .88 iv Velocity Feedback 89 Organizing the Iterations into Management Phases 89 What’s in an Iteration? .89 Developing in Pairs 90 Acceptance Tests 90 Unit Tests 91 Refactoring .91 Open Office 92 Continuous Integration .92 Conclusion 92 Bibliography 93 Chapter 8: Packages 95 Java Packages 95 Packages 95 Dependencies 96 Binary Components jar files 97 Principles of Package Design .97 The Release/Reuse Equivalency Principle (REP) 98 The Common Closure Principle (CCP) 98 The Common Reuse Principle (CRP) 99 The Acyclic Dependencies Principle (ADP) 99 The Stable Dependencies Principle (SDP) .99 The Stable Abstractions Principle (SAP) .100 Conclusion 100 Chapter 9: Object Diagrams 103 A Snapshot in Time 103 Active Objects 105 Conclusion 108 Chapter 10: State Diagrams 109 v Chapter : The Basics 109 Special Events 110 Super States 111 Initial and Final Pseudo States .113 Using FSM Diagrams 113 SMC .114 ICE: A Case Study 116 Conclusion 121 Chapter 11: Heuristics and Coffee .123 The Mark IV Special Coffee Maker 123 A Challenge 126 A Common, but Hideous, Coffee Maker Solution .126 MissingMethods 126 Vapor Classes .127 Imaginary Abstraction 128 God Classes 129 A Coffee Maker Solution 129 Crossed Wires 130 The Coffee Maker User Interface 131 Use Case 1: User pushes brew button .131 Use Case 2: Containment Vessel not Ready .132 Use Case 3: Brewing Complete 132 Use Case 4: Coffee all gone 134 Implementing the Abstract Model 134 Use Case User pushes Brew Button (Mark IV) 135 Implementing the isReady() functions 136 Implementing the start() functions 137 How does M4UserInterface.checkButton get called? 138 Completing the Coffee Maker 139 The Benefits of this design 141 How did I really come up with this design? 141 Chapter 12: SMC Remote Service: Case Study .153 ... Overview of UML for Java Programmers The Unified Modeling Language (UML) is a graphical notation for drawing diagrams of software concepts One can use it for drawing diagrams of a... Mentor Inc.: info@objectmentor.com www.objectmentor.com i Chapter : Chapter 1: Overview of UML for Java Programmers Diagram Types Class Diagrams Object Diagrams ... execution by depicting the flow of execution, or the way entities change Chapter : Overview of UML for Java Programmers state Physical diagrams show the unchanging physical structure of software entities