Argument Interface

A turbocharged version of ArgumentObject.

You want to define an abstract numerical model element for passing to sliders, numeric text boxes etc. To start with this looks pretty simple:

   interface Numeric{
     String title();
     double value();
     void setValue(double value);
     double minimum();
     double maximum();
   }
You also need a callback to do stuff when the value changes:
   class _AbstractNumeric implements Numeric{
     _AbstractNumeric(String Title,double min,double max,double value){...
     String title(){...}
     double value(){...}
     double minimum(){...}
     double maximum(){...}
     void setValue(double value){
       ...//range checking etc
       valueChanged();
     }
     abstract void valueChanged();
   }
Then things start to get complicated. You have to decide how to handle You build all these in by a mishmash of parameters, overrideable methods and trivial subclasses, and develop the surface types to match them. Your API and code are pretty messy, but you can just about live with that. Then you want to try a new implementation of the base class, but all the widgets are now tied not to the original interface but to the concrete classes.

The answer is to add helper interfaces to the original:

   interface Numeric{
     interface Constraints {
       double minimum();//Can now vary dynamically
       double maximum();
       double increment(int step);//Constrain, define nudge values etc
       //Handle range errors, force values etc
       double validValue(double proposed,double current);
       String[]nudgeTitles()//eg "Up","Down"
       ...//any other features required
     }
     interface Coupler{//Couples Numeric mechanism to Constraints policy, and both to the client code
       void valueChanged(Numeric n);
       Constraints constraints(Numeric n);
     }
     String title();
     double value();
     void setValue(double value);
     Constraints constraints();
   }
Now the basic interface is so clean that you can create instances using a single, implementation independent factory method:
   final class _FieldFactory{...
     Numeric newNumeric(String title,double value,Coupler coupler){...}
   }
Not only that, logically linked fields, e.g. coordinates, can call the same coupler when they change. You can have semi-standard _IntegerConstraints, _FractionalConstraints, even _ExponentialConstraints that dynamically adjust the nudge value logarithmically.

To the old computing saw that there is no problem in software that cannot be solved by another indirection, I would add - or interface.

-- DavidWright


CategoryRefactoring


EditText of this page (last edited December 1, 2005) or FindPage with title or text search