Design Patterns Java™ Workbook

(Michael S) #1

Table of Contents



  • Foreword............................................................................................................................................................. Table of Contents

  • Preface.................................................................................................................................................................

  • Chapter 1. Introduction To Patterns............................................................................................................

    • Why Patterns?................................................................................................................................................

    • Why Design Patterns?...................................................................................................................................

    • Why Java?.......................................................................................................................................................

    • Why UML?.......................................................................................................................................................

    • Why a Workbook?..........................................................................................................................................

    • The Organization of This Book....................................................................................................................

    • Welcome to Oozinoz!....................................................................................................................................

    • Source Code Disclaimer...............................................................................................................................

    • Summary..........................................................................................................................................................



  • Part I: Interface Patterns..........................................................................................

    • Chapter 2. Introducing Interfaces..............................................................................................................

      • Ordinary Interfaces......................................................................................................................................

      • Interfaces and Obligations..........................................................................................................................

      • Placing Constants in Interfaces.................................................................................................................

      • Summary........................................................................................................................................................

      • Beyond Ordinary Interfaces.......................................................................................................................



    • Chapter 3. Adapter.........................................................................................................................................

      • Adapting in the Presence of Foresight.....................................................................................................

      • Class and Object Adapters.........................................................................................................................

      • Unforeseen Adaptation...............................................................................................................................

      • Recognizing Adapter...................................................................................................................................

      • Summary........................................................................................................................................................



    • Chapter 4. Facade...........................................................................................................................................

      • Refactoring to Facade.................................................................................................................................

      • Facades, Utilities, and Demos...................................................................................................................

      • Summary........................................................................................................................................................



    • Chapter 5. Composite....................................................................................................................................

      • An Ordinary Composite...............................................................................................................................

      • Recursive Behavior in Composites...........................................................................................................

      • Trees in Graph Theory................................................................................................................................

      • Composites with Cycles..............................................................................................................................

      • Consequences of Cycles............................................................................................................................

      • Summary........................................................................................................................................................



    • Chapter 6. Bridge............................................................................................................................................

      • A Classic Example of Bridge: Drivers.......................................................................................................

      • Refactoring to Bridge...................................................................................................................................

      • A Bridge Using the List Interface...............................................................................................................

      • Summary........................................................................................................................................................





  • Part II: Responsibility Patterns...............................................................................

    • Chapter 7. Introducing Responsibility......................................................................................................

      • Ordinary Responsibility...............................................................................................................................

      • Controlling Responsibility with Visibility...................................................................................................

      • Summary........................................................................................................................................................

      • Beyond Ordinary Responsibility................................................................................................................



    • Chapter 8. Singleton......................................................................................................................................

      • Singleton Mechanics...................................................................................................................................

      • Singletons and Threads..............................................................................................................................

      • Recognizing Singleton................................................................................................................................

      • Summary........................................................................................................................................................



    • Chapter 9. Observer....................................................................................................................................... ii

      • A Classic Example: Observer in Swing....................................................................................................

      • Model/View/Controller.................................................................................................................................

      • Maintaining an Observable Object............................................................................................................

      • Summary........................................................................................................................................................



    • Chapter 10. Mediator......................................................................................................................................

      • A Classic Example: GUI Mediators...........................................................................................................

      • Relational Integrity Mediators....................................................................................................................

      • Summary........................................................................................................................................................



    • Chapter 11. Proxy...........................................................................................................................................

      • A Classic Example: Image Proxies...........................................................................................................

      • Image Proxies Reconsidered...................................................................................................................

      • Remote Proxies..........................................................................................................................................

      • Summary......................................................................................................................................................



    • Chapter 12. Chain of Responsibility.......................................................................................................

      • Varieties of Lookup....................................................................................................................................

      • Refactoring to Chain of Responsibility...................................................................................................

      • Anchoring a Chain......................................................................................................................................

      • Chain of Responsibility without Composite...........................................................................................

      • Summary......................................................................................................................................................



    • Chapter 13. Flyweight..................................................................................................................................

      • Recognizing Flyweight..............................................................................................................................

      • Immutability.................................................................................................................................................

      • Extracting the Immutable Part of a Flyweight.......................................................................................

      • Sharing Flyweights.....................................................................................................................................

      • Summary......................................................................................................................................................





  • Part III: Construction Patterns...............................................................................

    • Chapter 14. Introducing Construction....................................................................................................

      • Ordinary Construction...............................................................................................................................

      • Superclass Collaboration..........................................................................................................................

      • Collaboration within a Class.....................................................................................................................

      • Summary......................................................................................................................................................

      • Beyond Ordinary Construction.................................................................................................................



    • Chapter 15. Builder.......................................................................................................................................

      • Building from a Parser...............................................................................................................................

      • Building under Constraints.......................................................................................................................

      • Building a Counteroffer.............................................................................................................................

      • Summary......................................................................................................................................................



    • Chapter 16. Factory Method......................................................................................................................

      • Recognizing Factory Method...................................................................................................................

      • A Classic Example of Factory Method: Iterators..................................................................................

      • Taking Control of Which Class to Instantiate........................................................................................

      • Factory Method in Parallel Hierarchies..................................................................................................

      • Summary......................................................................................................................................................



    • Chapter 17. Abstract Factory....................................................................................................................

      • Abstract Factories for Families of Objects.............................................................................................

      • Packages and Abstract Factories...........................................................................................................

      • Abstract Factories for Look-and-Feel.....................................................................................................

      • Summary......................................................................................................................................................



    • Chapter 18. Prototype..................................................................................................................................

      • Prototypes as Factories............................................................................................................................

      • Prototyping with Clones............................................................................................................................

      • Using Object.clone().........................................................................................................................

      • Summary......................................................................................................................................................



    • Chapter 19. Memento...................................................................................................................................

      • Memento Durability....................................................................................................................................

      • Applying Memento.....................................................................................................................................

      • Persisting Mementos across Sessions..................................................................................................

      • Using Strings as Mementos..................................................................................................................... iii

      • Summary......................................................................................................................................................





  • Part IV: Operation Patterns....................................................................................

    • Chapter 20. Introducing Operations........................................................................................................

      • Operations, Methods, and Algorithms....................................................................................................

      • The Mechanics of Methods......................................................................................................................

      • Exceptions in Methods..............................................................................................................................

      • Summary......................................................................................................................................................

      • Beyond Ordinary Operations...................................................................................................................



    • Chapter 21. Template Method...................................................................................................................

      • A Classic Example of Template Method: Sorting.................................................................................

      • Completing an Algorithm..........................................................................................................................

      • Template Method Hooks...........................................................................................................................

      • Refactoring to Template Method.............................................................................................................

      • Summary......................................................................................................................................................



    • Chapter 22. State...........................................................................................................................................

      • Modeling States..........................................................................................................................................

      • Refactoring to State...................................................................................................................................

      • Making States Constant............................................................................................................................

      • Summary......................................................................................................................................................



    • Chapter 23. Strategy....................................................................................................................................

      • Modeling Strategies...................................................................................................................................

      • Refactoring to Strategy.............................................................................................................................

      • Comparing Strategy and State................................................................................................................

      • Comparing Strategy and Template Method..........................................................................................

      • Summary......................................................................................................................................................



    • Chapter 24. Command.................................................................................................................................

      • A Classic Example: Menu Commands...................................................................................................

      • Using Command to Supply a Service.....................................................................................................

      • Command in Relation to Other Patterns................................................................................................

      • Summary......................................................................................................................................................



    • Chapter 25. Interpreter................................................................................................................................

      • An Interpreter Example.............................................................................................................................

      • Interpreters, Languages, and Parsers....................................................................................................

      • Summary......................................................................................................................................................





  • Part V: Extension Patterns....................................................................................

    • Chapter 26. Introducing Extensions.......................................................................................................

      • Reuse as an Alternative to Extension....................................................................................................

      • Extending by Subclassing........................................................................................................................

      • The Liskov Substitution Principle............................................................................................................

      • Extending by Delegating...........................................................................................................................

      • Summary......................................................................................................................................................

      • Beyond Ordinary Extension.....................................................................................................................



    • Chapter 27. Decorator.................................................................................................................................

      • A Classic Example of Decorator: Streams............................................................................................

      • Function Decorators..................................................................................................................................

      • Decorating without Decorator..................................................................................................................

      • Summary......................................................................................................................................................



    • Chapter 28. Iterator.......................................................................................................................................

      • Type-Safe Collections...............................................................................................................................

      • Iterating Over a Composite......................................................................................................................

      • Thread-Safe Iterators................................................................................................................................

      • Summary......................................................................................................................................................



    • Chapter 29. Visitor........................................................................................................................................

      • Supporting Visitor.......................................................................................................................................

      • Extending with Visitor................................................................................................................................

      • Visitor Cycles.............................................................................................................................................. iv

      • Visitor Controversy.....................................................................................................................................

      • Summary......................................................................................................................................................





  • Part VI: Appendixes................................................................................................

    • Appendix A. Directions...............................................................................................................................

      • Get the Most from This Book...................................................................................................................

      • Understand the Classics...........................................................................................................................

      • Weave Patterns into Your Code..............................................................................................................

      • Keep Learning.............................................................................................................................................



    • Appendix B. Solutions................................................................................................................................

      • Introducing Interfaces (Chapter 2)..........................................................................................................

        • SOLUTION 2.1.......................................................................................................................................

        • SOLUTION 2.2.......................................................................................................................................

        • SOLUTION 2.3.......................................................................................................................................

        • SOLUTION 2.4.......................................................................................................................................

        • SOLUTION 2.5.......................................................................................................................................



      • Adapter (Chapter 3)...................................................................................................................................

        • SOLUTION 3.1.......................................................................................................................................

        • SOLUTION 3.2.......................................................................................................................................

        • SOLUTION 3.3.......................................................................................................................................

        • SOLUTION 3.4.......................................................................................................................................

        • SOLUTION 3.5.......................................................................................................................................

        • SOLUTION 3.6.......................................................................................................................................



      • Facade (Chapter 4)....................................................................................................................................

        • SOLUTION 4.1.......................................................................................................................................

        • SOLUTION 4.2 (from page 47)...........................................................................................................

        • SOLUTION 4.3 (from page 48)...........................................................................................................

        • SOLUTION 4.4.......................................................................................................................................



      • Composite (Chapter 5)..............................................................................................................................

        • SOLUTION 5.1.......................................................................................................................................

        • SOLUTION 5.2.......................................................................................................................................

        • SOLUTION 5.3.......................................................................................................................................

        • SOLUTION 5.4.......................................................................................................................................

        • SOLUTION 5.5.......................................................................................................................................

        • SOLUTION 5.6.......................................................................................................................................



      • Bridge (Chapter 6)......................................................................................................................................

        • SOLUTION 6.1.......................................................................................................................................

        • SOLUTION 6.2.......................................................................................................................................

        • SOLUTION 6.3.......................................................................................................................................

        • SOLUTION 6.4.......................................................................................................................................



      • Introducing Responsibility (Chapter 7)...................................................................................................

        • SOLUTION 7.1.......................................................................................................................................

        • SOLUTION 7.2.......................................................................................................................................

        • SOLUTION 7.3.......................................................................................................................................

        • SOLUTION 7.4.......................................................................................................................................



      • Singleton (Chapter 8)................................................................................................................................

        • SOLUTION 8.1.......................................................................................................................................

        • SOLUTION 8.2.......................................................................................................................................

        • SOLUTION 8.3.......................................................................................................................................

        • SOLUTION 8.4.......................................................................................................................................



      • Observer (Chapter 9).................................................................................................................................

        • SOLUTION 9.1.......................................................................................................................................

        • SOLUTION 9.2.......................................................................................................................................

        • SOLUTION 9.3.......................................................................................................................................

        • SOLUTION 9.4.......................................................................................................................................

        • SOLUTION 9.5.......................................................................................................................................

        • SOLUTION 9.6.......................................................................................................................................







  • Mediator (Chapter 10)............................................................................................................................... v

    • SOLUTION 10.1....................................................................................................................................

    • SOLUTION 10.2....................................................................................................................................

    • SOLUTION 10.3....................................................................................................................................

    • SOLUTION 10.4....................................................................................................................................

    • SOLUTION 10.5....................................................................................................................................



  • Proxy (Chapter 11).....................................................................................................................................

    • SOLUTION 11.1....................................................................................................................................

    • SOLUTION 11.2....................................................................................................................................

    • SOLUTION 11.3....................................................................................................................................

    • SOLUTION 11.4....................................................................................................................................



  • Chain of Responsibility (Chapter 12)......................................................................................................

    • SOLUTION 12.1....................................................................................................................................

    • SOLUTION 12.2....................................................................................................................................

    • SOLUTION 12.3....................................................................................................................................

    • SOLUTION 12.4....................................................................................................................................

    • SOLUTION 12.5....................................................................................................................................



  • Flyweight (Chapter 13)..............................................................................................................................

    • SOLUTION 13.1....................................................................................................................................

    • SOLUTION 13.2....................................................................................................................................

    • SOLUTION 13.3....................................................................................................................................

    • SOLUTION 13.4....................................................................................................................................

    • SOLUTION 13.5....................................................................................................................................



  • Introducing Construction (Chapter 14)...................................................................................................

    • SOLUTION 14.1....................................................................................................................................

    • SOLUTION 14.2....................................................................................................................................

    • SOLUTION 14.3....................................................................................................................................

    • SOLUTION 14.4....................................................................................................................................



  • Builder (Chapter 15)..................................................................................................................................

    • SOLUTION 15.1....................................................................................................................................

    • SOLUTION 15.2....................................................................................................................................

    • SOLUTION 15.3....................................................................................................................................



  • Factory Method (Chapter 16)...................................................................................................................

    • SOLUTION 16.1....................................................................................................................................

    • SOLUTION 16.2....................................................................................................................................

    • SOLUTION 16.3....................................................................................................................................

    • SOLUTION 16.4....................................................................................................................................

    • SOLUTION 16.5....................................................................................................................................

    • SOLUTION 16.6....................................................................................................................................

    • SOLUTION 16.7....................................................................................................................................



  • Abstract Factory (Chapter 17).................................................................................................................

    • SOLUTION 17.1....................................................................................................................................

    • SOLUTION 17.2....................................................................................................................................

    • SOLUTION 17.3....................................................................................................................................

    • SOLUTION 17.4....................................................................................................................................

    • SOLUTION 17.5....................................................................................................................................

    • SOLUTION 17.6....................................................................................................................................



  • Prototype (Chapter 18)..............................................................................................................................

    • SOLUTION 18.1....................................................................................................................................

    • SOLUTION 18.2....................................................................................................................................

    • SOLUTION 18.3....................................................................................................................................

    • SOLUTION 18.4....................................................................................................................................

    • SOLUTION 18.5....................................................................................................................................

    • SOLUTION 18.6....................................................................................................................................



  • Memento (Chapter 19)..............................................................................................................................

    • SOLUTION 19.1....................................................................................................................................

    • SOLUTION 19.2....................................................................................................................................

    • SOLUTION 19.3....................................................................................................................................

    • SOLUTION 19.4....................................................................................................................................

    • SOLUTION 19.5....................................................................................................................................



  • Introducing Operations (Chapter 20)...................................................................................................... vi

    • SOLUTION 20.1....................................................................................................................................

    • SOLUTION 20.2....................................................................................................................................

    • SOLUTION 20.3....................................................................................................................................

    • SOLUTION 20.4....................................................................................................................................

    • SOLUTION 20.5....................................................................................................................................



  • Template Method (Chapter 21)...............................................................................................................

    • SOLUTION 21.1....................................................................................................................................

    • SOLUTION 21.2....................................................................................................................................

    • SOLUTION 21.3....................................................................................................................................

    • SOLUTION 21.4....................................................................................................................................



  • State (Chapter 22)......................................................................................................................................

    • SOLUTION 22.1....................................................................................................................................

    • SOLUTION 22.2....................................................................................................................................

    • SOLUTION 22.3....................................................................................................................................

    • SOLUTION 22.4....................................................................................................................................

    • SOLUTION 22.5....................................................................................................................................



  • Strategy (Chapter 23)................................................................................................................................

    • SOLUTION 23.1....................................................................................................................................

    • SOLUTION 23.2....................................................................................................................................

    • SOLUTION 23.3....................................................................................................................................

    • SOLUTION 23.4....................................................................................................................................

    • SOLUTION 23.5....................................................................................................................................

    • SOLUTION 23.6....................................................................................................................................



  • Command (Chapter 24)............................................................................................................................

    • SOLUTION 24.1....................................................................................................................................

    • SOLUTION 24.2....................................................................................................................................

    • SOLUTION 24.3....................................................................................................................................

    • SOLUTION 24.4....................................................................................................................................

    • SOLUTION 24.5....................................................................................................................................

    • SOLUTION 24.6....................................................................................................................................



  • Interpreter (Chapter 25)............................................................................................................................

    • SOLUTION 25.1....................................................................................................................................

    • SOLUTION 25.2....................................................................................................................................

    • SOLUTION 25.3....................................................................................................................................

    • SOLUTION 25.4....................................................................................................................................



  • Introducing Extensions (Chapter 26)......................................................................................................

    • SOLUTION 26.1....................................................................................................................................

    • SOLUTION 26.2....................................................................................................................................

    • SOLUTION 26.3....................................................................................................................................

    • SOLUTION 26.4....................................................................................................................................

    • SOLUTION 26.5....................................................................................................................................

    • SOLUTION 26.6....................................................................................................................................

    • SOLUTION 26.7....................................................................................................................................



  • Decorator (Chapter 27).............................................................................................................................

    • SOLUTION 27.1....................................................................................................................................

    • SOLUTION 27.2....................................................................................................................................

    • SOLUTION 27.3....................................................................................................................................

    • SOLUTION 27.4....................................................................................................................................

    • SOLUTION 27.5....................................................................................................................................



  • Iterator (Chapter 28)..................................................................................................................................

    • SOLUTION 28.1....................................................................................................................................

    • SOLUTION 28.2....................................................................................................................................

    • SOLUTION 28.3....................................................................................................................................

    • SOLUTION 28.4....................................................................................................................................

    • SOLUTION 28.5....................................................................................................................................



  • Visitor (Chapter 29)....................................................................................................................................

    • SOLUTION 29.1....................................................................................................................................

    • SOLUTION 29.2....................................................................................................................................

    • SOLUTION 29.3....................................................................................................................................

      • SOLUTION 29.4.................................................................................................................................... vii

      • SOLUTION 29.5....................................................................................................................................





  • Appendix C. UML at a Glance...................................................................................................................

    • Classes........................................................................................................................................................

    • Class Relationships...................................................................................................................................

    • Interfaces.....................................................................................................................................................

    • Objects.........................................................................................................................................................

    • States...........................................................................................................................................................



  • Glossary..........................................................................................................................................................

  • Bibliography...................................................................................................................................................

Free download pdf