|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Feature Java Feature: The Flexible Model
Loose coupling of Models in an MVC-based framework
Feb. 9, 2006 03:45 PM
This article aims to illustrate how loose coupling of a Model in an MVC-based framework can be achieved by describing a real example - developing a framework for a Web-based XSD-XML generator, which is part of the Event Web research at the Infospheres Lab at Caltech. Why this is important is explained, along with a description of the various techniques used to accomplish the goal. Examples include: how a Model can be initiated in a modular manner; how to add dynamic properties to a Model without polluting the Model base classes; how to change the Model without affecting its existing operations; how the Model can be switched during runtime without affecting interactions with other components; how all these can be done if the Model is complex as in a DOM structure and is generated dynamically. Design patterns are largely employed to construct the framework; how the Eclipse XSD API should be used is also illustrated.
Struts is successful because its framework is sufficient to handle most Web application development. Changes always take place in the Controller and View, e.g., the calculation of a discount in an online store frequently changes; the appearance of the Web application is often changed to enhance the user experience. Why, then, would people ignore the Model? It's because the Model is typically static. For example, the customer and price model remains the same in most online shopping applications. So tight coupling between the Model and other components would normally not be a major problem during redevelopment. It's therefore reasonable for an MVC-based framework to put less emphasis on the Model. However, this isn't true in some cases, as shown in the XSD-XML Web generator below.
Model Matters Only a simplified version of the generator is developed for the prototype. However, redevelopment is needed when:
The complexity of the XSD specification makes these problems worse. Manual effort is needed to determine how each XSD feature should be interpreted in making a form and generating the XML. For example, element declaration and type definition describes what the DOM structure should be like, facets of simple types specify how error checking should be done, and model groups specify the different forms of DOM structures that users can choose from. For the sake of completing the background information before discussing the framework design, we will briefly discuss XSD parsing here, which is how the Model is initiated. A tree structure shown in the following diagram is formed when the example XSD is parsed using Eclipse XSD API. A common mistake is to assume that the tree structure generated by parsing the XSD is exactly the DOM structure of the target XML file, which is clearly not the case as shown. As mentioned, an XML instance specified by a XSD can have alternate DOM structures, so it doesn't make sense to expect that parsing the XSD will generate the corresponding DOM structure directly. Additional manipulation is needed to create the DOM tree of the target XML instance from the tree structure above. The corresponding DOM structure is shown below.
Shaping the Framework
While building our DOM structure after the XSD is parsed, Elements of different types know the different forms of data they are looking for. For example, a built-in type element only looks for the tag name and the built-in type. A complex type element looks for the other elements that make up the complex type as well as the model groups that structure the nested elements. As a result, the parsing process becomes modular - each Element knows how to parse itself. When more advanced XSD features are needed, like group referencing in elements of complex types, only the parsing method of ComplexTypeElement has to be changed. How the other elements are parsed isn't affected. This technique can also be used to build an abstract syntax tree for a compiler or interpreter (see Figure 5). What's more, sub-classing can be done to support even more complicated elements (e.g., elements referencing substitution groups). Since they have the same interface for providing information, adding more Elements in the Model is transparent to the other components. For example, View calls the same "getTag" method on the Elements without knowing their concrete classes. But this doesn't completely solve the problem of supporting extra XSD features. For example, sub-classing the Element won't support simple type elements with different facets, because 'simple type with facet' isn't classified as another type, and sub-classing like this would lead to an explosion of classes. Nor it is desirable to rewrite the entire SimpleTypeElement class to specify how the facet should be handled. In including attributes, the attributes should be added to the individual Element object instead of the entire class, since every Element doesn't need attributes, but this can only be determined at runtime when the XSD is parsed. To address this, a Decorator pattern can be used to attach additional responsibilities to an object dynamically without polluting the Element class:
To sum up, we have now described how our defined DOM structure can support different features of XSD for generating the input form and the XML file - a family of Elements is defined and different Decorators (additional features) contribute to different aspects of the Element. For example, Facet represents the validity of the node values. Of course it should be noted that the Element should only be a data keeper, except in the case of parsing, since only it knows how to initialize itself. How it's manipulated, (say represented in a form) is delegated to other parts of the framework (see Figure 6). YOUR FEEDBACK
LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK SPONSORED BY INFRAGISTICS
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||