表示层模式:Composite View—复合视图模式

Context
Sophisticated Web pages present content from numerous data sources, using multiple
subviews that comprise a single display page. Additionally, a variety of individuals with
different skill sets contribute to the development and maintenance of these Web pages.
Problem
Instead of providing a mechanism to combine modular, atomic portions of a view into
a composite whole, pages are built by embedding formatting code directly within each view.
Modification to the layout of multiple views is difficult and error prone, due to the
duplication of code.
Forces
Atomic portions of view content change frequently.
Multiple composite views use similar subviews, such as a customer inventory table.
These atomic portions are decorated with different surrounding template text, or they appear
in a different location within the page.
Layout changes are more difficult to manage and code harder to maintain when
subviews are directly embedded and duplicated in multiple views.
Embedding frequently changing portions of template text directly into views also
potentially affects the availability and administration of the system. The server may need to
be restarted before clients see the modifications or updates to these template components.
Solution
Use composite views that are composed of multiple atomic subviews. Each
component of the template may be included dynamically into the whole and the layout of
the page may be managed independently of the content.
This solution provides for the creation of a composite view based on the inclusion
and substitution of modular dynamic and static template fragments. It promotes the reuse of
atomic portions of the view by encouraging modular design. It is appropriate to use a
composite view to generate pages containing display components that may be combined in
a variety of ways. This scenario occurs, for example, with portal sites that include numerous
independent subviews, such as news feeds, weather information, and stock quotes on a
single page. The layout of the page is managed and modified independent of the subview
content.
Another benefit of this pattern is that Web designers can prototype the layout of a site,
plugging static content into each of the template regions. As site development progresses,
the actual content is substituted for these placeholders.
This pattern is not without its drawbacks. There is a runtime overhead associated with
it, a tradeoff for the increased flexibility that it provides. Also, the use of a more
sophisticated layout mechanism brings with it some manageability and development issues,
since there are more artifacts to maintain and a level of implementation indirection to
understand.

猜你喜欢

转载自abacus.iteye.com/blog/2041712