O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Design Patterns Java™ Workbook

Book Description

Praise for Design Patterns Java™ Workbook

“An excellent book... I’m incredibly impressed with how readable it is. I understood every single chapter, and I think any reader with any Java familiarity would. This book is going to be required reading in a lot of places, including my office.”

     —Joshua Engel

“Provides a new, more Java-literate way to understand the 23 GoF patterns.”

     —Bob Hanmer

“This book translates Design Patterns into what Java programmers need to know. It is full of short, engaging programming and design problems with solutions—making it easy for programmers to work through solutions and really make patterns ‘stick.’”

     —Rebecca Wirfs-Brock

“This is one exciting book. It’s approachable, readable, interesting, instructive, and just plain valuable. It’ll eclipse all other books purporting to teach people the GoF patterns in Java—and perhaps any other language.”

     —John Vlissides

Java programmers, you now have the resource you need to harness the considerable power of design patterns. This unique book presents examples, exercises, and challenges that will help you apply design pattern theory to real-world problems. Steve Metsker's learn-by-doing approach helps you enhance your practical skills and build the confidence you need to use design patterns effectively in mission-critical applications.

Design Patterns Java™ Workbook features the twenty-three foundational design patterns introduced in the classic book Design Patterns (Addison-Wesley, 1995). In this new, hands-on workbook, the patterns are organized into five major categories: interfaces, responsibility, construction, operations, and extensions. Each category begins with a chapter that reviews and challenges your ability to apply facilities built into Java. These introductory sections are followed by chapters that explain a particular pattern in detail, demonstrate the pattern in use with UML diagrams and Java code, and provide programming problems for you to solve.

With this book you will build expertise in important areas such as:

  • Adapting domain data to Swing components

  • Creating a FACADE for Swing

  • Handling recursion in composites

  • Understanding the role of BRIDGE in Java database connectivity

  • Making the connection between Model/View/Controller and OBSERVER

  • Maintaining relational integrity with a mediator

  • Using proxies to communicate between computers

  • Letting a service provider decide which class to instantiate

  • Supporting undo operations with MEMENTO

  • Prototyping with clones

  • Using COMMAND to supply a service

  • Developing thread-safe iterators

  • Extending classes with DECORATOR and VISITOR

Solutions to the design pattern challenges appear in the back of the book, so you can compare your own work to expert approaches. A brief guide to UML explains the modeling notation, and an accompanying Web site provides all the code examples from the book.

Through the instruction and exercises offered in Design Patterns Java™ Workbook, you can fully understand the role of design patterns in Java application development, and enhance your ability to put design patterns to work.

