More action with Struts 2
In a recent review of Struts 2 in Action, JW Blogger Oleg Mikheev notes that Struts 2 is "just a collection of extensions built upon WebWork, which is ultimately the right thing to learn before starting a Struts 2 project." While Struts 2 has some architectural flaws, Oleg calls WebWork well-designed, well-tested, and reliable. What are your experiences using Struts 2 and WebWork?

Also see "Hello World the WebWork way," a JavaWorld excerpt from WebWork in Action, by Patrick Lightbody and Jason Carreira.

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Using the if-then-else framework, Part 2

The final steps to coding branching logic without nested ifs

In Part 1 of this article, I introduced the if-then-else framework, a single-package framework that makes it relatively easy to code branching logic without nested ifs, in a maintainable form. The discussion in both parts of the article focuses on how to use the framework and revolves around the task of rewriting a typical piece of nested-if code having three levels of nesting (see Listing 1 below, reproduced from Part 1 for convenience).

Listing 1. Sample nested-if code

void decideUrl() {
     DataBank.Region region = db.getRegion();
     double limit = db.getLimit();
     String id = db.getUserId();
     if(region.equals(DataBank.EAST_REGION)) {
         if(limit > db.LIMIT_THRESHOLD) {
             setUrl(EAST_PRIVILEGED);
         }
         else {
             setUrl(EAST_NOT_PRIVILEGED);
         }
     }
     else if(region.equals(DataBank.WEST_REGION)) {
         if(isMemberWestAlliance(id)) {
             if(limit > db.LIMIT_THRESHOLD) {
                 setUrl(WEST_MEMBER_PRIVILEGED);
             }
             else {
                 setUrl(WEST_MEMBER_NOT_PRIVILEGED);
             }
         }
         else {
             if(limit > db.LIMIT_THRESHOLD) {
                 setUrl(WEST_NONMEMBER_PRIVILEGED);
             }
             else {
                 setUrl(WEST_NONMEMBER_NOT_PRIVILEGED);
             }
         }
     }
     else {
         setUrl(OTHER_REGION);
     }
 }


TEXTBOX:

TEXTBOX_HEAD: Using the if-then-else framework: Read the whole series!



:END_TEXTBOX

Recall that for the sake of having runnable code, I have wrapped this version of the decideURL() method in a class URLProcessor_bad in the downloadable sample code. The implementation of the if-then-else framework in this example will result in a rewrite of decideURL(). (This new version of the method appears in the class URLProcessor_good; see Resources.)

In Part 1, I outlined the main framework classes with which you will work and the four steps required to implement the framework, listed again here:

  1. Determine the conditions. Determine the individual conditions involved in the logic at hand, and implement each as an instance of Condition (or possibly of a Condition subclass).
  2. Determine the actions. Determine the possible actions that need to be executed and implement each as an instance of Action (or possibly of an Action subclass).
  3. Implement the Updateable interface. Decide which object(s) should receive the actions implemented in Step 2 and make sure that it (they) implements the Updateable interface (which means you need to implement the doUpdate() method).
  4. Subclass the Invoker class. Create a concrete invoker subclass of Invoker and implement the loadConditions() and loadRules() methods.


By the end of Part 1, I completed the analysis of the conditions involved in rewriting the decideURL() method. Here is a quick review:

The code in Listing 1 involves three main conditions that must be evaluated for the main action -- setting the value of a URL -- to be fired off: a location condition, a condition on the value of a limit, and a condition concerning membership to "West Alliance." Since the possible values of location cannot be reduced to a single Boolean-valued condition, I broke down the location condition into three finer-grained conditions: whether or not the specified region is EAST_REGION, whether or not it is WEST_REGION, and whether or not it is neither. The analysis of conditions resulted in the creation of five objects, which completed the work for Step 1:

Resources
Recent work by Ali Arsanjani
  • "Service ProviderA Domain Pattern and Its Business Framework Implementation," presented to PloP '99, is available from the online proceedings at
    http://st-www.cs.uiuc.edu/~plop/plop99/proceedings/Arsanjani/provider3.pdf
  • "Rule ObjectA Pattern Language for Pluggable and Adaptive Business Rule Construction," submitted to KoalaPLoP 2000. This paper will appear in the conference proceedings at the conclusion of the conference on May 26, 2000. Look for it online
    http://www.bell-labs.com/topic/conferences/KoalaPLoP/
  • "Analysis, Design, and Implementation of Distributed Java Business Frameworks Using Domain Patterns" in Proceedings of Tools '99, IEEE Computer Society Press 1999, pp. 490-500. Visit the IEEE online at http://www.computer.org