Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Build dynamically extensible applications

Find out how to build programs that you can extend dynamically -- even while they execute

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 3 of 3

How does it stack up?

I've shown you the technique for extending your applications as they execute. Now it's time for me to put my money where my mouth is and show you a functional demo. Luckily, I am prepared. Take a look at the applet below.

You need a Java-enabled browser to see this applet.

This applet is an implementation of a simple stack-based machine, which consists of a stack of items -- in this case, a collection of zero or more numbers stacked one on top of another, just like in the figure below.

A stack of numbers

There are only two valid operations you can perform on this stack of numbers. You can either push a number on the top of the stack or you can pop a number off the top of the stack. The figure below illustrates these operations.

Pushing onto and popping off of a stack

Take some time to peruse Stack.java, the source code for the stack. Stack.java is a subclass of Panel, and it displays the top five numbers in the stack.

The stack is embedded within a control panel. The control panel includes a field in which commands can be entered and a selector from which the previously entered command can be selected.

Believe it or not, this simple stack forms the basis of a very powerful calculating machine. All we need to complete the program are a few operators (add, subtract, and so on) to operate on the numbers in the stack. However, the operators can only operate on the numbers via the two operations mentioned earlier -- push and pop. Let me give you an example.

Consider an operator called add. This operator pops the top two numbers off the stack, adds them, and then pushes the result back on the stack. When the operation is complete, the stack will be one item shorter and the top two numbers will have been replaced by their sum.

Now add is only one of many different operators. I can think of several more off hand -- subtract, divide, factor, truncate, and convert to francs, to name a few. There are plenty more where those came from, but I wouldn't want to have to sit down and think of them all, much less code them.

Which brings me to my next point. Rather than trying to think up and code all possible operators, I'd like to build the framework and perhaps provide a few common operators. I'll leave the fancy operators -- such as converting to francs -- for you to develop and add into the program as you need them.

With that in mind, take a look at a method that accepts a string, locates the correct class, loads it, and invokes the proper method:

private void invoke(String str) throws Exception
{
   Double d = null;

// parse the string
try { d = new Double(str); } catch (NumberFormatException nfexp) { ; }
// if the string is not a number, it must be // a operator... let's try it...
if (d == null) { _ch.addItem(str);
// load the class...
Class c = Class.forName("howto.operators." + str);
// create a new instance...
Object o = c.newInstance();
// cast it...
Operator op = (Operator)o;
// do it...
op.operate(_s); } else { _s.push(d.doubleValue()); } }


Pretty simple, isn't it? The complete code for the control panel is in Main.java.

Let me tell you a bit more about the applet before I conclude. You can push numbers on the stack by typing them in the text field. Once you've pushed a few numbers, try out the following operators: add, subtract, clone, pop, rotate, and swap. I bet you can you guess what they do.

About the author

Todd Sundsted has been writing programs since computers became available in convenient desktop models. Though originally interested in building distributed applications in C++, Todd moved to the Java programming language when it became the obvious choice for that sort of thing. Todd is co-author of the Java Language API SuperBible. In addition to writing, Todd is president of Etcee, which offers Java-centric training, mentoring, and consulting.

Conclusion

Consider for a moment more what a powerful tool I've left you with. Even though I didn't write every conceivable operator, I've made it possible for you to add those you need, when you need them, without ever having to touch the original source code. That's not a convenience to be lightly dismissed. It's certainly something I'd like to see more of. Application developers, I hope you're listening.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (1)
Login
Forgot your account info?

GreatBy Anonymous on March 30, 2009, 9:27 amreally helpfull, hope its still usable today :)

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Browse the documentation for class Class online http://www.javasoft.com/products/jdk/1.0.2/api/java.lang.Class.html
  • See how dynamic extensibility is handled in TCL http://sunscript.sun.com/man/tcl8.0/TclCmd/load.htm
  • See how dynamic extensibility is handled in PERL ftp://ftp.cis.ufl.edu/pub/perl/CPAN/doc/manual/html/pod/perlmod.html
  • See how dynamic extensibility is handled in Python http://www.python.org/doc/ext/node21.html#SECTION00400000000000000000
  • Download this article and the complete source code as a gzipped tar file http://www.javaworld.com/javaworld/jw-09-1997/howto/jw-09-howto.tar.gz
  • Download this article and the complete source code as a zip file http://www.javaworld.com/javaworld/jw-09-1997/howto/jw-09-howto.zip
  • Previous How-To Java articles