معرفی کتاب «Bitter Java» نوشتهٔ Bruce A. Tate، منتشرشده توسط نشر Manning Publications; Pearson Education در سال 2002. این کتاب در فرمت pdf، زبان انگلیسی ارائه شده است. «Bitter Java» در دستهٔ بدون دستهبندی قرار دارد.
Bruce Tate tried to make learning Java more fun. He failed miserable. What is this? A novel? A sports book? More than 50% of the contents talks about his kayaking and sporting experience. I was surprised that at the end of the book I did not find his list of dates. Luckily I bought it used. Stay away from this book.It is a well-known fact that most software projects fail. Drawing important lessons from common failures is the goal of Bitter Java.Reusing design patterns is not enough for success: patterns are like partial maps of dangerous terrain. They help, but don't prevent you from getting lost. Bitter Java teaches you how to recognize when you are lost, and how to get back on the right path. It illustrates common pitfalls of Java programming through code examples; it then refactors the code and explains why the new solutions are safe.about the bookThis book is a systematic account of common server-side Java programming mistakes, their causes and solutions. It covers antipatterns for base Java and J2EE concepts such as Servlets, JSPs, EJBs, enterprise connection models, and scalability.what's insideAfter studying antipatterns in this book such as:* Round-tripping* The magic servlet* The cacheless cow* Performance thrashingYou will be standing on the shoulders of those who failed before you.about the readerIf you are an intermediate Java programmer, analyst or architect eager to avoid the bitter experiences of others, this book is for you.about the authorBruce A. Tate is an Internet architect who developed the bitter Java concept after seeing a set of customer problems repeated and decided to collect these stories and publish their solutions. He is the coauthor of Objects for OS/2. He lives in Austin, Texas. contents 6 foreword 16 preface 18 acknowledgments 22 about this book 24 about the cover illustration 28 The basics 30 Bitter tales 32 1.1 A Java development free fall 33 1.1.1 Antipatterns in life 35 1.2 Using design patterns accentuates the positive 36 1.2.1 Design patterns online 37 1.2.2 UML provides a language for patterns 38 1.3 Antipatterns teach from the negative 38 1.3.1 Some well-known antipatterns 39 1.3.2 Antipatterns in practice 40 1.3.3 Antipattern resources 41 1.4 Antipattern ideas are not new 42 1.4.1 Learning from the industry 43 1.4.2 Detective work 44 1.4.3 Refactoring antipatterns 46 1.5 Why Bitter Java? 46 1.5.1 The Bitter Java approach 47 1.5.2 Bitter Java tools 47 1.5.3 The Bitter Java organization 48 1.5.4 The Bitter Java audience 50 1.6 Looking ahead 50 The bitter landscape 52 2.1 Fertile grounds for antipatterns 53 2.1.1 The benefits of layering 53 2.1.2 Layering can work against us 55 2.2 Internet technologies 57 2.2.1 Internet topologies affect our applications 57 2.2.2 Enterprise layers add security and overhead 58 2.2.3 Standards enable the Internet and add layers 60 2.2.4 TCP and IP provide low-level communications 61 2.2.5 HTTP provides application-level transport 62 2.2.6 HTML and XML 63 2.2.7 Mini-antipattern: Too Many Web Page Items 64 2.3 Object technologies and antipatterns 66 2.3.1 Encapsulation helps to isolate change 67 2.3.2 Inheritance enables packaging of common behavior 67 2.3.3 Polymorphism enables flexible reuse 68 2.3.4 Mini-antipatterns: Excessive Layering 68 2.3.5 Setting the stage for Java 71 2.4 Java technologies solve antipatterns 71 2.5 Major problems with the waterfall 73 2.5.1 Iterative methodologies 74 2.5.2 Mini-antipatterns: Incomplete Process Transitions 74 2.5.3 Programming horizons: Extreme programming 75 2.6 A quick survey of the bitter landscape 77 2.7 Antipatterns in this chapter 77 Server-side Java antipatterns 80 Bitter servlets 82 3.1 Getting off on the wrong foot 83 3.1.1 An early antipattern: The Magic Pushbutton 83 3.1.2 Building with Model-View-Controller 85 3.1.3 Failing to separate model and view 85 3.1.4 Breaking out the model 87 3.2 Antipattern: The Magic Servlet 88 3.2.1 Can we use servlets as the model? 89 3.2.2 Stumbling into the Magic Servlet trap 91 3.2.3 Causes of the Magic Servlet 95 3.3 Solution: Refactor using commands 96 3.3.1 Break out the model 96 3.3.2 Wrapping the model with command objects 97 3.3.3 Separating the model logic 98 3.3.4 Separating the return trip 103 3.3.5 Using a JSP for the return trip 106 3.4 Summary 108 3.5 Antipattern in this chapter 108 Bitter JSPs 110 4.1 Getting only halfway home 111 4.1.1 Recognizing the danger signs 111 4.2 Antipattern: Monolithic JSPs 113 4.2.1 This program lacks model-view separation 113 4.2.2 Solution: Refactor to Model-View-Controller 115 4.3 Antipattern: Compound JSPs 117 4.3.1 Should we combine multiple JSPs? 117 4.3.2 An example combining two interfaces 118 4.3.3 Solution: Split the JSP 123 4.3.4 Making decisions in the controller servlet 123 4.4 Mini-antipatterns: Coarse and Fine Commands 127 4.4.1 Too many commands in a group 128 4.4.2 Solution: Refactor to appropriate granularity 128 4.4.3 Tips for granularity 130 4.5 Mini-antipattern: Fat Commands 131 4.6 Reviewing the JSP antipatterns 131 4.7 Antipatterns in this chapter 132 Bitter cache management 136 5.1 We need caches! 137 5.2 Antipattern: The Cacheless Cow 138 5.2.1 Bitter BBS with no cache 139 5.2.2 Building the model, view, and controller for ShowBoard 141 5.2.3 Building the model, view, and controller for ShowThread 144 5.2.4 Building the model, view and controller for AddPost 148 5.2.5 Performance problems 154 5.3 Solution: Cache 154 5.3.1 Solution 1: Use a hardware cache 155 5.3.2 Solution 2: Cache commands 155 5.3.3 Adding a cache to our BBS 157 5.3.4 Possible enhancements to cached commands 162 5.4 Cache-related mini-antipatterns 164 5.4.1 Concurrent access to static cache 164 5.4.2 The ever-growing cache 165 5.5 Antipattern: Synchronized Read/Write Bottlenecks 165 5.5.1 Collisions between readers can hurt performance 166 5.5.2 Read/write locks allow correct shared access 167 5.6 Cooking the Cacheless Cow 169 5.7 Antipatterns in this chapter 169 Bitter memories 172 6.1 Understanding memory leaks and antipatterns 173 6.1.1 Managing memory 174 6.1.2 Understanding garbage collection 175 6.1.3 Reference counting 175 6.1.4 Reachable objects 177 6.2 Trading C++ for Java 178 6.2.1 Circumstances that cause Java memory leaks 178 6.2.2 Finding Java leaks 179 6.3 Antipattern: Lapsed Listeners Leak 180 6.3.1 Examining some dangerous practices 181 6.3.2 Solution 1: Explicitly remove the listeners 184 6.3.3 Solution 2: Shorten the life cycle of the anchor 184 6.3.4 Solution 3: Weaken the reference 185 6.3.5 Reference objects simplify memory management 185 6.4 Antipattern: The Leak Collection 186 6.4.1 Causing trouble with caches and session state 187 6.4.2 Solution 1: Search for common warning signs 188 6.4.3 Solution 2: Aggressively pair adds with removes 189 6.4.4 Solution 3: Use soft references for caches 189 6.4.5 Solution 4: Use collections with weak references 190 6.4.6 Solution 5: Use finally 190 6.5 Shooting memory leaks 190 6.5.1 Make sure there is a leak 190 6.5.2 Determine that the leak should be fixed 191 6.5.3 Isolate the problem 192 6.5.4 Determine the source and fix the problem 193 6.5.5 Protect against the problem for the future 194 6.6 Mini-Antipatterns: Little Hogs 195 6.6.1 String manipulation 195 6.6.2 Collections 196 6.6.3 Inheritance chains 197 6.7 Summary 197 6.8 Antipatterns in this chapter 198 Bitter connections and coupling 200 7.1 Making connections 201 7.2 Antipattern: Connection Thrashing 201 7.2.1 Creating and terminating with every access 203 7.2.2 Solution: Reuse connections with a pool 203 7.2.3 Refactoring our BBS to add pooled connections 206 7.2.4 Using getPooledConnection 208 7.2.5 Using the J2EE connector architecture 209 7.3 Antipattern: Split Cleaners 210 7.3.1 Exceptions can lead to Split Cleaners 212 7.3.2 Solution: Pair connection with cleanup, in finally 213 7.4 Antipattern: Hardwired Connections 214 7.4.1 The communications buffer 215 7.4.2 Premature binding 218 7.4.3 Solution 1: Decouple with XML messages 218 7.4.4 Solution 2: Delay binding with web services 220 7.5 Mini-antipatterns for XML misuse 221 7.5.1 XML’s Golden Hammers 222 7.5.2 XML’s bitter transitions 222 7.6 Mini-antipatterns: Rigid XML 223 7.6.1 Name collisions 224 7.6.2 Rigid constructs 226 7.6.3 Restrictive variable-content containers 228 7.6.4 XML versioning 230 7.7 Summary: Sweetening bitter connections 231 7.8 Antipatterns in this chapter 232 Bitter beans 236 8.1 A brief Enterprise JavaBeans review 237 8.1.1 The component-based distributed architecture 237 8.1.2 Types of EJBs 238 8.2 Bitter BBS with EJBs 239 8.2.1 Elements of an EJB application 240 8.2.2 Building the remote interface 242 8.2.3 Creating the home interface 244 8.2.4 Implementing the bean class 245 8.2.5 Defining the primary key 250 8.2.6 Creating a deployment descriptor 251 8.2.7 Using the model 253 8.3 Antipattern: Round-tripping 254 8.3.1 Computing the cost of a distributed deployment 255 8.3.2 Chatty interfaces 256 8.3.3 Solution: Group together round-trips with a facade 257 8.3.4 Roots of round-tripping 258 8.3.5 Refactoring the BBS with a facade 259 8.4 Antipattern: Square Bean in a Round Hole 266 8.4.1 Mini-antipattern: Bean-Managed Joins 266 8.4.2 Solution: Views, mappers, bean-managed joins 267 8.4.3 Mini-antipattern: Entity Beans for Lightweight Functions 267 8.4.4 Mini-antipattern: Entities for Read Only 269 8.4.5 Mini-antipattern: Entity Beans for Write but Not Read 269 8.4.6 Troublesome scrollable lists 269 8.4.7 Overall solution: Pick the right bean for the job 270 8.5 Mini-antipattern: Everything Is an EJB 271 8.6 EJBs and caching 272 8.6.1 Implementing a cache with a facade 272 8.7 Smoothing out the bitter beans 273 8.8 Antipatterns in this chapter 274 The big picture 278 Bitter hygiene 280 9.1 Why study programming hygiene? 281 9.1.1 Extreme programming requires good hygiene 281 9.1.2 Coding standards protect against antipatterns 282 9.2 Mini-antipatterns: Unreadable code 284 9.2.1 Names matter 284 9.2.2 Standards for names 285 9.2.3 Braces and indentation 289 9.2.4 Comments 290 9.2.5 Tabs vs. spaces 293 9.2.6 Editors 294 9.3 Mini-antipatterns: Organization and visibility 295 9.4 Mini-antipatterns: Structure 298 9.4.1 Basic object-oriented philosophy 299 9.4.2 Low-level design considerations 299 9.4.3 Exceptions 301 9.5 Mini-antipatterns: Leaks and performance 302 9.6 Conventions for testing 303 9.7 Building a good style guide 305 9.7.1 Buy, borrow, or steal? 305 9.7.2 A sample style guide from Contextual, Inc. 306 9.8 Summary of coding standards 309 Bitter scalability 312 10.1 Good topologies for performance 313 10.1.1 Layering hardware in homogeneous groups 315 10.1.2 Other topology variations 318 10.2 Antipattern: Performance Afterthoughts 318 10.2.1 Developing without performance planning 319 10.2.2 Some real-world examples 320 10.2.3 Solution: Plan for performance! 321 10.3 Antipattern: Round-tripping 324 10.3.1 Solution: Cache and Facade 324 10.4 Antipattern: Bad Workload Management 327 10.4.1 Solution: Workload Management 328 10.4.2 True load balancing 330 10.5 Antipattern: Chaotic Session Management 331 10.5.1 Solution 1: Dispatching with session affinity 331 10.5.2 Solution 2: Using a distributed state management service 332 10.5.3 Using custom session bean solutions 332 10.5.4 Using custom entity bean solutions 333 10.6 Antipattern: Thrash-tuning 333 10.6.1 Solution: Use sound performance methodologies 334 10.7 Taming the performance beast 336 10.8 Antipatterns in this chapter 336 Sweet parting thoughts 340 11.1 Antipatterns help us on many levels 341 11.1.1 Antipatterns ignite careers 342 11.1.2 Understanding antipatterns improves programs 342 11.1.3 Understanding antipatterns makes you a better programmer 343 11.2 Integrating antipatterns with process 344 11.3 Next steps, last steps 346 Cross-references of antipatterns 348 bibliography 358 index 362 contents......Page 6 foreword......Page 16 preface......Page 18 acknowledgments......Page 22 about this book......Page 24 about the cover illustration......Page 28 The basics......Page 30 Bitter tales......Page 32 1.1 A Java development free fall......Page 33 1.1.1 Antipatterns in life......Page 35 1.2 Using design patterns accentuates the positive......Page 36 1.2.1 Design patterns online......Page 37 1.3 Antipatterns teach from the negative......Page 38 1.3.1 Some well-known antipatterns......Page 39 1.3.2 Antipatterns in practice......Page 40 1.3.3 Antipattern resources......Page 41 1.4 Antipattern ideas are not new......Page 42 1.4.1 Learning from the industry......Page 43 1.4.2 Detective work......Page 44 1.5 Why Bitter Java?......Page 46 1.5.2 Bitter Java tools......Page 47 1.5.3 The Bitter Java organization......Page 48 1.6 Looking ahead......Page 50 The bitter landscape......Page 52 2.1.1 The benefits of layering......Page 53 2.1.2 Layering can work against us......Page 55 2.2.1 Internet topologies affect our applications......Page 57 2.2.2 Enterprise layers add security and overhead......Page 58 2.2.3 Standards enable the Internet and add layers......Page 60 2.2.4 TCP and IP provide low-level communications......Page 61 2.2.5 HTTP provides application-level transport......Page 62 2.2.6 HTML and XML......Page 63 2.2.7 Mini-antipattern: Too Many Web Page Items......Page 64 2.3 Object technologies and antipatterns......Page 66 2.3.2 Inheritance enables packaging of common behavior......Page 67 2.3.4 Mini-antipatterns: Excessive Layering......Page 68 2.4 Java technologies solve antipatterns......Page 71 2.5 Major problems with the waterfall......Page 73 2.5.2 Mini-antipatterns: Incomplete Process Transitions......Page 74 2.5.3 Programming horizons: Extreme programming......Page 75 2.7 Antipatterns in this chapter......Page 77 Server-side Java antipatterns......Page 80 Bitter servlets......Page 82 3.1.1 An early antipattern: The Magic Pushbutton......Page 83 3.1.3 Failing to separate model and view......Page 85 3.1.4 Breaking out the model......Page 87 3.2 Antipattern: The Magic Servlet......Page 88 3.2.1 Can we use servlets as the model?......Page 89 3.2.2 Stumbling into the Magic Servlet trap......Page 91 3.2.3 Causes of the Magic Servlet......Page 95 3.3.1 Break out the model......Page 96 3.3.2 Wrapping the model with command objects......Page 97 3.3.3 Separating the model logic......Page 98 3.3.4 Separating the return trip......Page 103 3.3.5 Using a JSP for the return trip......Page 106 3.5 Antipattern in this chapter......Page 108 Bitter JSPs......Page 110 4.1.1 Recognizing the danger signs......Page 111 4.2.1 This program lacks model-view separation......Page 113 4.2.2 Solution: Refactor to Model-View-Controller......Page 115 4.3.1 Should we combine multiple JSPs?......Page 117 4.3.2 An example combining two interfaces......Page 118 4.3.4 Making decisions in the controller servlet......Page 123 4.4 Mini-antipatterns: Coarse and Fine Commands......Page 127 4.4.2 Solution: Refactor to appropriate granularity......Page 128 4.4.3 Tips for granularity......Page 130 4.6 Reviewing the JSP antipatterns......Page 131 4.7 Antipatterns in this chapter......Page 132 Bitter cache management......Page 136 5.1 We need caches!......Page 137 5.2 Antipattern: The Cacheless Cow......Page 138 5.2.1 Bitter BBS with no cache......Page 139 5.2.2 Building the model, view, and controller for ShowBoard......Page 141 5.2.3 Building the model, view, and controller for ShowThread......Page 144 5.2.4 Building the model, view and controller for AddPost......Page 148 5.3 Solution: Cache......Page 154 5.3.2 Solution 2: Cache commands......Page 155 5.3.3 Adding a cache to our BBS......Page 157 5.3.4 Possible enhancements to cached commands......Page 162 5.4.1 Concurrent access to static cache......Page 164 5.5 Antipattern: Synchronized Read/Write Bottlenecks......Page 165 5.5.1 Collisions between readers can hurt performance......Page 166 5.5.2 Read/write locks allow correct shared access......Page 167 5.7 Antipatterns in this chapter......Page 169 Bitter memories......Page 172 6.1 Understanding memory leaks and antipatterns......Page 173 6.1.1 Managing memory......Page 174 6.1.3 Reference counting......Page 175 6.1.4 Reachable objects......Page 177 6.2.1 Circumstances that cause Java memory leaks......Page 178 6.2.2 Finding Java leaks......Page 179 6.3 Antipattern: Lapsed Listeners Leak......Page 180 6.3.1 Examining some dangerous practices......Page 181 6.3.3 Solution 2: Shorten the life cycle of the anchor......Page 184 6.3.5 Reference objects simplify memory management......Page 185 6.4 Antipattern: The Leak Collection......Page 186 6.4.1 Causing trouble with caches and session state......Page 187 6.4.2 Solution 1: Search for common warning signs......Page 188 6.4.4 Solution 3: Use soft references for caches......Page 189 6.5.1 Make sure there is a leak......Page 190 6.5.2 Determine that the leak should be fixed......Page 191 6.5.3 Isolate the problem......Page 192 6.5.4 Determine the source and fix the problem......Page 193 6.5.5 Protect against the problem for the future......Page 194 6.6.1 String manipulation......Page 195 6.6.2 Collections......Page 196 6.7 Summary......Page 197 6.8 Antipatterns in this chapter......Page 198 Bitter connections and coupling......Page 200 7.2 Antipattern: Connection Thrashing......Page 201 7.2.2 Solution: Reuse connections with a pool......Page 203 7.2.3 Refactoring our BBS to add pooled connections......Page 206 7.2.4 Using getPooledConnection......Page 208 7.2.5 Using the J2EE connector architecture......Page 209 7.3 Antipattern: Split Cleaners......Page 210 7.3.1 Exceptions can lead to Split Cleaners......Page 212 7.3.2 Solution: Pair connection with cleanup, in finally......Page 213 7.4 Antipattern: Hardwired Connections......Page 214 7.4.1 The communications buffer......Page 215 7.4.3 Solution 1: Decouple with XML messages......Page 218 7.4.4 Solution 2: Delay binding with web services......Page 220 7.5 Mini-antipatterns for XML misuse......Page 221 7.5.2 XML’s bitter transitions......Page 222 7.6 Mini-antipatterns: Rigid XML......Page 223 7.6.1 Name collisions......Page 224 7.6.2 Rigid constructs......Page 226 7.6.3 Restrictive variable-content containers......Page 228 7.6.4 XML versioning......Page 230 7.7 Summary: Sweetening bitter connections......Page 231 7.8 Antipatterns in this chapter......Page 232 Bitter beans......Page 236 8.1.1 The component-based distributed architecture......Page 237 8.1.2 Types of EJBs......Page 238 8.2 Bitter BBS with EJBs......Page 239 8.2.1 Elements of an EJB application......Page 240 8.2.2 Building the remote interface......Page 242 8.2.3 Creating the home interface......Page 244 8.2.4 Implementing the bean class......Page 245 8.2.5 Defining the primary key......Page 250 8.2.6 Creating a deployment descriptor......Page 251 8.2.7 Using the model......Page 253 8.3 Antipattern: Round-tripping......Page 254 8.3.1 Computing the cost of a distributed deployment......Page 255 8.3.2 Chatty interfaces......Page 256 8.3.3 Solution: Group together round-trips with a facade......Page 257 8.3.4 Roots of round-tripping......Page 258 8.3.5 Refactoring the BBS with a facade......Page 259 8.4.1 Mini-antipattern: Bean-Managed Joins......Page 266 8.4.3 Mini-antipattern: Entity Beans for Lightweight Functions......Page 267 8.4.6 Troublesome scrollable lists......Page 269 8.4.7 Overall solution: Pick the right bean for the job......Page 270 8.5 Mini-antipattern: Everything Is an EJB......Page 271 8.6.1 Implementing a cache with a facade......Page 272 8.7 Smoothing out the bitter beans......Page 273 8.8 Antipatterns in this chapter......Page 274 The big picture......Page 278 Bitter hygiene......Page 280 9.1.1 Extreme programming requires good hygiene......Page 281 9.1.2 Coding standards protect against antipatterns......Page 282 9.2.1 Names matter......Page 284 9.2.2 Standards for names......Page 285 9.2.3 Braces and indentation......Page 289 9.2.4 Comments......Page 290 9.2.5 Tabs vs. spaces......Page 293 9.2.6 Editors......Page 294 9.3 Mini-antipatterns: Organization and visibility......Page 295 9.4 Mini-antipatterns: Structure......Page 298 9.4.2 Low-level design considerations......Page 299 9.4.3 Exceptions......Page 301 9.5 Mini-antipatterns: Leaks and performance......Page 302 9.6 Conventions for testing......Page 303 9.7.1 Buy, borrow, or steal?......Page 305 9.7.2 A sample style guide from Contextual, Inc.......Page 306 9.8 Summary of coding standards......Page 309 Bitter scalability......Page 312 10.1 Good topologies for performance......Page 313 10.1.1 Layering hardware in homogeneous groups......Page 315 10.2 Antipattern: Performance Afterthoughts......Page 318 10.2.1 Developing without performance planning......Page 319 10.2.2 Some real-world examples......Page 320 10.2.3 Solution: Plan for performance!......Page 321 10.3.1 Solution: Cache and Facade......Page 324 10.4 Antipattern: Bad Workload Management......Page 327 10.4.1 Solution: Workload Management......Page 328 10.4.2 True load balancing......Page 330 10.5.1 Solution 1: Dispatching with session affinity......Page 331 10.5.3 Using custom session bean solutions......Page 332 10.6 Antipattern: Thrash-tuning......Page 333 10.6.1 Solution: Use sound performance methodologies......Page 334 10.8 Antipatterns in this chapter......Page 336 Sweet parting thoughts......Page 340 11.1 Antipatterns help us on many levels......Page 341 11.1.2 Understanding antipatterns improves programs......Page 342 11.1.3 Understanding antipatterns makes you a better programmer......Page 343 11.2 Integrating antipatterns with process......Page 344 11.3 Next steps, last steps......Page 346 Cross-references of antipatterns......Page 348 bibliography......Page 358 index......Page 362
Intended for intermediate Java programmers, analysts, and architects, this guide is a comprehensive analysis of common server-side Java programming traps (called anti-patterns) and their causes and resolutions. Based on a highly successful software conference presentation, this book is grounded on the premise that software programmers enjoy learning not from successful techniques and design patterns, but from bad programs, designs, and war stories - bitter examples. These educational techniques of graphically illustrating good programming practices through negative designs and anti-patterns also have one added benefit: they are fun.
A comprehensive analysis of common server-side Java programming traps, called anti-patterns, and their causes and resolutions, this book is based on the premise that software programmers enjoy learning not from successful techniques and design patterns, but from bad programs, design problems, and war stories: bitter examples