没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
1
Front Matter
Table of Contents
About the Author
Applied Java™ Patterns
Stephen Stelting
Olav Maassen
Publisher: Prentice Hall PTR
First Edition December 01, 2001
ISBN: 0-13-093538-7, 598 pages
Pattern-based solutions for every stage of the development lifecycle Documents 30 patterns,
including the 23 core patterns for the Sun Certified Enterprise Architect exam.
Increasingly, developers are recognizing the value of design patterns in helping to
create more robust, scalable, reliable, and maintainable applications. Now there’s a
book that can help you bring the power of patterns to your Java-based projects.
Sun Microsystems experts Steve Stelting and Olav Maassen bring together today’s
best pattern-based techniques and demonstrate pattern use for a variety of business
systems. This practical guide features proven techniques for all types of patterns,
from system architecture to single classes.
After briefly reviewing the fundamentals of design patterns, the authors describe
how these patterns can be applied effectively to the Java platform. Next, they
provide a pattern catalog, organized into four major categories—the creational,
behavioral, structural, and system patterns. In addition, they identify patterns and
present techniques for pattern use in the core Java APIs as well as the APIs for
distributed development.
Applied Java Patterns also features a section on pattern use in systems built with
J2EE and JINI technologies, and coverage of the servlet, JSP, EJB, and JavaSpaces
APIs.
Without a doubt, this accessible and up-to-date guide can help you enhance your
Java platform programming skills.
TEAMFLY
TEAM FLY PRESENTS
2
Table of Content
Table of Content...................................................................................................................................................................................... 2
Preface...................................................................................................................................................................................................... 4
Why We Wrote This Book ................................................................................................................................................................... 4
What This Book Is About..................................................................................................................................................................... 4
Who Should Read This Book ............................................................................................................................................................... 4
Conventions Used................................................................................................................................................................................. 4
How This Book Is Organized ............................................................................................................................................................... 5
How to Use This Book ......................................................................................................................................................................... 5
Companion Web Site............................................................................................................................................................................ 5
Acknowledgments ................................................................................................................................................................................ 5
Why Patterns?....................................................................................................................................................................................... 6
History of the Patterns Movement........................................................................................................................................................ 7
Basic Concepts in Patterns.................................................................................................................................................................... 8
Software Abstraction and Reuse........................................................................................................................................................... 9
Summary............................................................................................................................................................................................. 10
Part I: Commonly Used Patterns......................................................................................................................................................... 11
Chapter 1. Creational Patterns ............................................................................................................................................................ 11
Introduction to Creational Patterns..................................................................................................................................................... 11
Abstract Factory ................................................................................................................................................................................. 12
Builder................................................................................................................................................................................................ 17
Factory Method................................................................................................................................................................................... 23
Prototype............................................................................................................................................................................................. 27
Singleton............................................................................................................................................................................................. 31
Chapter 2. Behavioral Patterns............................................................................................................................................................ 34
Introduction to Behavioral Patterns .................................................................................................................................................... 34
Chain of Responsibility ...................................................................................................................................................................... 35
Command............................................................................................................................................................................................ 41
Interpreter ........................................................................................................................................................................................... 46
Iterator ................................................................................................................................................................................................ 52
Mediator.............................................................................................................................................................................................. 57
Memento............................................................................................................................................................................................. 63
State.................................................................................................................................................................................................... 74
Strategy............................................................................................................................................................................................... 81
Visitor................................................................................................................................................................................................. 86
Template Method................................................................................................................................................................................ 93
Chapter 3. Structural Patterns............................................................................................................................................................. 97
Introduction to Structural Patterns...................................................................................................................................................... 97
Adapter ............................................................................................................................................................................................... 98
Bridge ............................................................................................................................................................................................... 103
Composite......................................................................................................................................................................................... 108
Decorator.......................................................................................................................................................................................... 114
Facade............................................................................................................................................................................................... 120
Flyweight.......................................................................................................................................................................................... 125
Half-Object Plus Protocol (HOPP)................................................................................................................................................... 129
Proxy................................................................................................................................................................................................. 134
Chapter 4. System Patterns ................................................................................................................................................................ 139
Introduction to System Patterns........................................................................................................................................................ 139
Model-View-Controller (MVC) ....................................................................................................................................................... 140
Session.............................................................................................................................................................................................. 148
Worker Thread.................................................................................................................................................................................. 155
Callback............................................................................................................................................................................................ 160
Successive Update ............................................................................................................................................................................ 167
Transaction ....................................................................................................................................................................................... 178
Part II: Patterns in the Java Programming Language..................................................................................................................... 183
Chapter 5. Introduction to Java Programming Language Patterns............................................................................................... 183
Chapter 6. Java Core APIs................................................................................................................................................................. 184
Event Handling................................................................................................................................................................................. 184
JavaBeans ......................................................................................................................................................................................... 186
AWT and Swing – The Graphical User Interface APIs.................................................................................................................... 188
Collections Framework..................................................................................................................................................................... 192
Input-Output (I/O) ............................................................................................................................................................................ 195
Reflection.......................................................................................................................................................................................... 197
Chapter 7. Distributed Technologies ................................................................................................................................................. 199
3
Java Naming and Directory Interface (JNDI)................................................................................................................................... 199
JDBC ................................................................................................................................................................................................ 201
RMI................................................................................................................................................................................................... 203
CORBA ............................................................................................................................................................................................ 205
Chapter 8. Jini and J2EE Architectures............................................................................................................................................ 207
Jini .................................................................................................................................................................................................... 207
Java 2, Enterprise Edition (J2EE)..................................................................................................................................................... 210
Servlets and JSPs.............................................................................................................................................................................. 213
Enterprise JavaBeans........................................................................................................................................................................ 215
Appendix A. Full Code Examples ...................................................................................................................................................... 218
System Requirements ....................................................................................................................................................................... 218
Creational Pattern Code Examples ................................................................................................................................................... 219
Abstract Factory........................................................................................................................................................................... 219
Builder ......................................................................................................................................................................................... 222
Factory Method............................................................................................................................................................................ 228
Prototype...................................................................................................................................................................................... 232
Singleton...................................................................................................................................................................................... 234
Behavioral Pattern Code Examples .................................................................................................................................................. 238
Chain of Responsibility................................................................................................................................................................ 238
Command..................................................................................................................................................................................... 243
Interpreter..................................................................................................................................................................................... 248
Iterator.......................................................................................................................................................................................... 253
Mediator....................................................................................................................................................................................... 257
Memento...................................................................................................................................................................................... 262
Observer....................................................................................................................................................................................... 266
State ............................................................................................................................................................................................. 270
Strategy........................................................................................................................................................................................ 277
Visitor .......................................................................................................................................................................................... 282
Template Method......................................................................................................................................................................... 288
Structural Pattern Code Examples .................................................................................................................................................... 291
Adapter......................................................................................................................................................................................... 291
Bridge .......................................................................................................................................................................................... 293
Composite.................................................................................................................................................................................... 296
Decorator ..................................................................................................................................................................................... 302
Facade.......................................................................................................................................................................................... 306
Flyweight..................................................................................................................................................................................... 312
Half-Object Plus Protocol (HOPP) .............................................................................................................................................. 316
Proxy............................................................................................................................................................................................ 322
System Pattern Code Examples........................................................................................................................................................ 327
Model-View-Controller (MVC)................................................................................................................................................... 327
Session......................................................................................................................................................................................... 331
Worker Thread............................................................................................................................................................................. 338
Callback ....................................................................................................................................................................................... 344
Successive Update ....................................................................................................................................................................... 349
Router .......................................................................................................................................................................................... 354
Transaction................................................................................................................................................................................... 360
Appendix B........................................................................................................................................................................................... 367
Pattern Origins.................................................................................................................................................................................. 367
Creational Patterns............................................................................................................................................................................ 367
Behavioral Patterns........................................................................................................................................................................... 367
Structural Patterns............................................................................................................................................................................. 367
System Patterns................................................................................................................................................................................. 367
4
Preface
Why We Wrote This Book
During the many Java™ programming language courses we teach, we have found that only a few programmers
know what design patterns are when asked. About one in ten is able to name a few patterns off the top of his or
her head. Of course, the concepts behind the patterns are familiar to many programmers. When we demonstrate
patterns in the classroom, developers know and recognize them.
We decided to create a pattern catalog for the Java programming language developers who understand at a basic
level why patterns are a good idea, and are interested in applying them, but want a practical, hands-on guide to
just how and why to use each individual pattern. We’ve kept the book casual and frank in tone, and included full
working Java code examples for each.
We will have succeeded when you complete this book having not only learned about design patterns and the Java
programming language, but having had fun reading it, as well.
What This Book Is About
This book will teach you the essential design patterns and how you can use them in your Java application.
Furthermore, this book will show you where patterns are used in Java technology APIs and why they were used.
Who Should Read This Book
This book is intended for experienced Java programmers who want to build better applications. You should be
comfortable with the Java programming language and be familiar with most of the basic Java APIs. Some
knowledge of UML is useful, but not required. We recommend UML Distilled by Martin Fowler as a UML
reference.
Conventions Used
Within this book, code examples are presented in monospaced font. The same font is used in the text when
talking about specific classes, interfaces, methods or variables. methodName is just to indicate all methods that
have that name, where methodName () refers to a method with that name that takes no parameters.
Abstract classes have a name that starts with Abstract, whereas classes that either implement an interface or
subclass another class have a name that starts with Concrete (unless they are abstract). This naming convention
is shown in Figure 1.
Figure 1. Example class diagram
A client is the general term used for a class that uses the classes of the design pattern, which is different from a
user. A user is a human being interacting with the application.
The notation “ [CJ2EEP] ” in the Related Patterns section for a pattern refers to J2EE patterns, listed in the
bibliography.
5
How This Book Is Organized
This book is divided into two parts. Part I, “ Commonly Used Patterns,” is organized like a pattern catalogue,
reference-style.
Chapter 1: “ Creational Patterns ” on page 3 discusses patterns that create objects: Abstract Factory, Builder,
Factory Method, Prototype, and Singleton.
Chapter 2: “ Behavioral Patterns ” on page 39 is focused on the patterns that can determine the behavior of your
object model: Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State,
Strategy, Template Method, and Visitor.
Chapter 3: “ Structural Patterns ” on page 139 describes patterns that can bring structure to your application and
has the following patterns: Adapter, Bridge, Composite, Decorator, Facade, Flyweight, HOPP, and Proxy.
Chapter 4: “ System Patterns ” on page 205 describes the patterns that help you build better architectures: Callback,
Router, MVC, Session, Successive Update, Transaction, and Worker Thread.
Part II, “ Patterns in the Java Programming Language,” presents many of the Java APIs and shows the use of
patterns in those API and their benefit.
Chapter 6: “ Java Core APIs ” on page 279 provides an overview in the familiar core APIs like Event Handling,
JavaBeans ™, AWT and Swing, Collections, Input/Output, and Reflection.
Chapter 7: “ Distributed Technologies ” on page 303 describes selected distributed APIs and how patterns are used:
JNDI, JDBC, RMI, and CORBA.
Chapter 8: “ Jini and J2EE Architectures ” on page 317 focuses on the two complementary frameworks Jini and
J2EE. J2EE is further divided into Servlets, JSP and EJB technologies.
How to Use This Book
There are several ways to read this book. You could start at page one and read from cover to cover. However, we
recommend you start with some of the easier patterns: Factory Method, Singleton, Observer, and Composite.
Work your way through the book using those as starting points for your exploration. Alternatively, you might
want to turn to sections in Part II first. Find an API you are familiar with and start looking for patterns there.
You can read the patterns in any order you feel most comfortable with. Later, you can use this book as a reference
to refresh your memory when you want to put your knowledge of patterns into practice
Companion Web Site
This book has a companion Web site to provide you with updates and other material: it is located at
http://www.phptr.com/appliedjavapatterns.
Acknowledgments
A book is, above all else, a team effort. We’d like to thank all the people who made this a reality. We’ve worked
with an exceptionally fine group. This page is dedicated to them, to let them know that their efforts are
appreciated.
For Greg Doench, Prentice Hall visionary: Thank you for being the Great Unifier for this project. When we
started this work, we discovered Greg was a marathon runner. When Steve mentioned that he would like to try his
hand (feet?) at the sport, Greg said, “After the book is done.” Now we understand why: writing a book is itself a
marathon. For your ongoing help and support, and for your belief in this book, our most sincere thanks.
For Rachel Borden, Sun Press luminary: Thank you for your guidance along the path to publication. If not for
your help, we’d still be scrawling ideas across massive expanses of sticky notes. Thank you for your ongoing
support and dedication, and for having patience when explaining to techies how publishing works. Our thanks for
getting up far too early on far too many mornings for conference calls with people on the other side of the world.
Most of all, thanks for being a continuing champion of our work.
剩余368页未读,继续阅读
资源评论
I010I
- 粉丝: 0
- 资源: 32
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功