Some reader favorites:
EJB fundamentals and session beans
Create a scrollable virtual desktop in Swing
Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API
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 But what are you supposed to do when you need to write code that has branching logic? Often, you can sidestep the issue by making sure that lookup values are stored in a database. If the database solution is not an option, you can usually solve the problem by mimicking the database solution with Java Hashtables. But if you have to repeatedly implement this solution, with slightly different conditions and events, you will discover as I have, a burning need for a more general solution.
This article is about a little framework I have developed for writing code with branching logic without nested ifs. The framework resides in a single package and has a simple API that you must use. Using this framework, you can rewrite the code in Listing 1 in just a few lines (shown below), and encapsulate the conditions and consequences in a table that is easy to maintain.
void decideUrl() {
try {
Invoker inv = new ConcreteInvoker((Updateable)this);
inv.execute();
}
catch(NestingTooDeepException ntde) {}
catch(IllegalExpressionException iee) {}
catch(RuleNotFoundException rnfe) {}
catch(DataNotFoundException dnfe) {}
}
My discussion of this framework will span three issues of JavaWorld. The first two parts of the series are devoted to an explanation of how to use the framework. As I develop my ideas, I will show you how to code the branching logic of Listing 1 without nested ifs. After reading these first two parts, you should easily be able use the framework in your own work -- simply import the logic package (see Resources) and follow the steps described in the article. In the third part of the series, I will explain in greater detail the design and implementation of the framework, and will discuss refinements that you can introduce if desired. The seed ideas for this framework are among the many profound design secrets I learned from Ali Arsanjani during informal discussions in 1998 (see Resources).
Free Download - 5 Minute Product Review. When slow equals Off: Manage the complexity of Web applications - Symphoniq
![]()
Free Download - 5 Minute Product Review. Realize the benefits of real user monitoring in less than an hour. - Symphoniq