John Hunt and Alex McManus Key Java Advanced Tips and Techniques
Preface xiii Part 1 Java Development 1 Introduction 3 1.1 Introduction 3 1.2 A Brief History of Java 3 1.3 The Three Stages of Java 4 1.4 The Java Application Platform 7 1.5 Case Studies 8 1.6 Issues for Java Developers 12 2 Inheritance and Reuse 13 2.1 Introduction 13 2.2 Inheritance 14 2.3 Drawbacks of Inheritance 16 2.4 Balancing Inheritance and Reuse 22 2.5 Compositional Reuse 23 2.6 Promoting Reuse in Object-Oriented Systems 24 2.7 Tool Support 25 2.8 Conclusions 26 2.9 Further Reading 26 3 Persistent Storage of Objects 27 3.1 Introduction 27 3.2 Streams 27 3.3 Serialization 28 3.4 The Object Input/Output Streams 30 3.5 Controlling What is and is not Saved 32 3.6 Version control 32 3.7 Problems with Serialization 33 3.8 Further Reading 34 3.9 Online References 34 4 Introductory JavaBeans 35 4.1 Introduction 35 4.2 Overview 35 vii
viii 4.3 Implementing JavaBeans 38 4.4 Defining JavaBeans 41 4.5 Using the BeanBox and Jar Files 46 4.6 Further Reading 47 4.7 Online References 47 5 JavaBeans in Depth 49 5.1 Introduction 49 5.2 Introspection 49 5.3 Property Data Types 50 5.4 Creating a Beanlnfo Object 54 5.5 Serializing a Bean 56 5.6 Property Editors 57 5.7 Customizers 60 5.8 A Critique of JavaBeans 60 5.9 Summary 62 6 Java DataBase Connectivity 63 6.1 Introduction 63 6.2 WhatisJDBC? 63 6.3 What the Driver Provides 65 6.4 Registering Drivers 65 6.5 Opening a Connection 66 6.6 Obtaining Data from a Database 67 6.7 Creating a Table 69 6.8 Limitations 70 6.9 Further Reading 71 6.10 Online References 71 7 The Web and JDBC 73 7.1 Introduction 73 7.2 MiniSQL 73 7.3 Sockets in Java 74 7.4 Web-Based Client-Server Architecture 75 7.5 Signed Applets 82 7.6 Further Reading 83 7.7 Online References 83 8 File-Based Storage Classes 85 8.1 Introduction 85 8.2 File-Based Storage Classes 85 8.3 Implementing a Fi lebag Class 86 8.4 The Collection Interface 86 8.5 The RangedRandom Class 87 8.6 The UniqueFilename Class 88 8.7 The Fi lebag Class 90 8.8 Online Resources 101
ix 9 Frameworks 103 9.1 Introduction 103 9.2 What is a Framework? 103 9.3 MVC: An Example Framework 104 9.4 The MVC Framework in Java 107 9.5 The Balance Application 110 9.6 The Application Code 112 9.7 Designing Object-Oriented Frameworks 116 9.8 General Guidance for Framework Development 118 9.9 Further Reading 118 10 Patterns for Documenting Frameworks 119 10.1 Introduction 119 10.2 Motivation Behind Patterns 119 10.3 Design Patterns 120 10.4 Documenting Patterns 121 10.5 When to Use Patterns 123 10.6 Strengths and Limitations of Design Patterns 123 10.7 An Example Pattern: Mediator 124 10.8 Summary 128 10.9 Further Reading 129 10.10 Online References 129 Part 2 Java Testing 11 Testing Object-Oriented Systems 133 11.1 Introduction 133 11.2 Why is Testing Object-Oriented Systems Hard? 133 11.3 Inheritance *. 136 11.4 Abstract Superclasses 140 11.5 Encapsulation 141 11.6 Polymorphism 143 11.7 Additional Java-Specific Features 145 11.8 Summary 145 11.9 Further Reading 146 12 Testing Java Code.. 147 12.1 Introduction 147 12.2 Class and Instance Sides 147 12.3 Sources of Test Information 150 12.4 Object Integration Testing 151 12.5 System Testing 151 12.6 Summary 152 12.7 Further Reading 152 13 Assertions in Java 153 13.1 Introduction 153
x Part 3 13.2 Assertions and Their Uses 153 13.3 Using Assertions in Java 154 13.4 Assertions in Java 156 13.5 CheckedAssertion and CheckedAssertionException 158 13.6 Analysis 159 13.7 Summary 160 13.8 Further Reading 160 Java Performance 14 Performance: Tooling Up 165 14.1 Introduction 165 14.2 The Java Language 165 14.3 Virtual Machines 166 14.4 Java Compilers 169 14.5 Targeting Optimization 170 14.6 Profiling 172 14.7 Summary 173 14.8 Online References 173 15 Optimizing for Speed 175 15.1 Introduction 175 15.2 Timing Harnesses 175 15.3 System Library Classes 179 15.4 Traditional Optimizations 184 15.5 The Cost of Java Operations 185 15.6 Rewriting Library Classes 191 15.7 Summary 192 15.8 Online References 192 15.9 References 193 16 Optimizing for Memory 195 16.1 Introduction 195 16.2 Reducing Class File Size 195 16.3 Memory Management 198 16.4 Strategies for Reducing Memory Overhead 201 16.5 Summary 204 16.6 Further Reading 205 16.7 Online References 205 17 Future Evaluators 207 17.1 Exploiting Idle Processor Time 207 17.2 Processes in Java 208 17.3 Inter-process Communication 209 17.4 Implementing Future Evaluators 210 17.5 Working with Future Evaluators 212
xi 17.6 Lazy Evaluators 214 17.7 Native Threads 214 17.8 Summary 214 17.9 Online References 215 18 Real-Time Java 217 18.1 Introduction 217 18.2 What are Real-Time Systems? 217 18.3 Language Requirements 218 18.4 Java for Real-Time Systems 218 18.5 Summary 221 18.6 Further Reading 221 Part 4 Graphic Java 19 Evolution of the Java User Interface 225 19.1 Introduction 225 19.2 The Abstract Window Toolkit 225 19.3 The Internet Foundation Classes 227 19.4 Other Contributions 228 19.5 The Java Foundation Classes 229 19.6 Summary 230 19.7 Online References 230 20 The Internet Foundation Classes 233 20.1 Introduction 233 20.2 Application Framework 233 20.3 View Classes 236 20.4 Layout Managers 242 20.5 Windows 243 20.6 Persistence 244 20.7 IFC Constructor 244 20.8 IFC and AWT 246 20.9 Migration to the Java Foundation Classes 246 20.10 Summary 247 20.11 Online References 247 21 Swinging with the Java Foundation Classes 249 21.1 Introduction 249 21.2 Model-View-Controller Architecture 250 21.3 Pluggable Look and Feel 251 21.4 Swing Components 254 21.5 Windows 260 21.6 Common Dialogs 261 21.7 Extending Swing Components 263 21.8 Creating New Swing Components 265 21.9 Actions 266
xii 21.10 Undo Framework 269 21.11 Java Accessibility 274 21.12 Summary 275 21.13 Online References 275 22 Graphical Java Media 277 22.1 Introduction 277 22.2 Java 2D 277 22.3 Java 3D 287 22.4 The Java Media Framework 294 22.5 Summary 299 22.6 Online References 299 23 User Interface Design Issues in Java 301 23.1 Introduction 301 23.2 Users and Tasks 301 23.3 General Guidelines for Good Design 306 23.4 Consistency 309 23.5 Menus 312 23.6 Dialogs 314 23.7 Error Handling 316 23.8 Internationalization 319 23.9 Response Time 322 23.10 Screen Design and Colour 323 23.11 Help! 323 23.12 Configuration 324 23.13 Platform Independence 325 23.14 Summary 325 23.15 Further Reading 326 23.16 Online References 326 Index 327