Thinking in Patterns with Java.pdf

(382 KB) Pobierz
1 z 157
Thinking in Patterns by Bruce Eckel
Revision 0.9, 5-20-2003 (This version contains the material that will
be used in the Crested Butte seminar; see
http://www.mindview.net/Seminars/ThinkingInPatterns/ )
Please note this document is under development and incomplete.
Updates to this document can be found at http://www.Mindview.net
Best viewed with Mozilla! (free at www.Mozilla.org ) (Even though this
document was created with MS Word, IE6 garbles lines with
superscripts on them. Mozilla seems to do a much better job).
___________________________________________
Note : This document requires the installation of the fonts Georgia, Verdana and Andale
Mono (code font) for proper viewing. These can be found at:
http://sourceforge.net/project/showfiles.php?group_id=34153&release_id=105355
Modifications in Revision 0.9:
Prose has still had little/no work. My current goal is to get the structure and examples
worked out so that the seminar works well. Once it has been proven in the seminars,
then I will spend time on the prose.
Added proxy:PoolManager.java to make a more generic/customizeable Pool Manager,
and modified proxy:ConnectionPoolProxyDemo.java accordingly [[ Still need to
decide what to return when you run out of objects in the pool ]]
Changed PoolManager.java to use an ArrayList (and thus does not require a fixed size
at initialization)
Added KissingPrincess.java to State description, as a motivational example for the
pattern
Added a simple Flyweight example
Simplified the enumeration in PaperScissorsRock.java
Modifications in Revision 0.8:
Changed Bridge example to improve clarity.
Removed superscripts for better viewing with IE (see note above)
Modifications in Revision 0.7:
NOTE primary changes have been made to structure of book and code
examples, but not to prose. Prose can be considered to be mostly a mess in
this revision.
Complete reorganization under headings that attempt to describe the problems you
are trying to solve with a pattern.
Addition of placeholders for the remainder of the GoF patterns
Addition of “Simplifying Idioms” section and examples
Addition of Builder section and examples
Removed unit-testing chapter; replaced with reference to “new” JUnit (which uses
reflection)
(4-30-2003) Added Ant build.xml files, and support files from TIJ necessary to do a
full standalone build. You should be able to type “ant” from the code root directory
889458089.001.png
2 z 157
and get a successful build.
Dramatically simplified chainofresponsibility:FindMinima.java
Added object pool/connection pool examples
Refactored small things in many examples
Some exercises may have been left behind when patterns were moved.
For simplicity, saved from Word into a single HTML document, using “filtered”
version to remove Office stuff. Seems to work pretty well; checked it with both IE and
Mozilla (actually seems to work better on Mozilla than on IE!).
TODO:
Reconfigure for new backtalk system
Replace references to TIJ2 with TIJ3
Thinking
in
Patterns
Problem-Solving Techniques
using Java
Bruce Eckel
President, MindView, Inc.
Contents
Preface
3 z 157
Introduction
The Y2K syndrome
Context and composition
A word about checked exceptions
The pattern concept
What is a pattern?
Pattern taxonomy
Design principles
Classifying patterns
The development challenge
Unit testing
Location of test code
Simplifying Idioms
Messenger
Collecting Parameter
Object quantity
Singleton
Exercises
Object pool
Exercises
Object decoupling
Proxy: fronting for another object
The PoolManager using Proxy
Dynamic Proxies
State: changing object behavior
Iterators: decoupling algorithms from containers
Type-safe iterators
Exercises
Factoring commonality
Strategy: choosing the algorithm at run-time
Policy: generalized strategy
Template method
Exercises
Encapsulating creation
Simple Factory method
Polymorphic factories
Abstract factories
Exercises
Specialized creation
Prototype
Builder
Exercises
Too many
Flyweight: too many objects
Decorator: too many classes
Basic decorator structure
A coffee example
Class for each combination
The decorator approach
Compromise
Other considerations
Exercises
4 z 157
Connecting different types
Adapter
Bridge
Exercises
Flexible structure
Composite
System decoupling
Observer
Observing flowers
A visual example of observers
Mediator
Exercises
Reducing interface complexity
Façade
Package as a variation of Façade
Algorithmic partitioning
Command: choosing the operation at run-time
Exercises
Chain of responsibility
Exercises
Externalizing object state
Memento
Complex interactions
Multiple dispatching
Visitor, a type of multiple dispatching
Exercises
Multiple languages
Interpreter motivation
Python overview
Built-in containers
Functions
Strings
Classes
Creating a language
Controlling the interpreter
Putting data in
Getting data out
Multiple interpreters
Controlling Java from Jython
Inner Classes
Using Java libraries
Inheriting from Java library classes
Creating Java classes with Jython
Building the Java classes from the Python code
The Java-Python Extension (JPE)
Summary
Exercises
Complex system states
StateMachine
Exercises
Table-Driven State Machine
The State class
Conditions for transition
5 z 157
Transition actions
The table
The basic machine
Simple vending machine
Testing the machine
Tools
Table-driven code: configuration flexibility
Table-driven code using anonymous inner classes
Exercises
Pattern refactoring
Simulating the trash recycler
Improving the design
“Make more objects”
A pattern for prototyping creation
Trash subclasses
Parsing Trash from an external file
Recycling with prototyping
Abstracting usage
Multiple dispatching
Implementing the double dispatch
The Visitor pattern
A Reflective Decorator
More coupling?
RTTI considered harmful?
Summary
Exercises
Projects
Rats & Mazes
Other maze resources
XML Decorator
A: Tools
Ant extensions
Array utilities
Preface
The material in this book has been developed in conjunction with a
seminar that I have given for several years, mostly with Bill Venners. Bill
and I have given many iterations of this seminar and we’ve changed it
many times over the years as we both have learned more about patterns
and about giving the seminar.
In the process we’ve both produced more than enough information for us each to have our
own seminars, an urge that we’ve both strongly resisted because we have so much fun giving
the seminar together. We’ve given the seminar in numerous places in the US, as well as in
Prague (where we try to have a mini-conference every Spring together with a number of other
seminars). We’ve also given it as an on-site seminar.
A great deal of appreciation goes to the people who have participated in these seminars over
the years, as they have helped me work through these ideas and to refine them. I hope to be
able to continue to form and develop these kinds of ideas through this book and seminar for
many years to come.
This book will not stop here, either. After much pondering, I’ve realized that I want Thinking
Zgłoś jeśli naruszono regulamin