You are on page 1of 48

J2EE™ Platform Design

Patterns for Enterprise
Applications

Inderjeet Singh
Mark Johnson
J2EE BluePrints
Sun Microsystems, Inc.
2333, J2EE Platform Design Patterns for Enterprise Applications

Why Design Patterns?

A good scientist is a person with
original ideas.

A good engineer is a person who
makes a design that works with as
few original ideas as possible.

—Freeman Dyson

2 2333, J2EE Platform Design Patterns for Enterprise Applications

Goal of This Presentation

To explain several software design patterns
that help a designer use the J2EE platform
most effectively

3 2333, J2EE Platform Design Patterns for Enterprise Applications

Who Is Inderjeet?

• Architect, J2EE BluePrints, Sun
Microsystems
– Investigates application design issues and
their solutions for the J2EE platform
• Contributing author to "Designing
Enterprise Applications with the
Java 2 Platform, Enterprise Edition,
published by Addison-Wesley.
• http://java.sun.com/j2ee/blueprints
• Java Pet Store application
4 2333, J2EE Platform Design Patterns for Enterprise Applications

Elucify Technical Communications • JavaWorld™ magazine J2EE platform (formerly JavaBeans™ technology) columnist • Contract technical writer with J2EE Blueprints group • Technology trainer (with Artima Training) 5 2333. J2EE Platform Design Patterns for Enterprise Applications . Who Is Mark? • President.

J2EE Platform Design Patterns for Enterprise Applications .Learning Objectives • Following this presentation. you will understand: –What Design Patterns are –How and why to use them with J2EE technology –Several specific patterns that maximize the value of the J2EE platform –How to fit these patterns together in an application 6 2333.

Agenda • What are Design Patterns? • Benefits of Design Patterns • What are J2EE Design Patterns? • Presentation of J2EE BluePrints™ patterns • Future work. Summary and Questions 7 2333. J2EE Platform Design Patterns for Enterprise Applications .

responsibilities.What Is a Design Pattern? • Generalized solution to a recurring problem in a particular context • Product of community experience • Four basic elements – name: a “handle” for the pattern – problem: the problem and its context – solution: elements. J2EE Platform Design Patterns for Enterprise Applications . and collaborations – consequences: benefits and tradeoffs 8 2333.

J2EE Platform Design Patterns for Enterprise Applications .Design Pattern Benefits • Capture expertise • Form a common design vocabulary • Make designs more transparent for newcomers • Facilitate restructuring of existing systems 9 2333.

What Is a J2EE Design Pattern? • Effective use of J2EE technology to solve a recurring problem • Experiences of the J2EE technology community • Name consistent with “traditional” patterns • Solution in terms of J2EE technology • Consequences include both benefits and liabilities • Also Implementation Strategies 10 2333. J2EE Platform Design Patterns for Enterprise Applications .

webmacro. and system model • Popular architecture for systems with GUI • Recommended by J2EE BluePrints™ design guidelines • Adopted by many open-source web-tier frameworks (Struts. J2EE Platform Design Patterns for Enterprise Applications .Model View Controller • Problem: highly-interactive system requires extensibility.) 11 2333. user interaction. and multiple user views • Solution: Use MVC to decouple presentation. etc. maintainabilty.

J2EE Platform Design Patterns for Enterprise Applications .MVC Philosophy 12 2333.

J2EE Platform Design Patterns for Enterprise Applications .MVC (Cont. impact analysis.) • Consequences – Clarifies design by separating class roles – Enables multiple data views/multiple users – Simplifies system extension. and deployment – Enhances extensibility and testability – Eases maintenance by decreasing code replication – Can partition developer responsibilities 13 2333.

J2EE Platform Design Patterns for Enterprise Applications .MVC (Cont. so efficiency may be suboptimal 14 2333.) • Implementation Strategies – Carefully analyze communication volume and latency to ensure scalability – Potential maintenance problem if Model API is in flux • Controller usually written in terms of Model • Consider applying Command or Facade pattern to decouple Controller from Model – Abstract components can not use each others’ implementation details.

