Sciweavers


Book

The Design Patterns Java Companion

15 years 7 months ago
The Design Patterns Java Companion
"The term “design patterns” sounds a bit formal to the uninitiated and can be somewhat off-putting when you first encounter it. But, in fact, design patterns are just convenient ways of reusing object-oriented code between projects and between programmers. The idea behind design patterns is simple-- write down and catalog common interactions between objects that programmers have frequently found useful. The field of design patterns goes back at least to the early 1980s. At that time, Smalltalk was the most common OO language and C++ was still in its infancy. At that time, structured programming was a commonly-used phrased and OO programming was not yet as widely supported. The idea of programming frameworks was popular however, and as frameworks developed, some of what we now called design patterns began to emerge."
James W. Cooper
Added 17 Apr 2009
Updated 17 Apr 2009
Authors James W. Cooper
1. Creational Patterns 17
The Factory Pattern 18
How a Factory Works 18
Sample Code 18
The Two Derived Classes 19
Building the Factory 20
Factory Patterns in Math Computation 22
When to Use a Factory Pattern 24
Thought Questions 25
 
The Abstract Factory Pattern 26
A GardenMaker Factory 26
How the User Interface Works 28
Consequences of Abstract Factory 30
Thought Questions 30
 
The Singleton Pattern 31
Throwing the Exception 32
Creating an Instance of the Class 32
Static Classes as Singleton Patterns 33
Creating Singleton Using a Static Method 34
Finding the Singletons in a Large Program 35
Other Consequences of the Singleton Pattern 35
 
The Builder Pattern 37
An Investment Tracker 38
Calling the Builders 40
The List Box Builder 42
The Checkbox Builder 43
Consequences of the Builder Pattern 44
Thought Questions 44
 
The Prototype Pattern 45
Cloning in Java 45
Using the Prototype 47
Consequences of the Prototype Pattern 50
 
Summary of Creational Patterns 51
2. The Java Foundation Classes 52
Installing and Using the JFC 52
Ideas Behind Swing 53
The Swing Class Hierarchy 53
 
Writing a Simple JFC Program 54
Setting the Look and Feel 54
Setting the Window Close Box 55
Making a JxFrame Class 55
A Simple Two Button Program 56
More on JButtons 57
 
Buttons and Toolbars 59
Radio Buttons 59
The JToolBar 59
Toggle Buttons 60
Sample Code 61
 
Menus and Actions 62
Action Objects 62
Design Patterns in the Action Object 65
 
The JList Class 67
List Selections and Events 68
Changing a List Display Dynamically 69
 
The JTable Class 71
A Simple JTable Program 71
Cell Renderers 74
 
The JTree Class 77
The TreeModel Interface 78
Summary 79
 
3. Structural Patterns 80
The Adapter Pattern 81
Moving Data between Lists 81
Using the JFC JList Class 83
Two Way Adapters 87
Pluggable Adapters 87
Adapters in Java 88
 
The Bridge Pattern 90
Building a Bridge 91
Consequences of the Bridge Pattern 93
 
The Composite Pattern 95
An Implementation of a Composite 96
Building the Employee Tree 98
Restrictions on Employee Classes 100
Consequences of the Composite Pattern 100
Other Implementation Issues 101
 
The Decorator Pattern 103
Decorating a CoolButton 103
Using a Decorator 105
Inheritance Order 107
Decorating Borders in Java 107
Non-Visual Decorators 109
Decorators, Adapters and Composites 110
Consequences of the Decorator Pattern 110
 
The Façade Pattern 111
Building the Façade Classes 112
Consequences of the Façade 115
 
The Flyweight Pattern 117
Discussion 117
Example Code 118
Flyweight Uses in Java 122
Sharable Objects 122
 
The Proxy Pattern 124
Sample Code 124
Copy-on-Write 127
Comparison with Related Patterns 127
 
Summary of structural patterns 128
4. Behavioral Patterns 129
Chain of Responsibility 130
Applicability 130
Sample Code 131
The List Boxes 133
A Chain or a Tree? 135
Kinds of Requests 137
Examples in Java 137
Consequences of the Chain of Responsibility 138
 
The Command Pattern 139
Motivation 139
The Command Pattern 140
Building Command Objects 141
The Command Pattern in Java 142
Consequences of the Command Pattern 143
Providing Undo 144
 
The Interpreter Pattern 145
Motivation 145
Applicability 145
Sample Code 146
Interpreting the Language 147
Objects Used in Parsing 148
Reducing the Parsed Stack 150
Consequences of the Interpreter Pattern 153
 
The Iterator Pattern 155
Motivation 155
Enumerations in Java 156
Filtered Iterators 156
Sample Code 157
Consequence of the Iterator Pattern 159
Composites and Iterators 160
 
The Mediator Pattern 161
An Example System 161
Interactions between Controls 162
Sample Code 164
Mediators and Command Objects 167
Consequences of the Mediator Pattern 167
Implementation Issues 168
 
The Memento Pattern 169
Motivation 169
Implementation 169
Sample Code 170
Consequences of the Memento 175
Other Kinds of Mementos 176
 
The Observer Pattern 177
Watching Colors Change 178
The Message to the Media 181
The JList as an Observer 182
The MVC Architecture as an Observer 183
Consequences of the Observer Pattern 184
 
The State Pattern 185
Sample Code 185
Switching Between States 190
How the Mediator Interacts with the State Manager 191
Consequences of the State Pattern 192
State Transitions 192
Thought Questions 192
 
The Strategy Pattern 194
Motivation 194
Sample Code 195
The Context 196
The Program Commands 197
The Line and Bar Graph Strategies 198
Drawing Plots in Java 198
Consequences of the Strategy Pattern 201
 
The Template Pattern 202
Motivation 202
Kinds of Methods in a Template Class 203
Sample Code 204
The Triangle Drawing Program 207
Templates and Callbacks 208
Summary and Consequences 209
 
The Visitor Pattern 210
Motivation 210
When to Use the Visitor Pattern 211
Sample Code 212
Visiting Several Classes 214
Bosses are Employees, too 215
Double Dispatching 216
Traversing a Series of Classes 216
Consequence of the Visitor Pattern 216 
Comments (0)