Web Tier Design Patterns Objectives ! " Learn design patterns relevant to web application programming ! " Model View Controller ! " Front Controller ! " View Helper ! " Composite View ! " Business Delegate ! " Dispatcher View ! " Service To Worker ! " Understand how enterprise applications can benefit from these patterns The Problem ! " Application construction ! " Applications built by many groups of developers ! " Groups have different skills ! " Groups have different objectives ! " Groups have different timelines ! " This translates into various pieces of the application being developed independently ! " Application use ! " Applications often also need to support multiple clients ! " Different clients can interact with application in different ways ! " Share business data in different ways ! " Interactions follow different flows ! " This increases the complexity and risk of an application The Solution: Model-View-Controller ! " Model ! " Represents business data and business rules ! " Typically a Java Bean or a POJO (plain old java object) ! " Controller ! " Maps user interactions to calls on the model, which often results in a change to the business data ! " View ! " Renders the data encapsulated by the model and guarantees coordination with any changes to the business data ! " Each view must remain consistent with the backend data it represents Benefits of MVC for the Web Tier ! " Separation of concern ! " Maintain modularized approach ! " Increased flexibility ! " For example, can move web-resident model to the EJB tier ! " Model components can be viewed in multiple ways ! " Support for new clients is simple MVC Diagram MODEL! -Encapsulates business data! and business rules! -Handles calls from the CONTROLLER! -Notifies VIEW of changes" VIEW! -Displays the MODEL data! -Sends user request to CONTROLLER! -Notified by MODEL of state change" User request" Select View" CONTROLLER! -Provides business logic! -Translates user requests into calls to MODEL! -Selects VIEW for response" The Model 1 Architecture ! " A variation of MVC for the web ! " Useful for smaller, simpler applications ! " Not as loosely coupled as larger-scale applications require ! " Not as complex to build / maintain ! " Model-1 architecture focuses on two components ! " JSP ! " Each JSP is responsible for handling requests ! " Also responsible for providing responses ! " JavaBeans ! " Model-1 solutions typically have large amount of JSP scriptlet code Model 1 Diagram request! (view)! jsp! response! client! JavaBean! (controller)! database! (model)! The Model 2 Architecture ! " Also a variation of MVC ! " Looser coupling ! " Higher cohesion ! " Increased modularity = increased flexibility ! " Better for larger-scale applications ! " Model-2 architecture adds servlet to Model-1 architecture ! " Servlet acts as a controller ! " Servlet focuses on low level tasks ! " Model-2 architecture changes role of JSP ! " JSP focuses on presentation only ! " Lessens scriplet code ! " Much stricter separation of model from view from controller Model 2 Diagram (view)! request! response! jsp! Servlet" (controller)! client! database! JavaBean! (model)! Front Controller ! " The “front door” to your application ! " Generally a pre-processor, but can be used for post- processing too ! " Often used to enforce ! " Validation rules ! " Authentication constraints ! " Dispatching of requests ! " Session management Front Controller Class Diagram Client! request! Front Controller! <<servlet>>! ServletFC! <<jsp>>! JSPFC! Front Controller Sequence Diagram Client! Controller! 1. Send Request! Dispatcher! View! 1.1 Delegate Request! 1.1.1 Forward Request! 2. Send Request! 2.1 Forward Request! Front Controller Servlet Strategy ! " This is the most common approach ! " All requests route through the servlet ! " Front Controller processing is more logically separated from the view Front Controller JSP Strategy ! " Generally, this is undesirable due to the mismatch of low- level processing and view processing ! " Tends to lead to complicated JSP code Modularized Front Controller ! " For complex logic, a front controller can be broken into smaller components ! " For example, an authentication component, validation component, and dispatching component ! " This continues the pursuit of architectural modularity View Helper ! " Separates presentation logic from presentation components (views) ! " Represent the model from a web tier perspective ! " Typically implemented as a JavaBean component ! " The JSP and View Helper can be implemented independently, allowing for ! " Separation of developer roles ! " Flexibility in the web tier ! " Multiple views sharing the same helper View Helper Class Diagram Client! 1..*! View! 0..*! Helper! View Helper Sequence Diagram Client! 1. Request! View! Helper! 1.1 Get Data! 1.1.1 Get Data! Business" Service! Implementing View Helper as a Tag Library ! " Requires more work upfront to create the tag library ! " May be more reusable than a simple JavaBean component ! " Custom tags also add manageability concerns ! " Deploying tags, tag libraries, and descriptors Business Delegate ! " Used to provide a façade over a complex business tier ! " Normally, presentation components interact with business services directly, which may cause problems ! " If the business tier API changes, it affects the presentation tier ! " Heavy interaction with the business tier may result in too many network calls ! " A Business Delegate can perform caching and throttling Business Delegate Class Diagram Client! Business Delegate! Business Service! Business Delegate Sequence Diagram Helper! 1. Create! Business Delegate! 1.1 Invoke! Business Service! Business Delegate as a Proxy ! " The Business Delegate exposes the same interface as the business tier ! " Calls to the Business Delegate can be forwarded to the business tier ! " Data can also be cached and calls short-circuited ! " This can reduce network calls and increase performance Business Delegate as an Adapter ! " Excellent for communication with disparate business components ! " For example, communication with a web service component could be accomplished ! " The delegate would parse the XML request ! " The data in the request could be sent to an existing, non-XML aware, business tier ! " A response could work the same way Composite View ! " Modern web pages are built from smaller “sub-pages” ! " All Composite View does is define a pattern for creating modularized views that can be combined ! " Complicated views can be managed much simpler via separate view components ! " This does add some runtime overhead in exchange for the added flexibility Composite View Class Diagram View Component! Sub View! Composite View! Dispatcher View ! " Combines Front Controller and View Helper ! " May also involve Composite View Dispatcher View Class Diagram Client! <<servlet>>! Front Controller! 1..*! <<jsp>>! View! 1..*! View Helper! Dispatcher View Sequence Diagram Client! 1. Request! Controller! View! Helper! 1.1 Dispatch! 1.1.1 Get Data! To business tier! Service To Worker ! " Structurally identical to Dispatcher View ! " Difference is in the behavior and location of the intelligence ! " Now, the Front Controller interacts with View Helper components before selecting a view Service To Worker Class Diagram Client! <<servlet>>! Front Controller! 1..*! <<jsp>>! View! 1..*! 1..*! View Helper! Service To Worker Sequence Diagram Client! 1. Request! Controller! View! Helper! 1.1 Get Data! To business tier! 1.1.1 Dispatch! Summary ! " There are several relevant design patterns for the web tier ! " Model View Controller ! " Front Controller ! " View Helper ! " Composite View ! " Business Delegate ! " Dispatcher View ! " Service To Worker ! " These patterns can benefit enterprise applications by promoting modularity, flexibility, and reuse About DevelopIntelligence ! " Founded in 2003 ! " Provides outsourced services to learning organizations in area of software development ! " Represents over 35 years of combined experience, enabling software development community through educational and performance services ! " Represents over 50 years of combined software development experience ! " Delivered training to over 40,000 developers worldwide ! 2003 - 2007 DevelopIntelligence Areas of Expertise ! " Instruction " Java " J2EE " WebServices / SOA " Web Application Development ! " Database Development ! " Open Source Frameworks ! " Application Servers ! ! ! ! ! 2003 - 2007 DevelopIntelligence ! " Courseware ! " Java Application Development ! " Java Web App Development ! " Enterprise Java Development ! " OOAD / UML ! " IT Managerial ! " Emerging Technologies and Frameworks Contact Us ! " For more information about our services, please contact us: ! " Kelby Zorgdrager ! " Kelby@DevelopIntelligence.com ! " 303-395-5340 ! 2003 - 2007 DevelopIntelligence