Front Controller • Problem: Complex interactions between View and Controller • Solution: Implement Controller with Command pattern at a single access point – Translates View requests to logical events – Manages View navigation (response generation) 15 2333. J2EE Platform Design Patterns for Enterprise Applications .

FrontController dispatches events. J2EE Platform Design Patterns for Enterprise Applications . selects views Account Catalog Shopping Cart Model Object Model Object Model Object 16 2333.Front Controller Webpage1 GET Webpage1 POST Webpage2 POST receives input.

Front Controller (Cont.) • Consequences – Separates request processing logic from response generation logic – Centralizes request processing – Simplifies security and logging – Unsuitable for small applications or for web sites with a lot of static content 17 2333. J2EE Platform Design Patterns for Enterprise Applications .

J2EE Platform Design Patterns for Enterprise Applications . implement as a servlet – Use Command pattern to effectively organize request processing and manage complexity – Use with XML-based GUI templates 18 2333.) • Implementation strategies – In Web tier.Front Controller (Cont.

Session Facade • Problem: complicated interactions between collaborating business classes • Solution: apply Facade pattern – Session bean presents unified API – Facade hides Entity bean details 19 2333. J2EE Platform Design Patterns for Enterprise Applications .

) SessionFacade Account Order Shopping Cart Entity Bean Entity Bean Session Bean 20 2333. J2EE Platform Design Patterns for Enterprise Applications .Session Facade (Cont.

Session Facade (Cont. logging – Adds an additional level of indirection – May duplicate API – Possible complexity hazard 21 2333.) • Consequences – Centralizes application interface – Decouples tiers and developers – Supports thin client development – Hides data model complexity from clients – Single point of control for security. J2EE Platform Design Patterns for Enterprise Applications .

Session Facade (Cont. J2EE Platform Design Patterns for Enterprise Applications .) • Implementation Strategies – Implement as a session bean in the EJB™ tier – Create a web-tier proxy for the session bean – Prefer stateless session bean where possible – Use Session Facade for a set of Use Cases 22 2333.

and defer to that class • Especially useful for BMP • Can be generalized to any resource type • Enhances resource and vendor independence 23 2333. J2EE Platform Design Patterns for Enterprise Applications .Data Access Object (DAO) • Problem: data source access complex or vendor-specific • Solution: implement access in a DAO class.

DAO (Cont. J2EE Platform Design Patterns for Enterprise Applications .) accesses BusinessObject DataAccessObject inherits inherits Oracle DAO Sybase DAO adapts adapts Oracle Database Sybase Database 24 2333.

OracleDAO. etc. etc. connector.) • Consequences – Resource type independence • Database.DAO (Cont. directory server. J2EE Platform Design Patterns for Enterprise Applications . – Enhanced extensibility – Easier migration to CMP – Increased complexity: Extra classes • Can be generated by tools 25 2333. – Resource vendor independence • CloudscapeDAO.

J2EE Platform Design Patterns for Enterprise Applications .) • Implementation Strategies – Apply Factory pattern to create DAO instances – Make them pluggable by using deployment descriptor entries or Java Naming and Directory Interface™ (JNDI) names – Generated DAO layer improves reliability 26 2333.DAO (Cont.

J2EE Platform Design Patterns for Enterprise Applications .Value Object • Problem: high network traffic and latency when accessing data values across tiers • Solution: exchange data between tiers in coarse-grained objects that group related values 27 2333.

J2EE Platform Design Patterns for Enterprise Applications .) remote. coarse graines access EJBObject instantiates WebComponent local. fine-grained access ValueObject 28 2333.Value Object (Cont.

) • Consequences – Reduce network traffic and improve response time for coarse-grained. read-only data – Can be cached – Extra classes add complexity – Need to handle stale value objects 29 2333. J2EE Platform Design Patterns for Enterprise Applications .Value Object (Cont.

J2EE Platform Design Patterns for Enterprise Applications .Value Object (Cont.) • Implementation Strategies – Serializable – Provide immutable and mutable versions – Should be relatively small 30 2333.

efficient access in others • Solution: accelerate read-only data access by not using enterprise beans 31 2333.Fast Lane Reader • Problem: application requires transactional access in some situations. J2EE Platform Design Patterns for Enterprise Applications .

Fast Lane (Cont.) read-only data access WebComponent FastLaneReader remote access EJBObject 32 2333. J2EE Platform Design Patterns for Enterprise Applications .

increases complexity – Nontransactional reads inappropriate for data that change often 33 2333. transactional access for updates – Duplicates functionality.) • Consequences – Provides efficient access to read-only data. J2EE Platform Design Patterns for Enterprise Applications .Fast Lane Reader (Cont.

) • Implementation Strategies – DAO must contain all methods that require efficient access – Prefer declarative transaction control 34 2333.Fast Lane Reader (Cont. J2EE Platform Design Patterns for Enterprise Applications .

Page-by-Page Iterator • Problem: need access to pages of items from large remote list • Solution: create server-side interface that returns specified sublist of remote list • Example: query results 35 2333. J2EE Platform Design Patterns for Enterprise Applications .

) iteratively accesses PageByPage accesses ClientObject EntityList Iterator accesses all items at once 36 2333.Page-by-Page Iterator (Cont. J2EE Platform Design Patterns for Enterprise Applications .

Page-by-Page Iterator (Cont. J2EE Platform Design Patterns for Enterprise Applications .) • Consequences – Efficient access to large lists on the server – Potentially higher number of server hits 37 2333.

) • Implementation issues – Trade-off between network transmission load and server hit frequency – Page size may be client-dependent – Pages must be serializable – Caching can improve user experience and decrease network load – Apply with Fast Lane Read pattern 38 2333. J2EE Platform Design Patterns for Enterprise Applications .Page-by-Page Iterator (Cont.

J2EE Platform Design Patterns for Enterprise Applications .Using Session Facade and Front Controller Together Account Order Profile Shopping Cart Session Facade Front Controller WebPage 1 WebPage 1 WebPage 2 WebPage 2 POST GET POST GET 39 2333.

J2EE Platform Design Patterns for Enterprise Applications .Using Fast-Lane Reader and Page-by-Page Iterator Together Catolog DAO Fast Lane Reader Page by Page Iterator Catolog Browser Web Component 40 2333.

J2EE Platform Design Patterns for Enterprise Applications .Using DAO and Value Object Together Customer DB Table Customer Data Access Object Customer Entity Bean Address Profile Order History Value Object Value Object Value Object Display Address Display Profile Order History Web Page Web Page Web Page 41 2333.

Patterns in Java Pet Store Encapsulate Model access DAO Database DAO (web-tier) EJB EJB EJB EJB Fast Lane Reader Session Facade Controller Efficient read only access Simple Value API EJB Controller Page-by-Page Object Iterator Coarse Front Controller Navigate large GrainedObject result sets View Navigation Web-Tier Single Point of entry View Browser 42 2333. J2EE Platform Design Patterns for Enterprise Applications .

3 release and web services – Patterns applicable to the View of MVC 43 2333. J2EE Platform Design Patterns for Enterprise Applications .Futures • Refinements of the Design Patterns – Improvements of the design and applicability – Detailed code examples – UML models – Success stories • More design patterns – Focus on a small set of high-quality patterns – Patterns for the J2EE™ 1.

Summary • Design Patterns for the J2EE™ platform and their benefits • MVC Architectural pattern • Several specific patterns • How patterns work together in the Java Pet Store demo • Future work 44 2333. J2EE Platform Design Patterns for Enterprise Applications .

J2EE Platform Design Patterns for Enterprise Applications .hillside.com/ • Patterns in general: http://www.sun.com • See also Sun Java Center.java.sun.Resources • Visit our website at http://java. “J2EE patterns” http://developer.net/patterns • Your feedback is welcome 45 2333.sun.com/j2ee/blueprints • Send comments to http://j2eepatterns-interest@java.

J2EE Platform Design Patterns for Enterprise Applications . R. —C.Closing Thought There are two ways of constructing a software design. The first method is far more difficult. A. One way is to make it so simple that there are obviously no deficiencies. Hoare 46 2333. And the other way is to make it so complicated that there are no obvious deficiencies.

J2EE Platform Design Patterns for Enterprise Applications .2333.

J2EE Platform Design Patterns for Enterprise Applications .48 2333.