***********************************************
*                                             *
*     The Anal-Retentive Java Programmer      *
*                                             *
*               by F. E. Tunalu               *
*                                             *
*          January 1998 Installment           *
*                                             *
***********************************************


Hello and welcome to another installment of the "Anal-
Retentive Java Programmer!"

This month as part of our ongoing "Math Widget" project
we are going to define the "addition" class.

The addition class is used to add two numbers together.

    public class Addition
    {
        int Add(int x, int y)
        {
            return x + y;
        }
    }


Of course, to make this sufficiently general purpose, we
should perhaps define a superclass "Operator" and make
Addition a subclass of this.

    public class Operator
    {
        int Operate(int x, int y)
        {
            return x;
        }
    }

    public class Addition extends Operator
    {
        int Operate(int x, int y)
        {
            return x + y;
        }
    }
    
But, of course we might have unary operators, so perhaps we should
define Operator as BinaryOperator, a subclass of Operator.

No, wait, we can simply include the the individual types of
operations within the same class.  That's much neater.

    public class Operator
    {
        int Operate(void)
        {
            return 0;
        }

        int Operate(int x)
        {
            return x;
        }
        int Operate(int x, int y)
        {
            return x;
        }
        int Operate(int x, int y, int z)
        {
            return x;
        }
    }

I think three is enough, don't you? 

Now of course, we might not be operating on integers, but
some other kind of objects, so we should take care to define
what we are operating on in a sufficiently general way.

    public class Operator
    {
        Thing Operate(void)
        {
            return new Thing();
        }

        Thing Operate(Thing x)
        {
            return x;
        }

        Thing Operate(Thing x, Thing y)
        {
            return x;
        }

        Thing Operate(Thing x, Thing y, Thing z)
        {
            return x;
        }
    }


Hmmm... But now we have potentially allocated a new
Thing, so I'd like to be careful and invoke the garbage collector
after using this operator.  Why don't we go ahead and make
a disposal method.

    public class Operator
    {
        void Dispose()
        {
            Runtime rt = Runtime.getRuntime();
            rt.gc();
        }
        .
        .
    }


Now that's nice and tidy.

However...

I find the call to the garbage collector to be a bit unsightly.  So
perhaps we should make a garbage collector container to handle our
garbage collection for us.  Then we can make a nicer constructor which
will balance our Dispose() method.

    public class Operator
    {
        GarbageCollectorCosy    gcc;

        public Operator()
        {
            gcc = new GarbageCollectorCosy();
        }

        void Dispose()
        {
            gcc.collect();
        }
        .
        .
        .
    }

Oh.  This is still somewhat bulky.  Perhaps to clean it up we should
make a superclass of Operator to hide the messy details from us.

    public class ThingThatDoesStuff
    {
        .
        .
        .
    }
    
    
Well, I see I'm out of room.  Join us next month for the next installment
of "The Anal-Retentive Java Programmer!"

* * *

Do you have a question for Dr. F. E. Tunalu?  The good doctor can
be reached at tunalu@jbum.com.

* * *

go back