Table of Contents

  1. Copyright
  2. Praise for Design Patterns Java™ Workbook
  3. Foreword
  4. Preface
  5. Introduction To Patterns
    1. Why Patterns?
    2. Why Design Patterns?
    3. Why Java?
    4. Why UML?
    5. Why a Workbook?
    6. The Organization of This Book
    7. Welcome to Oozinoz!
    8. Source Code Disclaimer
    9. Summary
  6. Interface Patterns
    1. Introducing Interfaces
      1. Ordinary Interfaces
      2. Interfaces and Obligations
      3. Placing Constants in Interfaces
      4. Summary
      5. Beyond Ordinary Interfaces
    2. Adapter
      1. Adapting in the Presence of Foresight
      2. Class and Object Adapters
      3. Unforeseen Adaptation
      4. Recognizing Adapter
      5. Summary
    3. Facade
      1. Refactoring to Facade
      2. Facades, Utilities, and Demos
      3. Summary
    4. Composite
      1. An Ordinary Composite
      2. Recursive Behavior in Composites
      3. Trees in Graph Theory
      4. Composites with Cycles
      5. Consequences of Cycles
      6. Summary
    5. Bridge
      1. A Classic Example of Bridge: Drivers
      2. Refactoring to Bridge
      3. A Bridge Using the List Interface
      4. Summary
  7. Responsibility Patterns
    1. Introducing Responsibility
      1. Ordinary Responsibility
      2. Controlling Responsibility with Visibility
      3. Summary
      4. Beyond Ordinary Responsibility
    2. Singleton
      1. Singleton Mechanics
      2. Singletons and Threads
      3. Recognizing Singleton
      4. Summary
    3. Observer
      1. A Classic Example: Observer in Swing
      2. Model/View/Controller
      3. Maintaining an Observable Object
      4. Summary
    4. Mediator
      1. A Classic Example: GUI Mediators
      2. Relational Integrity Mediators
      3. Summary
    5. Proxy
      1. A Classic Example: Image Proxies
      2. Image Proxies Reconsidered
      3. Remote Proxies
      4. Summary
    6. Chain of Responsibility
      1. Varieties of Lookup
      2. Refactoring to Chain of Responsibility
      3. Anchoring a Chain
      4. Chain of Responsibility without Composite
      5. Summary
    7. Flyweight
      1. Recognizing Flyweight
      2. Immutability
      3. Extracting the Immutable Part of a Flyweight
      4. Sharing Flyweights
      5. Summary
  8. Construction Patterns
    1. Introducing Construction
      1. Ordinary Construction
      2. Superclass Collaboration
      3. Collaboration within a Class
      4. Summary
      5. Beyond Ordinary Construction
    2. Builder
      1. Building from a Parser
      2. Building under Constraints
      3. Building a Counteroffer
      4. Summary
    3. Factory Method
      1. Recognizing Factory Method
      2. A Classic Example of Factory Method: Iterators
      3. Taking Control of Which Class to Instantiate
      4. Factory Method in Parallel Hierarchies
      5. Summary
    4. Abstract Factory
      1. Abstract Factories for Families of Objects
      2. Packages and Abstract Factories
      3. Abstract Factories for Look-and-Feel
      4. Summary
    5. Prototype
      1. Prototypes as Factories
      2. Prototyping with Clones
      3. Using Object.clone()
      4. Summary
    6. Memento
      1. Memento Durability
      2. Applying Memento
      3. Persisting Mementos across Sessions
      4. Using Strings as Mementos
      5. Summary
  9. Operation Patterns
    1. Introducing Operations
      1. Operations, Methods, and Algorithms
      2. The Mechanics of Methods
      3. Exceptions in Methods
      4. Summary
      5. Beyond Ordinary Operations
    2. Template Method
      1. A Classic Example of Template Method: Sorting
      2. Completing an Algorithm
      3. Template Method Hooks
      4. Refactoring to Template Method
      5. Summary
    3. State
      1. Modeling States
      2. Refactoring to State
      3. Making States Constant
      4. Summary
    4. Strategy
      1. Modeling Strategies
      2. Refactoring to Strategy
      3. Comparing Strategy and State
      4. Comparing Strategy and Template Method
      5. Summary
    5. Command
      1. A Classic Example: Menu Commands
      2. Using Command to Supply a Service
      3. Command in Relation to Other Patterns
      4. Summary
    6. Interpreter
      1. An Interpreter Example
      2. Interpreters, Languages, and Parsers
      3. Summary
  10. Extension Patterns
    1. Introducing Extensions
      1. Reuse as an Alternative to Extension
      2. Extending by Subclassing
      3. The Liskov Substitution Principle
      4. Extending by Delegating
      5. Summary
      6. Beyond Ordinary Extension
    2. Decorator
      1. A Classic Example of Decorator: Streams
      2. Function Decorators
      3. Decorating without Decorator
      4. Summary
    3. Iterator
      1. Type-Safe Collections
      2. Iterating Over a Composite
      3. Thread-Safe Iterators
      4. Summary
    4. Visitor
      1. Supporting Visitor
      2. Extending with Visitor
      3. Visitor Cycles
      4. Visitor Controversy
      5. Summary
  11. Appendixes
    1. Directions
      1. Get the Most from This Book
      2. Understand the Classics
      3. Weave Patterns into Your Code
      4. Keep Learning
    2. Solutions
      1. Introducing Interfaces (Chapter 2)
      2. Adapter (Chapter 3)
      3. Facade (Chapter 4)
      4. Composite (Chapter 5)
      5. Bridge (Chapter 6)
      6. Introducing Responsibility (Chapter 7)
      7. Singleton (Chapter 8)
      8. Observer (Chapter 9)
      9. Mediator (Chapter 10)
      10. Proxy (Chapter 11)
      11. Chain of Responsibility (Chapter 12)
      12. Flyweight (Chapter 13)
      13. Introducing Construction (Chapter 14)
      14. Builder (Chapter 15)
      15. Factory Method (Chapter 16)
      16. Abstract Factory (Chapter 17)
      17. Prototype (Chapter 18)
      18. Memento (Chapter 19)
      19. Introducing Operations (Chapter 20)
      20. Template Method (Chapter 21)
      21. State (Chapter 22)
      22. Strategy (Chapter 23)
      23. Command (Chapter 24)
      24. Interpreter (Chapter 25)
      25. Introducing Extensions (Chapter 26)
      26. Decorator (Chapter 27)
      27. Iterator (Chapter 28)
      28. Visitor (Chapter 29)
      1. Classes
      2. Class Relationships
      3. Interfaces
      4. Objects
      5. States
  12. Glossary
  13. Bibliography
  14. Index