Design patterns have become a staple of object-oriented design and programming by providing elegant, easy-to-reuse, and maintainable solutions to commonly encountered programming challenges. However, many busy Java programmers have yet to learn about design patterns and incorporate this powerful technology into their work.
Java Design Patterns is exactly the tutorial resource you need. Accessible and clearly written, it helps you understand the nature and purpose of design patterns. It also serves as a practical guide to using design patterns to create sophisticated, robust Java programs.
This book presents the 23 patterns cataloged in the flagship book Design Patterns by Gamma, Helm, Johnson, and Vlissides. In Java Design Patterns, each of these patterns is illustrated by at least one complete visual Java program. This practical approach makes design pattern concepts more concrete and easier to grasp, brings Java programmers up to speed quickly, and enables you to take practical advantage of the power of design patterns.
Key features include:
After reading this tutorial, you will be comfortable with the basics of design patterns and will be able to start using them effectively in your day-to-day Java programming work.
Download the Tutorial Files related to this title:
I. WHAT ARE DESIGN PATTERNS?1. Introduction.
Defining Design Patterns.
The Learning Process.
Studying Design Patterns.
Notes on Object-Oriented Approaches.
The Java Foundation Classes.
Java Design Patterns.2. UML Diagrams.
JVISION UML Diagrams.
Visual SlickEdit Project Files.
II. CREATIONAL PATTERNS.3. The Factory Pattern.
How a Factory Works.
The Two Subclasses.
Building the SimpleFactory.
Factory Patterns in Math Computation.
4. The Factory Method.
The Swimmer Class.
The Event Classes.
Our Seeding Program.
When to Use a Factory Method.
5. The Abstract Factory Pattern.
A GardenMaker Factory.
How the User Interface Works.
Adding More Classes.
Consequences of the Abstract Factory Pattern.
6. The Singleton Pattern.
Creating a Singleton Using a Static Method.
Exceptions and Instances.
Throwing an Exception.
Creating an Instance of the Class.
Providing a Global Point of Access to a Singleton Pattern.
The javax.comm Package as a Singleton.
Other Consequences of the Singleton Pattern.
7. The Builder Pattern.
An Investment Tracker.
Calling the Builders.
The List Box Builder.
The Check Box Builder.
Consequences of the Builder Pattern.
8. The Prototype Pattern.
Cloning in Java.
Using the Prototype.
Using the Prototype Pattern.
Cloning Using Serialization.
Consequences of the Prototype Pattern.
Summary of Creational Patterns.
III. STRUCTURAL PATTERNS.9. The Adapter Pattern.
Moving Data between Lists.
Using the JFC JList Class.
Adapters in Java.
10. The Bridge Pattern.
The Class Diagram.
Extending the Bridge.
Java Beans as Bridges.
Consequences of the Bridge Pattern.
11. The Composite Pattern.
An Implementation of a Composite.
The Employee Classes.
The Boss Class.
Building the Employee Tree.
Doubly Linked List.
Consequences of the Composite Pattern.
A Simple Composite.
Composites in Java.
Other Implementation Issues.
12. The Decorator Pattern.
Decorating a CoolButton.
Using a Decorator.
The Class Diagram.
Decorating Borders in Java.
Decorators, Adapters, and Composites.
Consequences of the Decorator Pattern.
13. The Faade Pattern.
Building the Faade Classes.
Consequences of the Faade Pattern.
Notes on Installing and Running the dbFrame Program.
14. The Flyweight Pattern.
Flyweight Uses in Java.
15. The Proxy Pattern.
Enterprise Java Beans.
Comparison with Related Patterns.
Summary of Structural Patterns.
IV. BEHAVIORAL PATTERNS.16. Chain of Responsibility Pattern.
The List Boxes.
Programming a Help System.
A Chain or a Tree?
Kinds of Requests.
Examples in Java.
Consequences of the Chain of Responsibility.
17. The Command Pattern.
Building Command Objects.
The Command Pattern.
The Command Pattern in the Java Language.
Consequences of the Command Pattern.
18. The Interpreter Pattern.
Simple Report Example.
Interpreting the Language.
Objects Used in Parsing.
Reducing the Parsed Stack.
Implementing the Interpreter Pattern.
Consequences of the Interpreter Pattern.
19. The Iterator Pattern.
Enumerations in Java.
Consequence of the Iterator Pattern.
Composites and Iterators.
Iterators in Java 1.2.
20. The Mediator Pattern.
An Example System.
Interactions between Controls.
Mediators and Command Objects.
Consequences of the Mediator Pattern.
Single Interface Mediators.
21. The Memento Pattern.
Consequences of the Memento Pattern.
22. The Observer Pattern.
Watching Colors Change.
The Message to the Media.
The JList as an Observer.
The MVC Architecture as an Observer.
The Observer Interface and Observable Class.
Consequences of the Observer Pattern.
23. The State Pattern.
Switching between States.
How the Mediator Interacts with the StateManager.
Mediators and the God Class.
Consequences of the State Pattern.
24. The Strategy Pattern.
The Context Class.
The Program Commands.
The Line and Bar Graph Strategies.
Drawing Plots in Java.
Consequences of the Strategy Pattern.
25. The Template Pattern.
Kinds of Methods in a Template Class.
Template Method Patterns in Java.
Templates and Callbacks.
Consequences of the Template Pattern.
26. The Visitor Pattern.
When to Use the Visitor Pattern.
Visiting the Classes.
Visiting Several Classes.
Bosses are Employees, Too.
Catch-All Operations Using Visitors.
Traversing a Series of Classes.
Consequence of the Visitor Pattern.
V. DESIGN PATTERNS AND THE JAVA FOUNDATION CLASSES.27. The JFC, or Swing.
Installing and Using Swing.
Ideas behind Swing.
The Swing Class Hierarchy.28. Writing a Simple JFC Program.
Setting the Look and Feel.
Setting the Window Close Box.
Making a JxFrame Class.
A Simple Two-Button Program.
More on JButton.
29. Radio Buttons and Toolbars.
A Sample Button Program.
30. Menus and Actions.
Design Patterns in the Action Object.
31. The JList Class.
List Selections and Events.
Changing a List Display Dynamically.
A Sorted JList with a ListModel.
Sorting More-Complicated Objects.
Getting Database Keys.
Adding Pictures in List Boxes.
Programs on the CD-ROM.32. The JTable Class.
A Simple JTable Program.
Rendering Other Kinds of Classes.
Selecting Cells in a Table.
Patterns Used in This Image Table.
33. The JTree Class.
The TreeModel Interface.
IV. CASE STUDIES.34. Sandy and the Mediator.
This is a practical book that tells you how to write Java programs using some of the most common design patterns. It is structured as a series of short chapters, each describing a design pattern and giving one or more complete, working, visual example programs that use that pattern. Each chapter also includes Unified Modeling Language (UML) diagrams illustrating how the classes interact.
This book is not a "companion" book to the well-known Design Patterns text Gamma, 1995 by the "Gang of Four." Rather, it is a tutorial for people who want to learn what design patterns are about and how to use them in their work. You need not have read Design Patterns to gain from reading this book, but when you are done here you might want to read or reread that book to gain additional insights.
In this book, you will learn that design patterns are a common way to organize objects in your programs to make those programs easier to write and modify. You'll also see that by familiarizing yourself with these design patterns, you will gain a valuable vocabulary for discussing how your programs are constructed.
People come to appreciate design patterns in different ways--from the highly theoretical to the intensely practical--and when they finally see the great power of these patterns, they experience an "Aha!" moment. Usually this moment means that you suddenly had an internal picture of how that pattern can help you in your work.
In this book, we try to help you form that conceptual idea, or gestalt, by describing the pattern in as many ways as possible. The book is organized into six main sections:
For each pattern, we start with a brief verbal description and then build simple example programs. Each example is a visual program that you can run and examine so as to make the pattern as concrete as possible. All of the example programs and their variations are on the CD-ROM that accompanies this book. In that way, you can run them, change them, and see how the variations that you create work.
All of the programs are based on Java 1.2, and most use the JFC. If you haven't taken the time to learn how to use these classes, there is a tutorial covering the basics in Appendix A where we also discuss some of the patterns that they illustrate.
Since each of the examples consists of a number of Java files for each of the classes we use in that example, we also provide a Visual SlickEdit project file for each example and place each example in a separate subdirectory to prevent any confusion.
As you leaf through the book, you'll see screen shots of the programs we developed to illustrate the design patterns; these provide yet another way to reinforce your learning of these patterns. You'll also see UML diagrams of these programs that illustrate the interactions between classes in yet another way. UML diagrams are just simple box and arrow illustrations of classes and their inheritance structure, with the arrows pointing to parent classes and dotted arrows pointing to interfaces. If you are unfamiliar with UML, we provide a simple introduction in the first chapter.
Finally, since we used JVISION to create the UML diagrams in each chapter, we provide the original JVISION diagram files for each pattern as well, so you can use your own copy of JVISION to play with them.
When you finish this book, you'll be comfortable with the basics of design patterns and will be able to start using them in your day to day Java programming work.
James W. Cooper
Click below for Errata related to this title:
The following Errata details changes made to the text for the second printing of Java™ Design Patterns. Each pdf file contains corrected text from the second printing. The text had been highlighted in yellow to indicate where the changes have been made.