Re: Best Way to Pass Info Between Objects?
ram@zedat.fu-berlin.de (Stefan Ram) writes:
TDD is not the only methodology, where one writes the calls
before one write the callee. In the 70s we already had
something called ?top-down programming? where one writes the
upper-level methods first and then implements the operations
they call.
For example, I assume that the task is given to plot a sine
curve. How do I do this in TDP (top-down programming)? Well,
public static void main( final java.lang.String[] args )
{ final Plotter plotter = new Plotter();
final Sine sine = new Sine();
for( double x = -sine.start(); x < sine.end(); x += sine.step( x ))
plotter.set( x, sine.val( x ));
plotter.pack();
plotter.setVisible(); }
(untested)
Now, I still need to implement ?Plotter? and ?Sine?. I wrote
the code intentionally as if I was not aware of the
complications of the EDT and ?paintComponent?, I wrote it in
a problem-oriented manner, in terms of the application
domain, ignoring the details of Java and Swing. To be aware
of such implementation details is left to the lower level
classes.
Another example. How do I write a recursive directory
listing? I intionally do *not* read the documentation of
java.io nor java.nio! I try to forget the little what I
already know about them. I start to code as if I just knew
the JLS, but not the rest of the Java SE library:
public class Main
{ public static void main( final java.lang.String[] args )
{ final Entry entry = Entry.from( args[ 0 ]);
entry.print(); }}
interface Printer
{ public void print(); }
public abstract class Entry implements Printer
{ /* factory method */
public static Entry from( final java.lang.String path )
{ ... } }
public class Invalid extends Entry
{ public void print()
{ java.lang.System.out.println( "invalid." ); }
... }
public class File extends Entry
{ public void print()
{ java.lang.System.out.println( this.path() ); }
... }
public class Directory extends Entry
{ public void print()
{ java.lang.System.out.println( this.path() );
for( final Entry entry : this.iterable() )
entry.print(); }
... }
(untested)
Only then, I start to look up the given file operations
in order to implement the details ?...?, which then
will be partially ?bottom-up?.
There once was a language ?Elan? that had special support
for TDD (by which I mean ?top-down design?, here) using
?stepwise refinement?
http://en.wikipedia.org/wiki/ELAN_(programming_language)
.