Java EE Patterns Student Guide - Volume 1 SL-500 Rev C.0.1 D61856GC10 Edition 1.0 D62465
Copyright 2005, 2009, Oracle and/or its affiliates. All rights reserved. Disclaimer This document contains proprietary information, is provided under a license agreement containing restrictions on use and disclosure, and is protected by copyright and other intellectual property laws. You may copy and print this document solely for your own use in an Oracle training course. The document may not be modified or altered in any way. Except as expressly permitted in your license agreement or allowed by law, you may not use, share, download, upload, copy, print, display, perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express authorization of Oracle. The information contained in this document is subject to change without notice. If you find any problems in the document, please report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not warranted to be error-free. Sun Microsystems, Inc. Disclaimer This training manual may include references to materials, offerings, or products that were previously offered by Sun Microsystems, Inc. Certain materials, offerings, services, or products may no longer be offered or provided.oracle and its affiliates cannot be held responsible for any such references should they appear in the text provided. Restricted Rights Notice If this documentation is delivered to the U.S. Government or anyone using the documentation on behalf of the U.S. Government, the following notice is applicable: U.S. GOVERNMENT RIGHTS The U.S. Government s rights to use, modify, reproduce, release, perform, display, or disclose these training materials are restricted by the terms of the applicable Oracle license agreement and/or the applicable U.S. Government contract. Trademark Notice Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. UNIX is a registered trademark licensed through X/Open Company, Ltd.
This page intentionally left blank.
This page intentionally left blank.
Table of Contents About This Course...Preface-i Course Goals... Preface-i Course Map...Preface-ii Topics Not Covered...Preface-iii How Prepared Are You?...Preface-iv Introductions...Preface-v How to Use Course Materials...Preface-vi Conventions...Preface-vii Icons...Preface-vii Typographical Conventions... Preface-viii Additional Conventions... Preface-ix Exploring Object-Oriented Design Principles and Design Patterns...1-1 Objectives... 1-1 Relevance... 1-2 Additional Resources... 1-3 Reviewing Design Goals... 1-4 Exploring Object-Oriented Design Concepts... 1-6 Cohesion... 1-7 Encapsulation... 1-9 Coupling... 1-12 Implementation Inheritance... 1-14 Composition... 1-16 Interface Inheritance... 1-17 Polymorphism... 1-19 Exploring Object-Oriented Design Principles... 1-20 Favoring Composition... 1-21 Programming to an Interface... 1-23 Designing for Change... 1-26 Introducing Design Patterns... 1-27 Origin of the Pattern Concept... 1-28 Design Pattern Catalogs... 1-29 Gang of Four (GoF) Patterns... 1-29 v
Design Pattern Selection... 1-30 Design Pattern Elements... 1-31 Design Pattern Notation... 1-33 When and How to Apply Patterns... 1-35 Summary... 1-37 Using Gang of Four Behavioral Patterns... 2-1 Objectives... 2-1 Relevance... 2-2 Additional Resources... 2-3 Introducing Behavioral Patterns... 2-4 Applying the Strategy Pattern... 2-5 Example Problem... 2-5 Problem Forces... 2-5 Solution... 2-6 Example Solution... 2-7 Consequences... 2-9 Applying the Command Pattern... 2-10 Example Problem... 2-10 Problem Forces... 2-10 Solution... 2-12 Example Solution... 2-14 Use in the Java Programming Language... 2-15 Consequences... 2-16 Applying the Iterator Pattern... 2-17 Example Problem... 2-17 Problem Forces... 2-17 Solution... 2-18 Example Solution... 2-19 Use in the Java Programming Language... 2-20 Consequences... 2-21 Applying the Observer Pattern... 2-22 Example Problem... 2-22 Problem Forces... 2-23 Solution... 2-24 Example Solution... 2-25 Use in the Java Programming Language... 2-28 Consequences... 2-29 Summary... 2-30 Using Gang of Four Creational Patterns... 3-1 Objectives... 3-1 Relevance... 3-2 Additional Resources... 3-3 Introducing Creational Patterns... 3-4 Applying the Factory Method Pattern... 3-5 Example Problem... 3-5 vi J2EE Patterns
Problem Forces... 3-5 Solution... 3-6 Example Solution... 3-7 Use in the Java Programming Language... 3-7 Consequences... 3-9 Applying the Abstract Factory Pattern... 3-10 Example Problem... 3-10 Problem Forces... 3-11 Solution... 3-12 Example Solution... 3-13 Consequences... 3-14 Applying the Singleton Pattern... 3-15 Example Problem... 3-15 Problem Forces... 3-15 Solution... 3-16 Example Solution... 3-17 Consequences... 3-17 Summary... 3-18 Using Gang of Four Structural Patterns...4-1 Objectives... 4-1 Relevance... 4-2 Additional Resources... 4-3 Introducing Structural Patterns... 4-4 Applying the Façade Pattern... 4-5 Example Problem... 4-6 Problem Forces... 4-7 Solution... 4-8 Example Solution... 4-9 Consequences... 4-10 Applying the Proxy Pattern... 4-11 Example Problem... 4-11 Problem Forces... 4-12 Solution... 4-13 Example Solution... 4-14 Use in the Java Programming Language... 4-15 Consequences... 4-16 Applying the Adapter Pattern... 4-17 Example Problem... 4-18 Problem Forces... 4-19 Solution... 4-19 Example Solution... 4-22 Consequences... 4-23 Applying the Composite Pattern... 4-24 Example Problem... 4-24 Problem Forces... 4-25 vii
Solution... 4-26 Example Solution... 4-27 Use in the Java Programming Language... 4-29 Consequences... 4-30 Applying the Decorator Pattern... 4-31 Example Problem... 4-31 Problem Forces... 4-33 Solution... 4-33 Example Solution... 4-35 Use in the Java Programming Language... 4-37 Consequences... 4-38 Summary... 4-39 Using Architectural Building Blocks... 5-1 Objectives... 5-1 Relevance... 5-2 Additional Resources... 5-3 Comparing Architectural Patterns to Design Patterns... 5-4 Applying the Model View Controller (MVC) Pattern... 5-5 Example Problem... 5-5 Problem Forces... 5-6 Solution... 5-7 Example Solution... 5-8 Consequences... 5-9 Applying the Layers Pattern... 5-10 Example Problem... 5-10 Problem Forces... 5-10 Solution... 5-11 Example Solution... 5-12 Consequences... 5-12 Applying Layers and Tiers in J2EE Platform Enterprise Applications... 5-13 SunTone Architecture Methodology Layers... 5-13 SunTone Architecture Methodology Tiers... 5-14 J2EE Platform Tiers... 5-16 Summary... 5-19 Introducing J2EE Patterns... 6-1 Objectives... 6-1 Relevance... 6-2 Additional Resources... 6-3 Exploring the J2EE Pattern Philosophy... 6-4 Purpose of the J2EE Patterns... 6-4 Benefits... 6-5 Relation to the J2EE BluePrints Design Guidelines... 6-6 Gang of Four and J2EE Patterns... 6-6 viii J2EE Patterns
Describing the Patterns and Tiers of the J2EE Pattern Catalog... 6-7 Integration Tier Patterns... 6-7 Business Tier Patterns... 6-8 Presentation Tier Patterns... 6-9 J2EE Pattern Relationships... 6-10 Summary... 6-12 Using Integration Tier Patterns...7-1 Objectives... 7-1 Relevance... 7-2 Additional Resources... 7-3 Introducing Integration Tier Patterns... 7-4 Applying the Service Activator Pattern... 7-7 Problem Forces... 7-7 Solution... 7-8 Strategies... 7-10 Example... 7-11 Consequences... 7-13 Related Patterns... 7-13 Applying the Data Access Object Pattern... 7-14 Problem Forces... 7-14 Solution... 7-16 Strategies... 7-18 Example... 7-19 Consequences... 7-21 Related Patterns... 7-22 Applying the Domain Store Pattern... 7-23 Problem Forces... 7-23 Solution... 7-24 Example... 7-29 Strategies... 7-32 Consequences... 7-32 Related Patterns... 7-33 Applying the Web Service Broker Pattern... 7-34 Problem Forces... 7-34 Solution... 7-35 Strategies... 7-36 Example... 7-37 Consequences... 7-39 Related Patterns... 7-40 Summary... 7-41 Using Presentation-to-Business Tier Patterns...8-1 Objectives... 8-1 Relevance... 8-2 Additional Resources... 8-3 Introducing Business Tier Patterns... 8-4 ix
Applying the Service Locator Pattern... 8-5 Problem Forces... 8-5 Solution... 8-6 Strategies... 8-9 Example... 8-10 Consequences... 8-11 Related Patterns... 8-12 Applying the Session Façade Pattern... 8-13 Problem Forces... 8-13 Solution... 8-14 Strategies... 8-16 Example... 8-17 Consequences... 8-19 Related Patterns... 8-19 Applying the Business Delegate Pattern... 8-20 Problem Forces... 8-20 Solution... 8-20 Strategies... 8-23 Example... 8-23 Consequences... 8-25 Related Patterns... 8-25 Applying the Transfer Object Pattern... 8-26 Problem Forces... 8-26 Solution... 8-26 Strategies... 8-29 Example... 8-31 Consequences... 8-32 Related Patterns... 8-33 Summary... 8-34 Using Intra-Business Tier Patterns... 9-1 Objectives... 9-1 Relevance... 9-2 Additional Resources... 9-3 Introducing Intra-Business Tier Patterns... 9-4 Applying the Application Service Pattern... 9-5 Problem Forces... 9-5 Solution... 9-6 Strategies... 9-7 Example... 9-8 Consequences... 9-10 Related Patterns... 9-11 Applying the Business Object Pattern... 9-12 Problem Forces... 9-12 Solution... 9-13 Strategies... 9-15 x J2EE Patterns
Example... 9-15 Consequences... 9-16 Related Pattern... 9-16 Applying the Transfer Object Assembler Pattern... 9-17 Problem Forces... 9-17 Solution... 9-18 Strategies... 9-20 Example... 9-20 Consequences... 9-22 Related Patterns... 9-23 Applying the Composite Entity Pattern... 9-24 Problem Forces... 9-24 Solution... 9-26 Strategies... 9-28 Example... 9-28 Consequences... 9-31 Related Patterns... 9-31 Applying the Value List Handler Pattern... 9-32 Problem Forces... 9-32 Solution... 9-33 Strategies... 9-36 Example... 9-37 Consequences... 9-39 Related Patterns... 9-39 Summary... 9-40 Using Presentation Tier Patterns...10-1 Objectives... 10-1 Relevance... 10-2 Additional Resources... 10-3 Introducing the Presentation Tier Patterns... 10-4 Describing the Model 2 Architecture and Struts Framework... 10-5 Traditional Model View Controller Architecture... 10-5 Model 2 Architecture... 10-6 Apache Struts Framework... 10-7 Applying the Intercepting Filter Pattern... 10-9 Problem Forces... 10-9 Solution... 10-10 Strategies... 10-13 Example... 10-14 Consequences... 10-16 Related Patterns... 10-16 Applying the Front Controller Pattern... 10-17 Problem Forces... 10-17 Solution... 10-18 Strategies... 10-20 xi
Example... 10-21 Consequences... 10-22 Applying the Application Controller Pattern... 10-23 Problem Forces... 10-23 Solution... 10-24 Strategies... 10-26 Example... 10-27 Consequences... 10-28 Related Patterns... 10-29 Applying the Context Object Pattern... 10-30 Problem Forces... 10-30 Solution... 10-30 Strategies... 10-33 Example... 10-35 Consequences... 10-37 Related Patterns... 10-37 Summary... 10-38 More Presentation Tier Patterns... 11-1 Objectives... 11-1 Relevance... 11-2 Additional Resources... 11-3 Describing the Additional Presentation Tier J2EE Patterns... 11-4 Applying the View Helper Pattern... 11-5 Problem Forces... 11-5 Solution... 11-5 Strategies... 11-8 Example... 11-9 Consequences... 11-10 Related Patterns... 11-10 Applying the Composite View Pattern... 11-11 Problem Forces... 11-11 Solution... 11-12 Strategies... 11-13 Example... 11-14 Consequences... 11-15 Related Patterns... 11-16 Applying the Dispatcher View Pattern... 11-17 Problem Forces... 11-17 Solution... 11-18 Example... 11-20 Consequences... 11-22 Related Patterns... 11-23 Applying the Service to Worker Pattern... 11-24 Problem Forces... 11-24 Solution... 11-25 xii J2EE Patterns
Example... 11-27 Consequences... 11-29 Related Patterns... 11-29 Service to Worker Pattern In Struts... 11-30 Summary... 11-32 Exploring AntiPatterns...12-1 Objectives... 12-1 Relevance... 12-2 Additional Resources... 12-3 Introducing AntiPatterns... 12-4 Describing Integration Tier AntiPatterns... 12-5 Not Pooling Connections AntiPattern... 12-6 Monolithic Consumer AntiPattern... 12-8 Fat Message AntiPattern... 12-9 Hot Potato AntiPattern... 12-11 Describing Business Tier AntiPatterns... 12-13 Sledgehammer for a Fly AntiPattern... 12-14 Local and Remote Interfaces Simultaneously AntiPattern... 12-16 Accesses Entities Directly AntiPattern... 12-18 Mirage AntiPattern... 12-19 Cacheless Cow AntiPattern... 12-20 Conversational Baggage AntiPattern... 12-22 Golden Hammers of Session State AntiPattern... 12-23 Describing Presentation Tier AntiPatterns... 12-25 Including Common Functionality in Every Servlet AntiPattern... 12-26 Embedded Navigational Information AntiPattern... 12-27 Ad Lib TagLibs AntiPattern... 12-28 Summary... 12-29 Applying J2EE BluePrints Design Guidelines...13-1 Objectives... 13-1 Relevance... 13-2 Additional Resources... 13-3 Describing the J2EE BluePrints Design Guidelines... 13-4 Object-Oriented Guidelines... 13-4 Client Tier Guidelines... 13-5 Web Tier Guidelines... 13-7 Business Tier Guidelines... 13-8 Integration Tier Guidelines... 13-9 Describing the Java Pet Store Demo Software... 13-10 Analyzing J2EE Patterns in the Java Pet Store Demo Software Web Application Framework... 13-13 J2EE Patterns in the Web Application Framework... 13-15 xiii
Design Pattern Implementation in the Web Application Framework... 13-16 Analyzing J2EE Patterns in the Java Pet Store Demo Software Administrative Classes... 13-31 J2EE Patterns in the Administrative Client Functionality... 13-32 Design Pattern Implementation in the Administrative Client Functionality... 13-32 Summary... 13-38 Quick Reference for UML...A-1 Objectives... A-1 Describing UML Basics... A-3 Describing General Elements... A-6 Packages... A-6 Stereotypes... A-8 Annotation... A-8 Constraints... A-9 Tagged Values... A-9 Describing Use Case Diagrams... A-10 Describing Class Diagrams... A-11 Class Nodes... A-11 Inheritance... A-14 Interface Implementation... A-15 Association, Roles, and Multiplicity... A-16 Aggregation and Composition... A-17 Association Classes... A-19 Other Association Elements... A-21 Describing Object Diagrams... A-22 Describing Collaboration Diagrams... A-24 Describing Sequence Diagrams... A-26 Describing Statechart Diagrams... A-28 Example... A-28 Transitions... A-29 Describing Activity Diagrams... A-30 Describing Component Diagrams... A-34 Describing Deployment Diagrams... A-36 J2EE Technology Review...B-1 Objectives... B-1 Additional Resources...B-3 Reviewing the J2EE Platform...B-4 Understanding Java Remote Method Invocation...B-7 Understanding the Java IDL API...B-11 Understanding the JAX-RPC API...B-12 Understanding the Java Naming and Directory Interface API...B-17 Understanding Servlets...B-20 xiv J2EE Patterns
Understanding JavaServer Pages Technology...B-23 Understanding the Java Message Service API...B-28 Understanding Enterprise JavaBeans Technology Components...B-33 EJB Architecture...B-34 Three Types of EJB Components...B-37 Session Beans... B-38 Entity Beans...B-42 Message-Driven Beans... B-51 Understanding JDBC Technology...B-53 Understanding the J2EE Connector Architecture...B-57 Summary...B-60 Index... Index-1 xv