Composite ViewContextSystem handles web requests. Presentation processing requires generating a view based on a template and dynamic model. ProblemInstead of providing a mechanism to combine modular, atomic portions of a view into a composite whole, pages are built by embedded formatting code directly within each View. Modifications to the layout of a View are difficult and prone to errors, due to the duplication of code. Forces
SolutionUse composite views that are aggregates of multiple atomic template components that can be included dynamically into the whole. This solution provides for the creation of a composite view based on the inclusion and substitution of modular dynamic and static template fragments. This solution also promotes the reuse of atomic portions of the View, and encourages modular design. It is appropriate to use a composite view to generate displays containing pluggable atomic components that may be combined in a variety of ways. This scenario occurs, for example with portal sites that include numerous independent views, such as news feeds, weather information, and stock quotes on a single page. There may be a runtime overhead associated with this pattern, and it is the tradeoff for the pattern's improved flexibility. A system typically has many views, some of which rely on similar atomic pieces of the display to be presented in alternate ways, such as in different locations on the screen or containing modified text. An example is a customer inventory table that is used on multiple pages, surrounded by different information on each page. Another example, is a page that includes a header and footer. If each atomic subcomponent is coded directly into the main view in which it is included, the result is duplicate code that is difficult to modify and maintain. When a component piece is coded directly into the view in which it is used, a change to that subcomponent requires changing each view that contains the subcomponent, requiring multiple modifications to make, in effect, a single change. StructureThe following class diagram represents the Composite View pattern:
Participants & ResponsibilitiesThe following sequence diagram represents the Composite View pattern:
Composite View A Composite View is a View that is an aggregate of multiple subviews.
View Manager The View Manager manages the inclusion of portions of template fragments into the Composite View. The View Manager may be part of a standard JSPTM runtime engine, in the form of the standard JSP include tag (<jsp:include>) or it may be encapsulated in a JavaBeanTM Helper (JSP 1.0) or Custom Tag (JSP 1.1+) to provide more robust functionality. Additionally, the View Manager may be implemented as an eXtensible Stylesheet Transformer (XSLT). A benefit of using a mechanism other than the standard include tag is that conditional inclusion is easily done. For example, certain template fragments may be included only if the user fulfills a particular role or certain system conditions are satisfied. Furthermore, using a Helper component as a View Manager allows for more sophisticated control of the page structure as a whole, which is useful for creating reusable page layouts.
Included View An Included View is a subview that is one atomic piece of a larger whole View. This included view could also potentially be a composite, itself including multiple subviews. StrategiesJSP View StrategyThe JSPView Strategy suggests using a JSP as the view component. This is the preferred strategy to the ServletView Strategy. While it is semantically equivalent to the Servlet View Strategy, it is a more elegant solution and is more commonly used. Servlet View StrategyThe ServletView Strategy utilizes a servlet as the view. It is semantically equivalent to the preferred JSPView Strategy. However, the ServletView Strategy is often more cumbersome for the software development and web production teams because it embeds markup tags directly within the Java code. When tags are embedded within the code, the view template is more difficult to update and to modify. Additionally, intermingling Java code and markup tags creates a poor separation of user roles within the project and increases the dependencies on the same resources among multiple members of different teams. When an individual works on a template containing unfamiliar code or tags, it increases the likelihood of accidental change introducing problems into the system. There is also a reduction in work environment efficiency (too many people sharing the same physical resource) and an increase in source control management complexity. These problems are more likely to occur in larger enterprise environments that have more complicated system requirements and that use teams of developers. They are less likely to occur with small systems that have simple business requirements and use few developers, because the same individual may likely fill the roles mentioned above. However, keep in mind that often projects start small-with simple requirements and few developers but often ultimately evolve to become sophisticated enough to benefit from these suggestions.
JavaBean View Manager StrategyView Manager is implemented as a JavaBean. The View uses the JavaBean, which implements the custom logic to control the View Management decisions relating to what elements will make up the page. These decisions on what to substitute and include may be based on roles or policies, making it much more powerful than the standard Servlet/JSP include functionality. While it is semantically equivalent to the 'Custom Tag View Manager Strategy', it is not as elegant since it introduces some scriptlet code into the View. Using the 'JavaBean View Manager Strategy' requires less up-front work than using the 'Custom View Manager Strategy', since it is easier to construct JavaBeans and integrate them into a JSP environment. Additionally, even the most novice developers understand JavaBeans. This strategy is also easier from a manageability standpoint, because the completed JavaBeans are the only resulting artifacts to manage and configure.
Custom Tag View Manager StrategyView Manager is implemented as Custom Tag (JSP 1.1+). Logic implemented within the tag controls the View Management decisions relating to what elements will make up the page. These tags extend the behavior of the standard JSP include action and are much more powerful. These custom actions are able to base their decisions for doing page composition on such things as user roles or security policies. Using the 'Custom Tag View Manager Strategy' requires more up-front work than does the 'JavaBean View Manager Strategy', since custom tag development is moderately complicated compared to JavaBean development. Not only is there more complexity in the development process, but there is much more complexity with respect to integrating and managing the completed tags. Using this strategy requires the generation of numerous artifacts, including the tag itself, a tag library descriptor, and configuration files, and configuring the environment with these artifacts.
Transformer View Manager StrategyView Manager is implemented as an XSL Transformer. When combined with other view manager strategies, the page can be built from numerous template fragments. These fragments themselves can be built based on stylesheets that are also composites, with larger stylesheets including and importing smaller stylesheets. Using this strategy can help to enforce the separation of the model from the view, since much of the view markup must be factored into a separate stylesheet.
Early-Binding Static Resource StrategyThe premise of this strategy is to include a piece of a template at translation time, as can be done with the standard JSP include directive. The Early-Binding Static Resource Strategy provides flexibility for maintaining and updating a relatively static template. Its use is recommended if a view includes headers and footers that change infrequently.
Late-Binding Static Resource StrategyThe premise of this strategy is to include a piece of a template at runtime, as can be done with the standard JSP include action. The Late-Binding Static Resource Strategy, which is an alternative to the Early-Binding Static Resource Strategy, is best used when aspects of the template change frequently or the template changes based on user activity. When using this strategy, a header may be included at runtime to allow up-to-the minute changes to appear in the display. Moreover, runtime inclusion as part of this strategy may be done in a policy-based manner. This means that inclusion of certain portions of a template might be based on such criteria as a user role or an access control list. Late-Binding Dynamic Resource StrategyThe Late-Binding Dynamic Resource Strategy is semantically equivalent to recursively applying the composite view pattern, since a dynamic resource that is included as a portion of a particular display may also be a composite view. Performance issues may arise when using this strategy, since runtime inclusion has overhead costs associated with it. An example of this strategy is as follows: Assume we have composite pages of the following structure: Outer.jsp is composed of:
OuterHeader.jsp is composed of:
OuterBody.jsp is composed of:
InnerBody.jsp is composed of:
When we display Outer.jsp, we will be dynamically compiling, if necessary, and including all the dynamic resources that make up this page. One needs to consider the performance impacts of such a structure in light of project requirements.
Consequences
Related Patterns
(c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved. Version 1.0 Beta | |||
|
| ||||||||||||