[23] in java-interest

home help back first fref pref prev next nref lref last post

Perform

daemon@ATHENA.MIT.EDU (James B. Gledhill)
Thu Apr 27 17:11:19 1995

Date: Thu, 27 Apr 1995 10:42:07 -0500
From: james.gledhill@wichita.brite.com (James B. Gledhill)
To: java-interest@java.Eng.Sun.COM

The following code shows one way that an Ojbect could implement the
'perform' method.  This example allows for any number of arguments to
be associated with the method. Error check should be done by each method.
Perhaps it would be better to have two methods, perform, with no args, and
performWith, with an object as a single argument (like NeXTSTEP). This would
cut down on the error checking required.

The way I implemented this class does not allow for nice inheretance.
Comments and suggestion are requested/accepted.  However it would be really
nice to see this a part of Object (and all subclasses).  Three methods,
perform(), performWith(Object), and boolean canPerform(String) would be
nice.


--------Sample Code-------------------------------------

import java.util.Hashtable;
import java.lang.Integer;
import java.io.*;
import java.lang.*;

class Foo1 {

    static Hashtable methods = new Hashtable(3);
    final static int ZERO = 0;
    final static int ONE = 1;
    final static int MANY = 99;

    public Foo1() {
        methods.put("zero", new Integer(ZERO));
        methods.put("one", new Integer(ONE));
        methods.put("many", new Integer(MANY));
    }
 
    public boolean canPerform (String s) {
        return methods.containsKey(s);
    }

    public void perform(String doit[]) {
        switch ( translate(doit[0]) ) {
            case ZERO: zero(); break;
            case ONE: one(doit[1]); break;
            case MANY: many(doit); break;
        }
    }    
 
    private int translate(String s) {
        if ( ! methods.containsKey(s)) {
            // throw exception
        }
        return ((Integer)methods.get(s)).intValue();
    }
     
    public void zero () {
        System.out.println("zero");
    }
 
    public void one (String s) { 
        System.out.println("one " + s);  
    }
 
    public void many (String s[]) {
        for (int i = 0; i < s.length; i++) {
            System.out.print(s[i] + " ");
        }
        System.out.println();
    }
}

--------------End Sample Code-------------------------------

The following code would give these results.

$> java Foo zero
zero

$> java Foo one test
one test

$> java Foo one "this is a test"
one this is a test

$> java Foo many Hello World
many Hello World

--------Sample Code-------------------------------------

import java.lang.*;
import java.oi.*;
import Foo1;

class Foo {
    public static void main(String argv[]) {
        Foo1 test = new Foo1();

        if (test.canPerform(argv[0])) {
            test.perform(argv);
        } else {
            System.err.println("Invalid method name");
        }
    }    
}
--------------End Sample Code-------------------------------


- James Gledhill

----------------------------------------------------------------------
james.gledhill@brite.com                         Phone: (316) 652-6538
----------------------------------------------------------------------

-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post